Learn with Abdou

Chapter 7 · Concept 3 of 6 · Practice

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?

Level 1: Watch it answered · Exercise 1 of 1

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

  1. FROM branches b: every branch must appear, so branches is the left table.
  2. LEFT JOIN tools t ON t.branch_id = b.id: attach each branch's tools; a branch with none keeps one row of NULLs.
  3. COUNT(t.id): count the tool key, which is NULL on the placeholder row, so a branch with no tools counts zero.
  4. 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

The queryOpening the database…
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;
Open in the playground

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.