SQLab Hub Schema Explorer

Understand SQL by Understanding Your Database

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.

users

Registered customers · 150 rows · 10 columns · Open in the Playground

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.

Columns of the users table
ColumnType and keyIn the data
idINTEGER
Primary key
first_nameTEXT57 distinct values
last_nameTEXT49 distinct values
emailTEXTunique on every row
phoneTEXTunique on every row
cityTEXT54 distinct values
countryTEXT'USA' (52), 'Canada' (21), 'UK' (16), 'Australia' (14), 'Brazil' (14), 'Germany' (13), 'France' (10), 'India' (10)
created_atTEXT (DATE)2019-01-26 to 2024-12-28
statusTEXT ('active'|'inactive')'inactive' (77), 'active' (73)
is_premiumINTEGER (0|1)0 (81), 1 (69)

Things to know

  • city is always a city in the customer's country, and phone numbers start with that country's calling code (+1 for the USA and Canada).
  • status (active or inactive) and is_premium are independent flags.

Try it: How many users signed up each year?

SELECT SUBSTR(created_at, 1, 4) AS year, COUNT(*) AS users
FROM users
GROUP BY year
ORDER BY year;
▶ Run this query in the Playground

orders

Customer orders · 200 rows · 10 columns · Open in the Playground

One row per order, with its status and value. Most revenue, trend and customer-behaviour questions start here.

Joins: orders.user_id → users.id. order_items.order_id and payments.order_id point back at orders.id.

Columns of the orders table
ColumnType and keyIn the data
idINTEGER
Primary key
user_idINTEGER
References users.id
114 distinct values
totalREAL10.52 to 5,887.32
discountREAL0 to 0.25
item_countINTEGER1 to 19
statusTEXT ('pending'|'shipped'|'delivered'|...)'delivered' (82), 'processing' (28), 'cancelled' (27), 'pending' (23), 'shipped' (21), 'refunded' (19)
order_dateTEXT (DATE)2022-01-06 to 2024-12-24
shipped_dateTEXT (DATE, nullable)2022-01-17 to 2024-12-24; NULL in 97 rows
shipping_cityTEXT48 distinct values
shipping_countryTEXT'USA' (75), 'UK' (25), 'Brazil' (21), 'Canada' (19), 'Australia' (17), 'Germany' (17), 'India' (14), 'France' (12)

Things to know

  • 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;
▶ Run this query in the Playground

products

Product catalog · 120 rows · 10 columns · Open in the Playground

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
ColumnType and keyIn the data
idINTEGER
Primary key
nameTEXTunique on every row
categoryTEXT10 distinct values
priceREAL11.99 to 1,299
costREAL5.12 to 402.69
stockINTEGER10 to 496
skuTEXTunique on every row
statusTEXT ('active'|'discontinued')'active' (113), 'discontinued' (7)
ratingREAL (1.0-5.0, NULL if no reviews)1 to 5; NULL in 24 rows
review_countINTEGER1 (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;
▶ Run this query in the Playground

order_items

Line items linking orders to products · 494 rows · 6 columns · Open in the Playground

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
ColumnType and keyIn the data
idINTEGER
Primary key
order_idINTEGER
References orders.id
200 distinct values
product_idINTEGER
References products.id
119 distinct values
quantityINTEGER5 (107), 4 (105), 3 (99), 1 (95), 2 (87), 6 (1)
unit_priceREAL11.99 to 1,299
subtotalREAL12.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;
▶ Run this query in the Playground

payments

Payment transactions · 207 rows · 8 columns · Open in the Playground

Payment attempts, with the method used and whether each one succeeded. Used for payment-method, success-rate and reconciliation questions.

Joins: payments.order_id → orders.id and payments.user_id → users.id (always the customer who placed the order).

Columns of the payments table
ColumnType and keyIn the data
idINTEGER
Primary key
order_idINTEGER
References orders.id
188 distinct values
user_idINTEGER
References users.id
112 distinct values
amountREAL10.52 to 5,887.32
methodTEXT ('Credit Card'|'PayPal'|...)'Credit Card' (46), 'PayPal' (40), 'Debit Card' (31), 'Google Pay' (22), 'Bank Transfer' (19), 'Gift Card' (19), 'Apple Pay' (16), 'Cryptocurrency' (14)
statusTEXT ('success'|'pending'|'failed'|'refunded')'success' (131), 'failed' (34), 'pending' (23), 'refunded' (19)
paid_atTEXT (DATE)2022-01-06 to 2024-12-24
transaction_idTEXTunique on every row

Things to know

  • 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;
▶ Run this query in the Playground

reviews

Product reviews by users · 192 rows · 7 columns · Open in the Playground

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
ColumnType and keyIn the data
idINTEGER
Primary key
product_idINTEGER
References products.id
96 distinct values
user_idINTEGER
References users.id
72 distinct values
ratingINTEGER (1-5)5 (55), 4 (44), 1 (35), 2 (33), 3 (25)
commentTEXT22 distinct values
reviewed_atTEXT (DATE)2022-02-13 to 2024-12-31
helpful_votesINTEGER0 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;
▶ Run this query in the Playground

employees

Company employees · 120 rows · 12 columns · Open in the Playground

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
ColumnType and keyIn the data
idINTEGER
Primary key
first_nameTEXT51 distinct values
last_nameTEXT46 distinct values
emailTEXTunique on every row
departmentTEXT10 distinct values
department_idINTEGER
References departments.id
10 distinct values
titleTEXT53 distinct values
salaryREAL46,044 to 204,449
hire_dateTEXT (DATE)2015-01-14 to 2024-10-26
manager_idINTEGER (self-ref, nullable)
References employees.id (by convention, not a declared constraint)
10 distinct values; NULL in 10 rows
cityTEXT20 distinct values
statusTEXT ('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;
▶ Run this query in the Playground

departments

Company departments · 10 rows · 5 columns · Open in the Playground

The ten departments, with a location, the number of people and an annual budget.

Joins: employees.department_id points at departments.id.

Columns of the departments table
ColumnType and keyIn the data
idINTEGER
Primary key
nameTEXTunique on every row
locationTEXT9 distinct values
headcountINTEGER10 (2), 11 (2), 13 (2), 9 (1), 12 (1), 15 (1), 16 (1)
budgetREAL2,082,000 to 4,149,000

Things to know

  • 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;
▶ Run this query in the Playground