Learn with Abdou

Chapter 7 · Concept 2 of 6 · Practice

Never borrowed or not returned?

LEFT JOIN and the unmatched row · level 1 of 6

Opening your saved work…

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

Never borrowed or not returned? · LEFT JOIN and the unmatched row · level 1 of 6

Ten tools nobody has borrowed

Level 1: Watch it answered · Exercise 1 of 1

Which tools have never been borrowed? Watch the query answer it, then run it yourself.

Data: Twin Ports Tool Library.

Why it works, clause by clause

  1. FROM tools t: start from the table whose rows must all survive. Every tool, borrowed or not.
  2. LEFT JOIN loans l ON l.tool_id = t.id: pair each tool with its loans. A tool with no loans still gets one row, with NULL in every loan column.
  3. WHERE l.id IS NULL: keep only the rows where the pairing found nothing. That is the gap.
  4. ORDER BY t.name: a repeatable order, so the list reads the same every time.

From the reading: Never borrowed, or not returned?, paragraph 2

The queryOpening the database…
SELECT t.name, t.category
FROM tools t
LEFT JOIN loans l ON l.tool_id = t.id
WHERE l.id IS NULL
ORDER BY t.name;
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.