LEFT JOIN: keep rows with no match
LEFT JOIN keeps every row from the left table, even customers with no orders.
SQL · LEFT JOIN: keep rows with no matchBeginner
SELECT c.name, COUNT(o.id) AS orders, COALESCE(SUM(o.total), 0) AS spent
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name
ORDER BY spent DESC;left joinjoinnull