Module 8 · Integration

Project: query a library database

By the end you will be able to solve four questions from a library with the queries learned.

About 70 min

0 of 52 lessons

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

Not started

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.

Task 2: active loans

Guided practice

Task 2: active loans

Not started

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.

Task 3: authors with no books

Guided practice

Task 3: authors with no books

Not started

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.

Task 4: books with no active loan

Practice

Task 4: books with no active loan

Not started

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.

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.

Next: Check what you have learned