Before you start, you need
The problem
You will count shipped orders per customer and books per author. The lists must also include anyone with zero. First decide which table must keep all its rows.
To keep customers, put them on the left of a LEFT JOIN. The shipped-orders condition must limit the matches in ON. Then gather the rows for each customer with GROUP BY.
Count the id from the right-hand table. COUNT(p.id) counts real orders. COUNT(*) would also count the kept row of a customer with no orders and show 1 instead of 0.
Check a case with no matches, one with a single match and one with several. In the challenge the tables will change, but you will still need to keep the missing ones and count the right matches.
How to think about it
What does COUNT(*) count on the row of a customer with no matches?
- Keep customers and limit the matches to shipped orders inside ON.
- Group by id and name. Count p.id, because it is missing when there is no matching order.
- Check a customer with zero, one and several shipped orders. In the challenge, apply that same decision to authors and books.
Guided practice
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Count shipped orders, including zero
Write a query that returns every customer and the number of shipped orders each one has, including zero.
Expected result: 12 rows with the columns name, shipped, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
The customer must stay even if they have no shipped orders. Count real matches.
The shipped status is filtered inside ON.
Count p.id, not *.
Solution explained
SELECT c.name, COUNT(p.id) AS shipped FROM customers AS c LEFT JOIN orders AS p ON c.id = p.customer_id AND p.status = 'shipped' GROUP BY c.id, c.name;This form keeps every customer and counts only shipped matches.
Challenge
Solve this variation. Spot the part that has changed before you look for help.
Practice
Count books per author
Show the author's name and COUNT(l.id) as books for every author, including those with no books.
Expected result: 6 rows with the columns name, books, in that order. Row order does not matter.
This activity uses a copy of the data with extra edge cases. Open the full data before you predict the result.
View tables and data
Open this panel to see the full data.
Every author must survive the join.
Count an attribute that is NULL when there is no book.
COUNT(l.id) tells zero books apart from a kept row.
Solution explained
SELECT a.name, COUNT(l.id) AS books FROM authors AS a LEFT JOIN books AS l ON a.id = l.author_id GROUP BY a.id, a.name;The author with no books gets 0 because the key from the right-hand side is counted, not the kept row.
Key takeaways
- First choose which rows should be kept.
- Count the right-hand key to get zero when there are no matches.
- Group by the entity's id.