Before you start, you need
Practice: choose and check a JOIN · Find matches with EXISTS · Work correctly with NULL values
The challenge
The library needs four lists: books with their authors, loans not yet returned, the number of books per author and books with no active loan. Each task answers one question, so you will write four separate queries.
Start with one task and check what each row of its result represents. The book list has one row per book; the loan list, one per loan; the count, one per author. Do not try to merge the four answers into one table.
A loan is active when return_date IS NULL. The same book can have several returned loans, so counting loans is not the same as counting books. To decide whether a book is available now, check whether any active loan exists.
Some activities add an author with no books, and past loans, to test these cases. Read their prepared data and check the missing values too. Each task stores its result and its hints separately.
Data and requirements
- books holds the catalogue, and author_id references authors.id. An author can have several books or none.
- loans holds the history. return_date IS NULL means the loan is still active.
- A book can have several past loans: counting loans is not counting books. Some activities add prepared cases to check this.
- The stored available flag and the absence of an active loan describe related decisions. In this project, availability is checked against the loan history.
Project stages
- Task 1: one row per book with its matching author. Check the join condition.
- Task 2: one row per active loan, with the title and the member. Use the missing return date.
- Task 3: one row per author, including anyone with no books. Count ids from the books side.
- Task 4: one row per book with no active loan. Look for the absence of matches, even if the book has old returned loans.
Guided practice
Task 1: the catalogue with authors
Show title and the author's name as author for every book with a matching author.
Expected result: 7 rows with the columns title, author, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Each requested row represents a book with a matching author.
The reference to the author is in books.author_id.
Join authors.id to books.author_id and use author as the alias for the name.
Solution explained
SELECT l.title, a.name AS author FROM books AS l JOIN authors AS a ON l.author_id = a.id;Each row represents a book in the catalogue, regardless of its loans.
Task 2: active loans
Guided practice
Task 2: active loans
Write a query that returns the title and the member for loans that have not been returned yet.
Expected result: 3 rows with the columns title, member, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
A loan is still active when it has no return date.
Join book_id to books.id.
Filter return_date IS NULL.
Solution explained
SELECT l.title, p.member FROM loans AS p JOIN books AS l ON p.book_id = l.id WHERE p.return_date IS NULL;The query keeps the join between tables apart from the loan status.
Task 3: authors with no books
Guided practice
Task 3: authors with no books
Show the name of every author and COUNT(l.id) as books. Keep authors who have zero books.
Expected result: 6 rows with the columns name, books, in that order. Row order does not matter.
This activity uses a copy of the data with extra edge cases. Open the full data before you predict the result.
View tables and data
Open this panel to see the full data.
What should happen to an author with no books: do they disappear or appear with 0?
Start from authors and use LEFT JOIN with books to keep every author.
Count with COUNT(l.id), not COUNT(*), and group by author.
Solution explained
SELECT a.name, COUNT(l.id) AS books FROM authors AS a LEFT JOIN books AS l ON a.id = l.author_id GROUP BY a.id,a.name;Book ids are counted; the author with no match gets 0.
Task 4: books with no active loan
Practice
Task 4: books with no active loan
Show the title of books for which no loan with a missing return_date exists. Use NOT EXISTS.
Expected result: 4 rows with the column title, in that order. Row order does not matter.
This activity uses a copy of the data with extra edge cases. Open the full data before you predict the result.
View tables and data
Open this panel to see the full data.
A past loan that has been returned does not tie up the book now.
Look for any active loan of the outer book.
Negate the existence check with NOT EXISTS.
Solution explained
SELECT l.title FROM books AS l WHERE NOT EXISTS (SELECT 1 FROM loans AS p WHERE p.book_id = l.id AND p.return_date IS NULL);Whether there is an active loan is checked against the full history, not a single loan row.
Check
Check one row from each task. In the count, look for the author with no books; in the availability list, look for a book with returned loans. Check that the result still answers the question if the history changes.
Each task stores its result and its hints separately. Solve the last one without copying the solution, to check that you can decide whether an active loan is missing.
Wrap-up
- Define what each row of each task represents.
- Keep authors with no books when the question asks for it.
- An active loan has no return_date.
- Check the full history to decide availability.