Skip to content
TechCareer Lab

Library Management Database

Design a small relational database for a library, load sample data and answer real questions with SQL JOINs and aggregate queries.

Beginner

Overview

You will build the database for a small library that lends books to members. It is a compact project, but it covers the core skills of relational database work: modelling data, enforcing relationships and writing queries that combine tables.

You can use PostgreSQL (recommended) or another SQL database. Minor syntax changes may be needed elsewhere.

Step 1: Create the tables

CREATE TABLE books (
  id        SERIAL PRIMARY KEY,
  title     VARCHAR(200) NOT NULL,
  author    VARCHAR(120) NOT NULL,
  published_year INTEGER
);
 
CREATE TABLE members (
  id        SERIAL PRIMARY KEY,
  name      VARCHAR(120) NOT NULL,
  joined_on DATE NOT NULL DEFAULT CURRENT_DATE
);
 
CREATE TABLE loans (
  id          SERIAL PRIMARY KEY,
  book_id     INTEGER NOT NULL REFERENCES books(id),
  member_id   INTEGER NOT NULL REFERENCES members(id),
  borrowed_on DATE NOT NULL,
  returned_on DATE
);

A NULL value in returned_on means the book is still on loan.

Step 2: Add sample data

Insert at least 8 books, 5 members and 10 loans. Include some books that have never been borrowed and at least one member with no loans, so your queries have interesting edge cases.

Step 3: Answer these questions with SQL

  1. List every loan with the book title and member name.
  2. Which books are currently on loan?
  3. List all members and how many books each has borrowed, including members with zero loans.
  4. Which books have never been borrowed?
  5. Who are the three most active borrowers?

Stretch goals

  • Add a due_on column and write a query that lists overdue loans.
  • Add a constraint so a book cannot be lent twice at the same time.
  • Create a view that shows each book's availability.
  • Demo

    SQL JOINs Explained with Practical Examples

    Understand INNER, LEFT, RIGHT and FULL OUTER JOINs using two small tables, see exactly which rows each one returns, and avoid the most common JOIN mistake.

    3 min read