Combining tables with joins

Real questions span tables: orders and customers, events and users. Joins put them back together.

Inner join

Rows that match on both sides:

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

Left join

Keep every row on the left, matched or not โ€” the go-to for "including the ones with zero":

SELECT c.name, count(o.order_id) AS orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.name;
JoinKeeps
JOIN (inner)Only matching rows
LEFT JOINAll left rows, matched right rows
FULL JOINAll rows from both sides

Exercise: list customers who have never placed an order. (Hint: LEFT JOIN + IS NULL.)


That's the core of analytics SQL. Nice work finishing the course! ๐ŸŽ‰