TExcelTAD
Home › SQL › SQL Joins

SQL Joins

Real data is spread across tables: an order row knows only the customer_id, while the customer's name lives in customers. A join combines rows from two tables based on a related column, so one query can show both.

The idea

sql · try it yourself
SELECT order_id, customer_id, order_date
FROM orders
WHERE order_id IN (1, 2, 3)
ORDER BY order_id;
order_id | customer_id | order_date
---------+-------------+-----------
1        | 5           | 2025-01-18
2        | 18          | 2025-01-02
3        | 17          | 2025-01-19
(3 rows)

The ids 5, 18 and 17 mean little on their own. Join customers on the matching id:

sql · try it yourself
SELECT orders.order_id, customers.name, orders.order_date
FROM orders
JOIN customers ON customers.customer_id = orders.customer_id
WHERE orders.order_id IN (1, 2, 3)
ORDER BY orders.order_id;
order_id | name            | order_date
---------+-----------------+-----------
1        | Le Petit Marche | 2025-01-18
2        | Felix Reyes     | 2025-01-02
3        | Amara Costa     | 2025-01-19
(3 rows)

For every order row, the database finds the customer row where customers.customer_id = orders.customer_id and glues the two together. The condition after ON is the join condition.

Types of joins

Join Returns
INNER JOIN (or just JOIN) only rows that have a match in both tables
LEFT JOIN all rows from the left table, plus matches from the right (NULLs where none)
RIGHT JOIN all rows from the right table, plus matches from the left
FULL JOIN all rows from both tables, matched where possible
CROSS JOIN every combination of rows (no condition)
Self join a table joined with itself (with aliases)
text
 INNER          LEFT           RIGHT          FULL
 A ( █ ) B      A (██ ) B      A ( ██) B      A (███) B

Inner vs left at a glance

Seven customers have never ordered. An inner join leaves them out; a left join keeps them, with NULL in the order columns:

sql · try it yourself
SELECT COUNT(DISTINCT c.customer_id) AS inner_join_customers
FROM customers AS c
INNER JOIN orders AS o ON o.customer_id = c.customer_id;

SELECT COUNT(DISTINCT c.customer_id) AS left_join_customers
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id;
inner_join_customers
--------------------
18
(1 row)

left_join_customers
-------------------
25
(1 row)

Joining more than two tables

Chain joins one after another. Each order line with the order date, product name and category:

sql · try it yourself
SELECT o.order_id, o.order_date, p.name AS product, c.name AS category, oi.quantity
FROM order_items AS oi
JOIN orders     AS o ON o.order_id = oi.order_id
JOIN products   AS p ON p.product_id = oi.product_id
JOIN categories AS c ON c.category_id = p.category_id
WHERE o.order_id = 19
ORDER BY p.name;
order_id | order_date | product        | category    | quantity
---------+------------+----------------+-------------+---------
19       | 2025-05-09 | Chai Latte Mix | Beverages   | 5
19       | 2025-05-09 | Espresso Beans | Beverages   | 3
19       | 2025-05-09 | Power Bank     | Electronics | 4
19       | 2025-05-09 | Tortilla Chips | Snacks      | 5
(4 rows)

CROSS JOIN

A cross join pairs every row of one table with every row of the other — the Cartesian product. Useful for generating combinations, dangerous by accident (forgetting the ON condition in old-style joins produces one):

sql · try it yourself
SELECT s.status, c.name AS category
FROM (SELECT DISTINCT status FROM orders WHERE status IN ('shipped', 'delivered')) AS s
CROSS JOIN categories AS c
WHERE c.category_id <= 3
ORDER BY s.status, c.name;
status    | category
----------+----------
delivered | Bakery
delivered | Beverages
delivered | Dairy
shipped   | Bakery
shipped   | Beverages
shipped   | Dairy
(6 rows)

Note: The old comma syntax FROM orders, customers WHERE orders.customer_id = customers.customer_id is an inner join too. Prefer explicit JOIN … ON — the join condition is then clearly separated from the filters.

Test yourself

Which join returns only rows with a match in both tables?

  1. LEFT JOIN
  2. INNER JOIN
  3. FULL JOIN
  4. CROSS JOIN

A CROSS JOIN of a 3-row and a 4-row table returns how many rows?

  1. 3
  2. 4
  3. 7
  4. 12

Which join keeps all customers, even those without orders, when written customers ... JOIN orders?

  1. INNER JOIN
  2. LEFT JOIN
  3. RIGHT JOIN
  4. NATURAL JOIN