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;
| Join | Keeps |
|---|---|
JOIN (inner) | Only matching rows |
LEFT JOIN | All left rows, matched right rows |
FULL JOIN | All 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! ๐