List loans with LEFT JOIN
- SQL course
- Intermediate
- Lesson 21/115
- 5 min
List every member and their loans with LEFT JOIN.
Loaned books
To match loaned books with members you use LEFT JOIN. Books never loaned come as NULL.
SELECT b.title, l.loan_date
FROM books b
LEFT JOIN loans l ON l.book_id = b.id;This query lists all books with their loan records.
b.title, l.loan_date is selected from books b. loans l is joined on l.book_id = b.id, keeping every row of the left table.
Loaned books — details
LEFT JOIN keeps every book; with no loan record, loan_date becomes NULL.
This way you see every book; a filled loan_date means it is on loan, and NULL means it is on the shelf.
Practice Loaned books
LEFT JOIN all books with the loans table.
Starter code
-- loan list
SELECT b.title, l.loan_date
FROM books bSample solution
SELECT b.title, l.loan_date
FROM books b
LEFT JOIN loans l ON l.book_id = b.id;Expected output
title loan_date Suç ve Ceza 2026-01-10 Sefiller NULL
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What happens to unloaned books?
- It is deleted
- An error
- They are left out
- NULL is returned Correct answer
Explanation: LEFT JOIN keeps them with NULL.
2 Add LEFT JOIN
SELECT b.title, l.loan_date
FROM books b
____ loans l ON l.book_id = b.id;
SELECT b.title, l.loan_date
FROM books b
LEFT JOIN loans l ON l.book_id = b.id;
Explanation: LEFT JOIN is the right answer.
3 List loans with LEFT JOIN: match the terms
| Term | Meaning |
|---|---|
books |
book |
loans |
loan |
LEFT JOIN |
all left |
NULL |
empty |