Ask a room of developers which SQL topic confused them longest and joins win every time — not because joins are complicated, but because they are usually taught with Venn diagrams instead of data. This tutorial takes the opposite approach: one tiny dataset, every join type run against it, and the exact rows each query returns, so you can see what each join does.

We will use two tables you could meet in any real project: employees and departments. Two details in the data are deliberate: employee Dana has no department yet (department_id is NULL), and the Research department has no employees. Those two “unmatched” rows are what separate the join types.

CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);

CREATE TABLE employees (
  id            INTEGER PRIMARY KEY,
  name          TEXT NOT NULL,
  department_id INTEGER,          -- can be NULL
  salary        INTEGER NOT NULL
);

INSERT INTO departments VALUES
  (1, 'Engineering'), (2, 'Marketing'), (3, 'HR'), (4, 'Research');

INSERT INTO employees VALUES
  (1, 'Ayesha', 1,    120000),
  (2, 'Bilal',  1,    95000),
  (3, 'Chen',   2,    80000),
  (4, 'Dana',   NULL, 60000),
  (5, 'Omar',   3,    70000);

1. INNER JOIN — Only the Matches

An INNER JOIN returns a row only when the join condition matches in both tables. Dana (no department) disappears, and so does Research (no employees). Four rows survive:

SELECT e.name AS employee, d.name AS department
FROM employees e
INNER JOIN departments d ON e.department_id = d.id
ORDER BY e.id;
-- employee | department
-- Ayesha   | Engineering
-- Bilal    | Engineering
-- Chen     | Marketing
-- Omar     | HR

Use INNER JOIN when you only want complete, matched records — orders with their customers, payments with their invoices. It is the default JOIN in SQL, and the right choice more often than beginners expect.

2. LEFT JOIN — Everything From the Left, Matches Where They Exist

A LEFT JOIN keeps every row of the left table, whether or not a match exists. Where there is no match, the right table’s columns come back as NULL. Dana now appears — with a NULL department:

SELECT e.name AS employee, d.name AS department
FROM employees e
LEFT JOIN departments d ON e.department_id = d.id
ORDER BY e.id;
-- employee | department
-- Ayesha   | Engineering
-- Bilal    | Engineering
-- Chen     | Marketing
-- Dana     | NULL
-- Omar     | HR

This makes LEFT JOIN the tool for finding what is missing. Add one condition and you have the classic “unassigned employees” report:

SELECT e.name
FROM employees e
LEFT JOIN departments d ON e.department_id = d.id
WHERE d.id IS NULL;   -- returns: Dana

3. RIGHT JOIN and FULL OUTER JOIN — The Other Side of the Story

RIGHT JOIN is LEFT JOIN mirrored: every department survives, and Research finally appears — with a NULL employee. A FULL OUTER JOIN keeps unmatched rows from both sides, so Dana and Research both show up:

SELECT e.name AS employee, d.name AS department
FROM employees e
FULL OUTER JOIN departments d ON e.department_id = d.id;
-- employee | department
-- Ayesha   | Engineering
-- Bilal    | Engineering
-- Chen     | Marketing
-- Dana     | NULL
-- Omar     | HR
-- NULL     | Research

Support notes worth knowing: RIGHT and FULL OUTER JOIN work in PostgreSQL, SQL Server and SQLite 3.39+. MySQL supports RIGHT JOIN but has no FULL OUTER JOIN — emulate it with a LEFT JOIN ... UNION ... RIGHT JOIN. In practice, most developers simply swap the table order and use LEFT JOIN instead of RIGHT JOIN, and reach for FULL OUTER mainly in data-reconciliation work.

4. CROSS JOIN — Every Combination (Handle With Care)

A CROSS JOIN pairs every row of one table with every row of the other — the Cartesian product. Five employees times four departments gives 20 rows:

SELECT COUNT(*) FROM employees CROSS JOIN departments;  -- 20

