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;
First 6 of 11 rows from the company database Open in editor
last_namerolehours
BauerTech Lead800
BauerTech Lead560
DuboisDesigner480
IqbalArchitect720
IqbalArchitect400
LindqvistEngineer640
Use when: You only want records that exist in both tables — employees with departments, orders with customers, etc.

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;
First 6 of 16 rows from the company database Open in editor
last_namerole
ChenNULL
BauerTech Lead
BauerTech Lead
IqbalArchitect
IqbalArchitect
VenkatEngineer

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;
5 rows from the company database Open in editor
first_namelast_name
AlexandraChen
DanielOsei
YunaPark
OliviaWalsh
LeoAbramov
Use when: You need a complete list from one table with optional data from another — all orders (including unassigned ones), all products (even without categories).

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;
First 6 of 16 rows from the company database Open in editor
last_namerole
ChenNULL
BauerTech Lead
BauerTech Lead
IqbalArchitect
IqbalArchitect
VenkatEngineer
Best practice: Avoid RIGHT JOIN. It is harder to read and reason about. Flip the tables and use LEFT JOIN instead.

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;
6 rows from the company database Open in editor
last_namerole
LindqvistNULL
WalshNULL
OkonkwoAnalyst
NULLCreative Lead
NULLDesigner
NULLFinance Lead
Use when: You need a complete picture combining two tables with no pre-filtering — audit reports, reconciliation queries, data quality checks.

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;
First 6 of 25 rows from the company database Open in editor
departmentproject
EngineeringAurora Platform
EngineeringBlue River Mobile
EngineeringCopper Pipeline
EngineeringDelta Rebrand
EngineeringEagle Compliance
SalesAurora Platform
Warning: CROSS JOIN on large tables produces enormous result sets. Always verify your table sizes before running.

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;
First 6 of 13 rows from the company database Open in editor
employeemanager
ChenNULL
BauerChen
IqbalChen
VenkatBauer
LindqvistBauer
OseiNULL
Use when: Hierarchies (manager-employee), comparing rows within one table, finding related pairs.

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.