TExcelTAD
Home › PostgreSQL › The Sample Database

The Sample Database

Every playground in this course starts with a fresh copy of TAD Books, a small online bookshop. Knowing its tables makes the examples in later lessons much easier to follow, so take a few minutes to explore it here. Because each run starts from scratch, you can change the data however you like.

The tables

Table Rows What it holds
categories 7 Book categories: Fiction, Science Fiction, Mystery, Programming, History, Children, Poetry
authors 10 Name, country and birth_year (one author's birth year is unknown: NULL)
books 20 Title, author, category, price, stock, published date, a tags array and a details JSONB document
customers 15 First and last name, email (three customers have none), city, country, joined date
orders 20 Who ordered, order_date (all in 2024) and status: pending, shipped, delivered or cancelled
order_items 34 The lines of each order: book_id, quantity and the unit_price paid
employees 8 Shop staff with job_title, salary, hired date and a manager_id pointing to another employee

A few facts are built in on purpose, because they make interesting queries:

  • Customers 7, 13 and 15 have never placed an order.
  • Books 2, 12 and 16 have never been sold, and books 2 and 12 are out of stock (stock = 0).
  • The category Poetry has no books, and the author Grace Liu has not written any book in the shop yet.
  • Every id is an identity column, so a new row automatically gets the next number (21 for a new book).

How the tables connect

text
categories.id  <── books.category_id
authors.id     <── books.author_id
customers.id   <── orders.customer_id
orders.id      <── order_items.order_id
books.id       <── order_items.book_id
employees.id   <── employees.manager_id   (a self-reference)

Each arrow is a foreign key: the database refuses an order for a customer that does not exist.

A first look at the data

SELECT * returns every column. Here are the categories:

pgsql · try it yourself
SELECT * FROM categories;
 id |      name
----+-----------------
  1 | Fiction
  2 | Science Fiction
  3 | Mystery
  4 | Programming
  5 | History
  6 | Children
  7 | Poetry
(7 rows)

The books table is the richest one. Its tags column is a PostgreSQL array and details is a JSONB document. Later lessons cover both in depth:

pgsql · try it yourself
SELECT id, title, price, stock, tags, details
FROM books
WHERE id <= 4;
 id |       title        | price | stock |      tags       |                                              details
----+--------------------+-------+-------+-----------------+----------------------------------------------------------------------------------------------------
  1 | The Salt Road      | 14.99 |    12 | {travel,family} | {"pages": 312, "format": "paperback", "language": "en"}
  2 | Harbor Lights      | 12.50 |     0 | {family,sea}    | {"pages": 248, "format": "paperback", "language": "en"}
  3 | Signal from Kepler | 18.00 |     7 | {space,aliens}  | {"pages": 401, "format": "hardcover", "series": {"name": "Deep Sky", "part": 2}, "language": "en"}
  4 | The Last Orbit     | 16.75 |     3 | {space}         | {"pages": 356, "format": "paperback", "series": {"name": "Deep Sky", "part": 1}, "language": "en"}
(4 rows)

Orders and their lines live in two tables. This query shows order 3, which contains three different books:

pgsql · try it yourself
SELECT order_id, book_id, quantity, unit_price
FROM order_items
WHERE order_id = 3;
 order_id | book_id | quantity | unit_price
----------+---------+----------+------------
        3 |       6 |        1 |      11.99
        3 |       7 |        1 |      13.49
        3 |      13 |        2 |       8.99
(3 rows)

Inspecting the columns of a table

In psql you would type \d books to see the column definitions. With plain SQL you can ask information_schema.columns:

pgsql · try it yourself
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'customers'
ORDER BY ordinal_position;
 column_name | data_type | is_nullable
-------------+-----------+-------------
 id          | integer   | NO
 first_name  | text      | NO
 last_name   | text      | NO
 email       | text      | YES
 city        | text      | NO
 country     | text      | NO
 joined      | date      | NO
(7 rows)

Tip: Keep this page open in a second tab while you work through the course. When a later example mentions order_items or manager_id, you will know exactly what it means.

Test yourself

Which column links an order to the customer who placed it?

  1. orders.id
  2. orders.customer_id
  3. customers.order_id
  4. order_items.customer_id

What happens to the sample data after you run a playground that deletes rows?

  1. The rows are gone for all later lessons
  2. The next run starts again from a fresh copy
  3. The browser asks you to reinstall
  4. Nothing can be deleted

How does employees record who someone reports to?

  1. With a separate managers table
  2. With manager_id, which references another row of employees
  3. With the job title
  4. It doesn't