See how tables connect, how keys work, and what JOINs actually do — visually and interactively, using the exact database behind the SQLab Hub playground.
Loading live schema from SQLab Hub…
Practice database documentation
The practice database models a small online store. It has 8 tables and 1,493 rows of generated sample data, and it is the database every lesson example, challenge and Playground query on SQLab Hub runs against. The engine is SQLite, so dates are stored as text in YYYY-MM-DD form and true/false flags as 0 or 1. Customer data (users, orders, order items, payments, reviews) and the company's own staff data (employees, departments) are two separate groups of tables: no key links a customer to an employee.
Each table below has what it is for, its columns with what the data actually contains (counted from the database, not typed by hand), things to watch for, and a query to start from.
How the tables relate
One user can have many orders: orders.user_id → users.id
One order can have many order items: order_items.order_id → orders.id
One product can have many order items: order_items.product_id → products.id
One order can have many payments: payments.order_id → orders.id
One user can have many payments: payments.user_id → users.id
One product can have many reviews: reviews.product_id → products.id
One user can have many reviews: reviews.user_id → users.id
One department can have many employees: employees.department_id → departments.id
One employee can have many employees: employees.manager_id → employees.id (a self-reference: an employee’s manager is another employee)
order_items is the bridge table between orders and products: an order can contain many products and a product can appear in many orders. The lessons on JOINs, LEFT JOIN and SELF JOIN use these relationships.
How the data fits together
What you can rely on when you combine tables, and the few things the sample data does not model. Each statement is checked against the database by an automated test.
The data is generated, not real, but the tables agree with each other the way a store's records would. An order's total is the sum of its order lines minus its discount; every line is charged at the product's list price; every payment belongs to the customer who placed the order and is for the order total.
Dates are in a believable order: every order is placed after the customer signed up, ships one to six days after it was placed, and every review comes from a customer who received the product, after it shipped.
Order status was assigned independently of the order date, so some old orders are still marked pending, processing or shipped (59 placed before June 2024). Use status for "what state is it in" questions, not for "how long do orders stay open".
The data ends in December 2024. Queries relative to today, such as DATE('now', '-30 day'), return nothing; anchor date windows on MAX(order_date) instead.
The data is clean: there are no duplicate emails, missing phone numbers or orphaned rows. To practise cleaning dirty data, the lessons build small sample tables with a WITH … AS (VALUES …) clause instead.
One row per customer account. It is the starting point for customer questions: who signed up when, where they live, and whether they pay for a premium membership.
Joins: orders.user_id, payments.user_id and reviews.user_id all point at users.id. 36 users have no orders, so a LEFT JOIN from users is the usual way to include them.
total is the sum of the order's lines after the discount, and item_count is the number of units on those lines.
shipped_date is filled for shipped and delivered orders only; it is NULL for the 97 orders that are pending, processing, cancelled or refunded.
shipping_city and shipping_country are the customer's own city and country.
discount is a fraction between 0 and 0.25, not an amount.
Revenue questions usually exclude cancelled and refunded orders.
Try it: What is the revenue per year, excluding cancelled and refunded orders?
SELECT SUBSTR(order_date, 1, 4) AS year,
COUNT(*) AS orders,
ROUND(SUM(total), 2) AS revenue
FROM orders
WHERE status NOT IN ('cancelled', 'refunded')
GROUP BY year
ORDER BY year;
The catalogue: one row per product with its category, list price, cost and stock level. price minus cost is the gross profit per unit.
Joins: order_items.product_id and reviews.product_id point at products.id.
Columns of the products table
Column
Type and key
In the data
id
INTEGER Primary key
name
TEXT
unique on every row
category
TEXT
10 distinct values
price
REAL
11.99 to 1,299
cost
REAL
5.12 to 402.69
stock
INTEGER
10 to 496
sku
TEXT
unique on every row
status
TEXT ('active'|'discontinued')
'active' (113), 'discontinued' (7)
rating
REAL (1.0-5.0, NULL if no reviews)
1 to 5; NULL in 24 rows
review_count
INTEGER
1 (37), 2 (32), 0 (24), 3 (18), 4 (8), 5 (1)
Things to know
Every product name is unique, and prices are in a realistic range for the item (from 11.99 for a bag of dried mango to 1,299 for a laptop).
rating and review_count summarise the reviews table: rating is the average star rating rounded to one decimal, and both are NULL and 0 for the 24 products nobody has reviewed.
Try it: Which categories have the highest average profit per unit?
SELECT category,
COUNT(*) AS products,
ROUND(AVG(price - cost), 2) AS avg_profit_per_unit
FROM products
GROUP BY category
ORDER BY avg_profit_per_unit DESC;
The lines of each order: which product, how many, at what unit price. It is the bridge table between orders and products, and the place to answer "what sold?" questions.
Joins: order_items.order_id → orders.id and order_items.product_id → products.id. Every order has at least one line, and a product appears at most once per order.
Columns of the order_items table
Column
Type and key
In the data
id
INTEGER Primary key
order_id
INTEGER References orders.id
200 distinct values
product_id
INTEGER References products.id
119 distinct values
quantity
INTEGER
5 (107), 4 (105), 3 (99), 1 (95), 2 (87), 6 (1)
unit_price
REAL
11.99 to 1,299
subtotal
REAL
12.99 to 6,495
Things to know
unit_price is the product's list price, and subtotal is always quantity × unit_price.
Line subtotals are before the order discount; for revenue after discounts use orders.total.
Try it: Which ten products sold the most units?
SELECT p.id, p.name, SUM(oi.quantity) AS units
FROM order_items oi
JOIN products p ON p.id = oi.product_id
GROUP BY p.id, p.name
ORDER BY units DESC
LIMIT 10;
A paid order has one successful payment for its total, sometimes after a declined attempt (19 orders have two payment rows). Pending orders have a pending payment and refunded orders a refunded one.
12 cancelled orders have no payment at all; the other cancelled orders have a single failed attempt.
Try it: What share of payment attempts succeeds for each method?
SELECT method,
COUNT(*) AS payments,
ROUND(100.0 * SUM(status = 'success') / COUNT(*), 1) AS success_pct
FROM payments
GROUP BY method
ORDER BY success_pct DESC;
Product reviews: a 1 to 5 star rating, a short comment and the number of helpful votes.
Joins: reviews.product_id → products.id and reviews.user_id → users.id. 24 products have no reviews.
Columns of the reviews table
Column
Type and key
In the data
id
INTEGER Primary key
product_id
INTEGER References products.id
96 distinct values
user_id
INTEGER References users.id
72 distinct values
rating
INTEGER (1-5)
5 (55), 4 (44), 1 (35), 2 (33), 3 (25)
comment
TEXT
22 distinct values
reviewed_at
TEXT (DATE)
2022-02-13 to 2024-12-31
helpful_votes
INTEGER
0 to 59
Things to know
Every review is by a customer who received the product (a delivered or refunded order) and is dated after it shipped. A customer reviews a product at most once.
Refunded purchases get low ratings, and the comment matches the rating, so text and stars agree. comment has only 22 distinct texts: fine for LIKE practice, not for text analysis.
Try it: Which products have at least three reviews, and what is their average rating?
SELECT product_id, COUNT(*) AS reviews, ROUND(AVG(rating), 2) AS avg_rating
FROM reviews
GROUP BY product_id
HAVING COUNT(*) >= 3
ORDER BY avg_rating DESC;
The store's own staff: one row per employee with department, title, salary, hire date and manager. It is the table for HR questions and for self joins.
Joins: employees.department_id → departments.id. employees.manager_id → employees.id: a manager is another employee, which is what the SELF JOIN lesson uses.
Columns of the employees table
Column
Type and key
In the data
id
INTEGER Primary key
first_name
TEXT
51 distinct values
last_name
TEXT
46 distinct values
email
TEXT
unique on every row
department
TEXT
10 distinct values
department_id
INTEGER References departments.id
10 distinct values
title
TEXT
53 distinct values
salary
REAL
46,044 to 204,449
hire_date
TEXT (DATE)
2015-01-14 to 2024-10-26
manager_id
INTEGER (self-ref, nullable) References employees.id (by convention, not a declared constraint)
10 distinct values; NULL in 10 rows
city
TEXT
20 distinct values
status
TEXT ('active'|'on_leave')
'active' (106), 'on_leave' (14)
Things to know
Each department has one head (manager_id is NULL, top title such as CFO or VP Engineering), and everyone else in the department reports to that head: two levels, 10 heads and 110 reports.
Titles follow pay within a department, but the head is chosen for seniority, so 13 specialists earn more than their head.
department (text) always agrees with department_id, so either can be used for grouping. Some full names repeat (three people are called Amy Nguyen); they are different people.
Try it: How many people report to each department head?
SELECT m.id, m.first_name || ' ' || m.last_name AS head, m.department, COUNT(e.id) AS reports
FROM employees m
JOIN employees e ON e.manager_id = m.id
GROUP BY m.id
ORDER BY reports DESC;
headcount equals the number of employees in that department.
Try it: What is each department's payroll as a share of its budget?
SELECT d.name, d.headcount, d.budget,
SUM(e.salary) AS payroll,
ROUND(100.0 * SUM(e.salary) / d.budget, 1) AS payroll_pct
FROM departments d
JOIN employees e ON e.department_id = d.id
GROUP BY d.id
ORDER BY payroll_pct DESC;