JOINs are how relational databases connect data across tables. They are also the most commonly misunderstood part of SQL. Every example below runs on the company practice database, and the rows under it are its real result.
INNER JOIN — Matching Rows Only
INNER JOIN returns only rows that have matching values in both tables. Any row without a match in the other table is excluded from the result.
SELECT e.last_name, a.role, a.hours
FROM employees e
INNER JOIN assignments a ON a.employee_id = e.id
ORDER BY e.last_name; | last_name | role | hours |
|---|---|---|
| Bauer | Tech Lead | 800 |
| Bauer | Tech Lead | 560 |
| Dubois | Designer | 480 |
| Iqbal | Architect | 720 |
| Iqbal | Architect | 400 |
| Lindqvist | Engineer | 640 |
LEFT JOIN — All Left, Match Right
LEFT JOIN (also called LEFT OUTER JOIN) returns all rows from the left table, with matching rows from the right table. Where there is no match, the right-side columns return NULL.
SELECT e.last_name, a.role
FROM employees e
LEFT JOIN assignments a ON a.employee_id = e.id
ORDER BY e.id; | last_name | role |
|---|---|
| Chen | NULL |
| Bauer | Tech Lead |
| Bauer | Tech Lead |
| Iqbal | Architect |
| Iqbal | Architect |
| Venkat | Engineer |
Chen has no assignment, so role is NULL. Filter on that NULL to find the rows with no match:
-- Employees with no assignment at all
SELECT e.first_name, e.last_name
FROM employees e
LEFT JOIN assignments a ON a.employee_id = e.id
WHERE a.employee_id IS NULL; | first_name | last_name |
|---|---|
| Alexandra | Chen |
| Daniel | Osei |
| Yuna | Park |
| Olivia | Walsh |
| Leo | Abramov |
RIGHT JOIN — All Right, Match Left
RIGHT JOIN returns all rows from the right table with matching rows from the left table. Non-matching left rows get NULLs. RIGHT JOIN is uncommon in practice — rewrite as LEFT JOIN instead.
-- Every employee again, with the tables written the other way round
SELECT e.last_name, a.role
FROM assignments a
RIGHT JOIN employees e ON a.employee_id = e.id
ORDER BY e.id; | last_name | role |
|---|---|
| Chen | NULL |
| Bauer | Tech Lead |
| Bauer | Tech Lead |
| Iqbal | Architect |
| Iqbal | Architect |
| Venkat | Engineer |
FULL OUTER JOIN — Everything from Both
FULL OUTER JOIN returns all rows from both tables. Where there is no match, the non-matching side gets NULLs. Think of it as LEFT JOIN + RIGHT JOIN combined.
-- Recent hires, and everyone on projects 4 and 5
SELECT e.last_name, a.role
FROM (SELECT * FROM employees WHERE hire_date >= '2020-01-01') e
FULL OUTER JOIN (SELECT * FROM assignments WHERE project_id IN (4, 5)) a
ON a.employee_id = e.id; | last_name | role |
|---|---|
| Lindqvist | NULL |
| Walsh | NULL |
| Okonkwo | Analyst |
| NULL | Creative Lead |
| NULL | Designer |
| NULL | Finance Lead |
CROSS JOIN — Every Combination
CROSS JOIN produces the Cartesian product of two tables — every row
from the first paired with every row from the second. Five departments and
five projects give 25 rows. No ON clause needed.
SELECT d.name AS department, p.name AS project
FROM departments d
CROSS JOIN projects p
ORDER BY d.id, p.id; | department | project |
|---|---|
| Engineering | Aurora Platform |
| Engineering | Blue River Mobile |
| Engineering | Copper Pipeline |
| Engineering | Delta Rebrand |
| Engineering | Eagle Compliance |
| Sales | Aurora Platform |
SELF JOIN — Table to Itself
A self-join joins a table to itself. Useful for hierarchical data like org charts (employees and their managers), or comparing rows within the same table.
SELECT e.last_name AS employee, m.last_name AS manager
FROM employees e
LEFT JOIN employees m ON m.id = e.manager_id
ORDER BY e.id; | employee | manager |
|---|---|
| Chen | NULL |
| Bauer | Chen |
| Iqbal | Chen |
| Venkat | Bauer |
| Lindqvist | Bauer |
| Osei | NULL |
JOIN Types — Side by Side
| JOIN Type | Matches in Both | All from Left | All from Right | Cartesian |
|---|---|---|---|---|
| INNER | ✓ | ✗ | ✗ | ✗ |
| LEFT | ✓ | ✓ | ✗ | ✗ |
| RIGHT | ✓ | ✗ | ✓ | ✗ |
| FULL OUTER | ✓ | ✓ | ✓ | ✗ |
| CROSS | ✗ | ✗ | ✗ | ✓ |
Frequently Asked Questions
- What is the difference between INNER JOIN and LEFT JOIN?
- INNER JOIN returns only rows that have matching values in both tables. LEFT JOIN returns all rows from the left table and matching rows from the right table, with NULLs for non-matching right-side rows.
- When should I use RIGHT JOIN vs LEFT JOIN?
- Prefer LEFT JOIN in most cases — it is more intuitive (all of the left table) and easier to chain. Use RIGHT JOIN only when you genuinely need all rows from the right table and the left table is a lookup. RIGHT JOINs often indicate a query that should be rewritten.
- What is a CROSS JOIN?
- CROSS JOIN returns the Cartesian product of two tables — every row from the first table paired with every row from the second table. With m rows and n rows, you get m × n rows. Use with caution on large tables.
- What is a self-join and when is it useful?
- A self-join joins a table to itself. It is useful for hierarchical data (employees and their managers), comparing rows within the same table (employees in the same department), or finding related pairs (products in the same category). For many of these patterns, window functions like LAG and LEAD offer a simpler, more performant alternative.
- How do multiple JOINs work in a single query?
- JOINs are evaluated left-to-right by default, but you can control order with parentheses. Each JOIN creates an intermediate result that is then joined with the next table. Understanding this matters when some JOINs are LEFT and others are INNER.