The disappearing branch dashboard
COUNT(key) versus COUNT(*), and a condition in ON versus WHERE · level 1 of 6
Opening your saved work…
Sign in to save across devices → · Download your guest work before switching accounts.
The disappearing branch dashboard · COUNT(key) versus COUNT(*), and a condition in ON versus WHERE · level 1 of 6
How many tools does each branch own?
One row per branch with its number of tools. Watch the count, then run it.
Data: Twin Ports Tool Library.
Why it works, clause by clause
- FROM branches b: every branch must appear, so branches is the left table.
- LEFT JOIN tools t ON t.branch_id = b.id: attach each branch's tools; a branch with none keeps one row of NULLs.
- COUNT(t.id): count the tool key, which is NULL on the placeholder row, so a branch with no tools counts zero.
- GROUP BY b.id, b.name: one row per branch. ORDER BY b.id for a repeatable order.
From the reading: Two counts, one honest, paragraph 3
SELECT b.id AS branch_id, b.name AS branch, COUNT(t.id) AS tool_count
FROM branches b
LEFT JOIN tools t ON t.branch_id = b.id
GROUP BY b.id, b.name
ORDER BY b.id;Query results
Run it to see the result here.Run it to see the result here.
Practice here is evidence for you and your instructor, never a grade. Grades live in Canvas.