That is occasionally exactly what you want: generating size-by-colour combinations for a product catalogue, or pairing every day in a date range with every store in a sales report. Far more often, a surprise CROSS JOIN is a bug — it is what you get when a join condition is forgotten. If a query suddenly returns row counts that multiply instead of add, check here first.

5. Self Join — A Table Joins Itself

Nothing stops a table from joining itself, with two aliases. Here we find pairs of colleagues who share a department:

SELECT a.name AS employee_a, b.name AS employee_b, d.name AS department
FROM employees a
JOIN employees b
  ON a.department_id = b.department_id AND a.id < b.id
JOIN departments d ON a.department_id = d.id;
-- employee_a | employee_b | department
-- Ayesha     | Bilal      | Engineering

The a.id < b.id condition prevents duplicate pairs (Bilal–Ayesha) and self-pairs (Ayesha–Ayesha). Self joins are the standard trick for hierarchical data too — employees and their managers usually live in the same table.

6. Joins With GROUP BY — Where a Classic Bug Hides

Real reports aggregate. “Headcount and average salary per department, including departments with nobody in them” is a LEFT JOIN from departments to employees:

SELECT d.name,
       COUNT(e.id) AS headcount,
       COALESCE(AVG(e.salary), 0) AS avg_salary
FROM departments d
LEFT JOIN employees e ON e.department_id = d.id
GROUP BY d.name
ORDER BY d.id;
-- name        | headcount | avg_salary
-- Engineering | 2         | 107500
-- Marketing   | 1         | 80000
-- HR          | 1         | 70000
-- Research    | 0         | 0

Notice COUNT(e.id), not COUNT(*). With COUNT(*), Research would report a headcount of 1 — because the LEFT JOIN still produces one row for it (with NULL employee columns), and COUNT(*) counts rows. COUNT(e.id) counts only non-NULL employee ids. This single-character-class bug ships to production dashboards every week; now you will never write it.

7. Choosing the Right Join

Ask one question: which rows must survive even without a match? None — INNER JOIN. All rows from the first table — LEFT JOIN. All from both — FULL OUTER JOIN. Every combination, deliberately — CROSS JOIN. And whatever you choose, make sure the column you join on is indexed: an unindexed foreign key turns every join into a full table scan, which is where “SQL is slow” stories are born. For what happens after the join, our guide to SQL window functions picks up exactly where this one ends.

Frequently Asked Questions (FAQ)

Q: What is the difference between INNER JOIN and LEFT JOIN in one line?

INNER JOIN returns only rows that match in both tables; LEFT JOIN returns every row from the left table, with NULLs where no match exists.

Q: Can I join more than two tables?

Yes — joins chain. Each additional JOIN ... ON ... clause adds another table, as the self-join example above does when it brings in departments as a third table. Real reporting queries routinely join five or more.

Q: Why do I get NULL values after a join?

NULLs in joined columns mean “no matching row existed” in an outer join. They are information, not an error — and filtering on IS NULL is how you turn them into “find the missing records” reports.

Q: Does the order of tables in a join change the result?

For INNER JOIN, no. For LEFT JOIN, absolutely — the table after FROM is the one whose rows all survive. The database’s query planner, not your clause order, decides the actual execution strategy.

Conclusion

Joins stop being mysterious the moment you watch the rows. Keep one small test dataset like this one, run every new join against it, and compare what comes back with what you expected — five minutes of that habit is worth more than memorising every diagram.

💡 Engineering Key Takeaway

INNER = only matches. LEFT = everything from the left, matches where they exist. And in aggregates over a LEFT JOIN, count the joined table’s column — never COUNT(*).

DI

DevInsights Editorial Team

Developer Education Publication

DevInsights is the developer-education publication of NovaSonic Global (Private) Limited, a software company based in Bahawalpur, Pakistan. Every tutorial is written and reviewed by working engineers, and all code is tested before publication.