Learn with Abdou

Chapter 7 · Concept 6 of 6 · Practice

The checkout count changes a wage total

count repetitions before adding money · level 1 of 6

Opening your saved work…

Sign in to save across devices → · Download your guest work before switching accounts.

The checkout count changes a wage total · count repetitions before adding money · level 1 of 6

Loans checked out by each staff member

Level 1: Watch it answered · Exercise 1 of 1

How many loans has each staff member checked out? Watch the count, then run it. This is the number that will multiply a wage in a moment.

Data: Twin Ports Tool Library.

Why it works, clause by clause

  1. FROM staff s LEFT JOIN loans l ON l.staff_id = s.id: one row per loan, with the staff member attached; a staff member with no loans keeps one placeholder row.
  2. COUNT(l.id) per staff member: how many loans each one checked out.
  3. Read the numbers. Priya has 13. If her wage were on each of those rows, a SUM would count it 13 times.

From the reading: The multiplication trap, paragraph 2

The queryOpening the database…
SELECT s.id AS staff_id, s.first_name, COUNT(l.id) AS matching_loans
FROM staff s
LEFT JOIN loans l ON l.staff_id = s.id
GROUP BY s.id, s.first_name
ORDER BY s.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.