Shop database as an SQL script

Four linked tables of a fictional office supplies shop as one SQL file: customers, products, orders and order items. With a data dictionary, ten tasks with checked answers and design questions. Public domain, ready for any SQL course.

DatasetBachelor +2CC0 1.0

What you will learn

  • Load a small relational database
  • Practise joins across four tables
  • Read primary and foreign keys from a schema
  • Discuss the design choices of a small database

The script creates and fills four tables. It is the same data as the "Office supplies shop" in the SQL sandbox, so learners can work in the browser and you can load the file into your own database.

Table

Rows

Linked by

customers

8

id

products

6

id

orders

10

customer_id → customers.id

order_items

15

order_id → orders.id, product_id → products.id

One customer, Hannah Wolf, has never ordered: useful for showing what a LEFT JOIN keeps.

The first lines
CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT NOT NULL, company TEXT, city TEXT, country TEXT);
CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, category TEXT, price REAL);
CREATE TABLE orders (id INTEGER PRIMARY KEY, customer_id INTEGER REFERENCES customers(id), order_date TEXT, status TEXT);

Data dictionary

Table

Column

Type

Meaning

customers

id

integer, primary key

Number of the customer, 1 to 8

customers

name, company, city, country

text

Who the customer is and where; seven cities in six countries

products

id

integer, primary key

Number of the product, 1 to 6

products

name

text

For example "Desk lamp"

products

category

text

Paper, Writing or Furniture

products

price

real

Price of one unit, from 3.50 to 149.00

orders

id

integer, primary key

Number of the order, 1 to 10

orders

customer_id

integer, foreign key

Points at customers.id

orders

order_date

text

Date written year-month-day, January to April 2026

orders

status

text

shipped (7), pending (2) or cancelled (1)

order_items

order_id

integer, foreign key

Points at orders.id

order_items

product_id

integer, foreign key

Points at products.id

order_items

quantity

integer

Units of that product in that order

A primary key identifies each row of a table. A foreign key is a column that points at the primary key of another table. order_items links orders and products: one order can hold several products, and one product can appear in many orders.

Loading it

  • In the browser: nothing to load; pick "Office supplies shop" in the SQL sandbox.

  • SQLite on your computer: run sqlite3 shop.db < shop-database.sql, or .read shop-database.sql inside the sqlite3 program.

  • Other systems: the script uses only common SQL (CREATE TABLE, INSERT, REFERENCES), so it should also load in PostgreSQL or MySQL. It was tested in SQLite.

Ten tasks

  1. Which products cost less than 10? Show name and price, cheapest first.

  2. Which customers are in Germany?

  3. How many orders have each status?

  4. List every order with the name of the customer who placed it.

  5. What is the total value of each order (quantity times price)? Which is the largest?

  6. Which customers have never ordered?

  7. How many orders has each customer placed, including customers with none?

  8. How many units of each category were sold in shipped orders?

  9. How much did each customer spend on shipped orders?

  10. Which customers bought furniture?

Tasks 1, 2, 4, 5, 6 and 8 are also the six shop tasks in the SQL sandbox, where "Check my query" compares the learner's result with the expected one.

Solutions

All results were checked by running the script in SQLite.

1. Notebook A5 3.50, Ballpoint pens 4.90, Printer paper 6.20, Whiteboard markers 7.80.

Task 1
SELECT name, price FROM products WHERE price < 10 ORDER BY price;

2. Anna Berger (Hamburg), Emma Schulz (Hamburg), Farid Haddad (Berlin).

3. shipped 7, pending 2, cancelled 1.

Task 3
SELECT status, COUNT(*) AS orders FROM orders GROUP BY status;

4. Ten rows, one per order. Anna Berger appears three times (orders 1, 3 and 10), Ben Okafor twice (orders 2 and 8).

Task 4
SELECT orders.id, customers.name, orders.order_date
FROM orders
JOIN customers ON customers.id = orders.customer_id;

5. Largest: order 2, 298.00 (two office chairs). Then order 6 227.00, order 3 163.00, order 7 62.00, order 1 59.50, order 8 45.00, order 9 42.00, order 10 39.00, order 5 33.20, order 4 14.00.

Task 5
SELECT order_items.order_id, ROUND(SUM(order_items.quantity * products.price), 2) AS total
FROM order_items
JOIN products ON products.id = order_items.product_id
GROUP BY order_items.order_id
ORDER BY total DESC;

6. Hannah Wolf.

Task 6
SELECT customers.name
FROM customers
LEFT JOIN orders ON orders.customer_id = customers.id
WHERE orders.id IS NULL;

7. Anna Berger 3, Ben Okafor 2, Carla Mendes, David Lee, Emma Schulz, Farid Haddad and Greta Nilsson 1 each, Hannah Wolf 0. Use COUNT(orders.id): COUNT(*) would count Hannah's empty joined row and show 1.

Task 7
SELECT customers.name, COUNT(orders.id) AS orders
FROM customers
LEFT JOIN orders ON orders.customer_id = customers.id
GROUP BY customers.name
ORDER BY orders DESC;

8. Paper 42, Writing 18, Furniture 6.

9. Ben Okafor 343.00, Farid Haddad 227.00, Anna Berger 222.50, Emma Schulz 42.00, David Lee 33.20; together 867.70. Carla Mendes (cancelled) and Greta Nilsson (pending) have no shipped order.

Task 9
SELECT customers.name, ROUND(SUM(order_items.quantity * products.price), 2) AS spent
FROM customers
JOIN orders ON orders.customer_id = customers.id
JOIN order_items ON order_items.order_id = orders.id
JOIN products ON products.id = order_items.product_id
WHERE orders.status = 'shipped'
GROUP BY customers.id
ORDER BY spent DESC;

10. Ben Okafor, Anna Berger and Farid Haddad (a JOIN across all four tables with WHERE products.category = 'Furniture' and SELECT DISTINCT customers.name).

Questions on the design

These work well for a discussion after the tasks.

  • order_items has no primary key of its own. Which two columns together identify each row?

  • Prices are stored as REAL, a floating-point number. Why do many shops store money as whole cents or as a DECIMAL type instead?

  • orders has no total column. What is the advantage of calculating the total from order_items each time? What is the disadvantage?

  • Anna Berger and Emma Schulz both work for Northwind Trading, which is stored twice as text. How could a separate companies table avoid that?

Answers to the design questions
  • order_id and product_id together: each product appears at most once per order. A composite primary key on the two columns would enforce that.

  • Floating-point numbers cannot store every decimal exactly, so sums can be off by a tiny amount. Whole cents (INTEGER) or DECIMAL avoid rounding errors in money.

  • Calculated totals are always correct after an item changes. The disadvantage is that every query must join and add up again, and a price change would also change old orders. Real shops therefore store the price paid in order_items as well.

  • A companies table with an id, and a company_id column in customers. A change of company name then happens in one place. This is normalisation.

The data is public domain under CC0 1.0.

Downloads

Reuse this material

CC0 1.0

It's in the public domain: use it however you like. A credit with a link is appreciated, not required.

"Shop database as an SQL script" by ManyPI (https://manypi.com/edu/shop-database-sql), licensed under CC0 1.0 (https://creativecommons.org/publicdomain/zero/1.0/)