CodeBlanks Logo Icon CodeBlanks

Find overdue loans

Find overdue loans with LEFT JOIN and WHERE.

Overdue books

To find overdue books you filter the LEFT JOIN result with WHERE. Records past their due date are picked.

SELECT b.title, l.due_date
FROM books b
LEFT JOIN loans l ON l.book_id = b.id
WHERE l.due_date < CURRENT_DATE;

This query returns books past their due date.

b.title, l.due_date is selected from books b. loans l is joined on l.book_id = b.id, keeping every row of the left table. WHERE keeps only the rows where l.due_date < CURRENT_DATE holds.

Overdue books — details

CURRENT_DATE gives today. If due_date is earlier, the book is overdue.

This query lists loans whose due date is before today; ORDER BY due_date puts the most overdue book at the top.

Practice Overdue books

Filter the LEFT JOIN result with due_date < CURRENT_DATE.

Starter code

query.sql
-- overdue
SELECT b.title, l.due_date
FROM books b
LEFT JOIN loans l ON l.book_id = b.id

Sample solution

query.sql
SELECT b.title, l.due_date
FROM books b
LEFT JOIN loans l ON l.book_id = b.id
WHERE l.due_date < CURRENT_DATE;

Expected output

title    due_date
Suç ve Ceza  2026-01-10

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Find overdue loans: match the terms

Term Meaning
CURRENT_DATE today
due_date due date
WHERE filter
overdue past due

2 How is overdue detected?

  • When due_date is earlier than today Correct answer
  • When due_date is later
  • With SUM
  • With COUNT

Explanation: due_date < CURRENT_DATE signals overdue.

3 Add WHERE

Question
SELECT b.title, l.due_date
FROM books b
LEFT JOIN loans l ON l.book_id = b.id
____ l.due_date < CURRENT_DATE;
Answer
SELECT b.title, l.due_date
FROM books b
LEFT JOIN loans l ON l.book_id = b.id
WHERE l.due_date < CURRENT_DATE;

Explanation: WHERE l.due_date < CURRENT_DATE finds loans whose due date has passed.

All lessons in Intermediate