Manuals / SQL / Ch 4

A · QueryBeginner45 min read

4. JOINs — inner, left, and relationships

SQL · 48 pages source format

Relational power: combine tables on keys. INNER JOIN keeps matches. LEFT JOIN keeps all left rows.

What you'll learn

  • INNER JOIN
  • LEFT JOIN
  • Join conditions
  • Table aliases

INNER JOIN

customers INNER JOIN orders ON customers.id = orders.customer_id — only customers with orders.

SELECT o.id, c.name, o.total
FROM orders o
INNER JOIN customers c ON c.id = o.customer_id;

Do this now

SQLBolt lessons 6–8. List order id, customer name, total for each order.

Clear?

LEFT JOIN

LEFT JOIN finds rows in left table with no match — WHERE right.id IS NULL is the anti-join pattern.

SELECT c.id, c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;

Do this now

Find customers who never placed an order.

Clear?

Multi-table joins

Chain joins: orders → customers, orders → products. Alias tables (o, c, p) for readability.

Do this now

3-table join: order line items with product name and customer email.

Clear?

Checklist