← SQL and databases

Project: customer questions

Find your best customers and the quiet ones.

Start this lesson

The last project is about people. Some questions need every customer, even those with no orders (LEFT JOIN). Some need labels (CASE) or lists built by another query (subqueries). There is often more than one correct query; if your result matches, your answer is right.

SELECT c.name, COUNT(o.id) AS orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id;

Every customer with their number of orders. Zara and Tom have none, and COUNT(o.id) correctly shows 0 for them.

When a question says "including", "every" or "even if", think LEFT JOIN. When it says "never" or "not any", think LEFT JOIN … IS NULL or NOT IN.