SQL practice › Sample database
SQL sample database for practice: download it, or query it right here
A database small enough to read and large enough that you can't answer by looking: an online shop with 40 customers, 13 products, 150 orders placed during 2025 and 315 order lines. Four tables, linked the usual way, so it covers joins, GROUP BY, subqueries and date filters. All of it is made up.
Download it, or use it without installing anything: every query box on this page runs on this same database in your browser.
Free, no sign-up. The .sql script creates the tables and inserts every row; the .sqlite file is the same database, ready to open; the CSV files are one per table. Free to use in classes, tutorials and your own projects under CC BY 4.0: credit SheetDrill with a link to this page. Skip to the 8 exercises ↓
The four tables
One customer places many orders; one order has one or more lines in order_items; each line points at a product. Prices are whole dollars, and order_items.unit_price is the price the product was sold at.
orders.customer_id → customers.idorder_items.order_id → orders.idorder_items.product_id → products.id
Three things are in the data on purpose: two customers never ordered, one product was never sold, and some orders were cancelled or returned (orders.status). They are what makes a LEFT JOIN or a status filter change the answer.
customers · first 5 of 40 rows
| id | name | city | state | segment | signup_date |
| 1 | Nils Engel | Seattle | WA | Consumer | 2023-10-07 |
| 2 | Pablo Voss | Denver | CO | Business | 2023-12-22 |
| 3 | Felix Solberg | Seattle | WA | Business | 2024-07-27 |
| 4 | Rhea Reyes | Austin | TX | Business | 2024-07-18 |
| 5 | Rhea Ostrander | Chicago | IL | Consumer | 2024-03-17 |
products · first 5 of 13 rows
| id | name | category | price |
| 1 | Desk Chair | Furniture | 189 |
| 2 | Standing Desk | Furniture | 449 |
| 3 | Bookcase | Furniture | 129 |
| 4 | Filing Cabinet | Furniture | 159 |
| 5 | Monitor | Technology | 229 |
orders · first 5 of 150 rows
| id | customer_id | order_date | status |
| 1001 | 4 | 2025-01-05 | Shipped |
| 1002 | 10 | 2025-01-07 | Delivered |
| 1003 | 19 | 2025-01-08 | Delivered |
| 1004 | 27 | 2025-01-12 | Delivered |
| 1005 | 37 | 2025-01-13 | Returned |
order_items · first 5 of 315 rows
| id | order_id | product_id | quantity | unit_price |
| 1 | 1001 | 10 | 10 | 12 |
| 2 | 1002 | 7 | 3 | 25 |
| 3 | 1002 | 8 | 2 | 69 |
| 4 | 1003 | 12 | 8 | 9 |
| 5 | 1003 | 9 | 6 | 42 |
Live queryLook at a table
Tables: customers, products, orders, order_items
customers · first 5 of 40 rows
| id | name | city | state | segment | signup_date |
| 1 | Nils Engel | Seattle | WA | Consumer | 2023-10-07 |
| 2 | Pablo Voss | Denver | CO | Business | 2023-12-22 |
| 3 | Felix Solberg | Seattle | WA | Business | 2024-07-27 |
| 4 | Rhea Reyes | Austin | TX | Business | 2024-07-18 |
| 5 | Rhea Ostrander | Chicago | IL | Consumer | 2024-03-17 |
products · first 5 of 13 rows
| id | name | category | price |
| 1 | Desk Chair | Furniture | 189 |
| 2 | Standing Desk | Furniture | 449 |
| 3 | Bookcase | Furniture | 129 |
| 4 | Filing Cabinet | Furniture | 159 |
| 5 | Monitor | Technology | 229 |
orders · first 5 of 150 rows
| id | customer_id | order_date | status |
| 1001 | 4 | 2025-01-05 | Shipped |
| 1002 | 10 | 2025-01-07 | Delivered |
| 1003 | 19 | 2025-01-08 | Delivered |
| 1004 | 27 | 2025-01-12 | Delivered |
| 1005 | 37 | 2025-01-13 | Returned |
order_items · first 5 of 315 rows
| id | order_id | product_id | quantity | unit_price |
| 1 | 1001 | 10 | 10 | 12 |
| 2 | 1002 | 7 | 3 | 25 |
| 3 | 1002 | 8 | 2 | 69 |
| 4 | 1003 | 12 | 8 | 9 |
| 5 | 1003 | 9 | 6 | 42 |
Result · 5 rows
| id | name | city | state | segment | signup_date |
| 1 | Nils Engel | Seattle | WA | Consumer | 2023-10-07 |
| 2 | Pablo Voss | Denver | CO | Business | 2023-12-22 |
| 3 | Felix Solberg | Seattle | WA | Business | 2024-07-27 |
| 4 | Rhea Reyes | Austin | TX | Business | 2024-07-18 |
| 5 | Rhea Ostrander | Chicago | IL | Consumer | 2024-03-17 |
Live queryOne order, through all four tables
Tables: customers, products, orders, order_items
customers · first 5 of 40 rows
| id | name | city | state | segment | signup_date |
| 1 | Nils Engel | Seattle | WA | Consumer | 2023-10-07 |
| 2 | Pablo Voss | Denver | CO | Business | 2023-12-22 |
| 3 | Felix Solberg | Seattle | WA | Business | 2024-07-27 |
| 4 | Rhea Reyes | Austin | TX | Business | 2024-07-18 |
| 5 | Rhea Ostrander | Chicago | IL | Consumer | 2024-03-17 |
products · first 5 of 13 rows
| id | name | category | price |
| 1 | Desk Chair | Furniture | 189 |
| 2 | Standing Desk | Furniture | 449 |
| 3 | Bookcase | Furniture | 129 |
| 4 | Filing Cabinet | Furniture | 159 |
| 5 | Monitor | Technology | 229 |
orders · first 5 of 150 rows
| id | customer_id | order_date | status |
| 1001 | 4 | 2025-01-05 | Shipped |
| 1002 | 10 | 2025-01-07 | Delivered |
| 1003 | 19 | 2025-01-08 | Delivered |
| 1004 | 27 | 2025-01-12 | Delivered |
| 1005 | 37 | 2025-01-13 | Returned |
order_items · first 5 of 315 rows
| id | order_id | product_id | quantity | unit_price |
| 1 | 1001 | 10 | 10 | 12 |
| 2 | 1002 | 7 | 3 | 25 |
| 3 | 1002 | 8 | 2 | 69 |
| 4 | 1003 | 12 | 8 | 9 |
| 5 | 1003 | 9 | 6 | 42 |
Result · 3 rows
| id | order_date | name | product | quantity | unit_price |
| 1009 | 2025-01-24 | Viktor Reyes | Bookcase | 4 | 129 |
| 1009 | 2025-01-24 | Viktor Reyes | Gel Pens | 8 | 12 |
| 1009 | 2025-01-24 | Viktor Reyes | Filing Cabinet | 1 | 159 |
Live queryRevenue per category
Tables: customers, products, orders, order_items
customers · first 5 of 40 rows
| id | name | city | state | segment | signup_date |
| 1 | Nils Engel | Seattle | WA | Consumer | 2023-10-07 |
| 2 | Pablo Voss | Denver | CO | Business | 2023-12-22 |
| 3 | Felix Solberg | Seattle | WA | Business | 2024-07-27 |
| 4 | Rhea Reyes | Austin | TX | Business | 2024-07-18 |
| 5 | Rhea Ostrander | Chicago | IL | Consumer | 2024-03-17 |
products · first 5 of 13 rows
| id | name | category | price |
| 1 | Desk Chair | Furniture | 189 |
| 2 | Standing Desk | Furniture | 449 |
| 3 | Bookcase | Furniture | 129 |
| 4 | Filing Cabinet | Furniture | 159 |
| 5 | Monitor | Technology | 229 |
orders · first 5 of 150 rows
| id | customer_id | order_date | status |
| 1001 | 4 | 2025-01-05 | Shipped |
| 1002 | 10 | 2025-01-07 | Delivered |
| 1003 | 19 | 2025-01-08 | Delivered |
| 1004 | 27 | 2025-01-12 | Delivered |
| 1005 | 37 | 2025-01-13 | Returned |
order_items · first 5 of 315 rows
| id | order_id | product_id | quantity | unit_price |
| 1 | 1001 | 10 | 10 | 12 |
| 2 | 1002 | 7 | 3 | 25 |
| 3 | 1002 | 8 | 2 | 69 |
| 4 | 1003 | 12 | 8 | 9 |
| 5 | 1003 | 9 | 6 | 42 |
Result · 3 rows
| category | revenue |
| Furniture | 69519 |
| Technology | 20851 |
| Office Supplies | 13573 |
How to load the file
SQLite: open the .sqlite file directly in any SQLite tool, or build it from the script: sqlite3 shop.db < sample-shop-database.sql
PostgreSQL and MySQL: the script is written to load there as well: it uses standard column types, primary keys, REFERENCES and plain INSERT statements, with dates written as year-month-day. It was only tested on SQLite, so if a line fails on your database, that line is the place to look.
Spreadsheets and other tools: use the CSV files, one per table.
Eight practice questions, easiest first
Each one has the answer folded under it.
Write the query, press Run. It is checked against the result of a reference query, on this data and on changed data, so typing the values in doesn't pass. Column names don't matter.
Exercise 1Filter and sort
List the name and city of the Business customers in Denver, in alphabetical order of name.
Table: customers
customers · first 5 of 40 rows
| id | name | city | state | segment | signup_date |
| 1 | Nils Engel | Seattle | WA | Consumer | 2023-10-07 |
| 2 | Pablo Voss | Denver | CO | Business | 2023-12-22 |
| 3 | Felix Solberg | Seattle | WA | Business | 2024-07-27 |
| 4 | Rhea Reyes | Austin | TX | Business | 2024-07-18 |
| 5 | Rhea Ostrander | Chicago | IL | Consumer | 2024-03-17 |
Show the answer
SELECT name, city
FROM customers
WHERE segment = 'Business' AND city = 'Denver'
ORDER BY name
Result · 2 rows
| name | city |
| Greta Lombard | Denver |
| Pablo Voss | Denver |
Two conditions joined with AND, both on customers, then ORDER BY. Text values go in single quotes.
Exercise 2Count per group
How many orders are there for each status? Return the status and the number of orders.
Table: orders
orders · first 5 of 150 rows
| id | customer_id | order_date | status |
| 1001 | 4 | 2025-01-05 | Shipped |
| 1002 | 10 | 2025-01-07 | Delivered |
| 1003 | 19 | 2025-01-08 | Delivered |
| 1004 | 27 | 2025-01-12 | Delivered |
| 1005 | 37 | 2025-01-13 | Returned |
Show the answer
SELECT status, COUNT(*)
FROM orders
GROUP BY status
Result · 4 rows
| status | COUNT(*) |
| Cancelled | 10 |
| Delivered | 109 |
| Returned | 14 |
| Shipped | 17 |
GROUP BY makes one row per status and COUNT(*) counts the orders in each.
Exercise 3A join with a total
For each customer in Austin who has ordered, return the name and the total amount of their orders (quantity × unit price, every order counted).
Tables: customers, products, orders, order_items
customers · first 5 of 40 rows
| id | name | city | state | segment | signup_date |
| 1 | Nils Engel | Seattle | WA | Consumer | 2023-10-07 |
| 2 | Pablo Voss | Denver | CO | Business | 2023-12-22 |
| 3 | Felix Solberg | Seattle | WA | Business | 2024-07-27 |
| 4 | Rhea Reyes | Austin | TX | Business | 2024-07-18 |
| 5 | Rhea Ostrander | Chicago | IL | Consumer | 2024-03-17 |
products · first 5 of 13 rows
| id | name | category | price |
| 1 | Desk Chair | Furniture | 189 |
| 2 | Standing Desk | Furniture | 449 |
| 3 | Bookcase | Furniture | 129 |
| 4 | Filing Cabinet | Furniture | 159 |
| 5 | Monitor | Technology | 229 |
orders · first 5 of 150 rows
| id | customer_id | order_date | status |
| 1001 | 4 | 2025-01-05 | Shipped |
| 1002 | 10 | 2025-01-07 | Delivered |
| 1003 | 19 | 2025-01-08 | Delivered |
| 1004 | 27 | 2025-01-12 | Delivered |
| 1005 | 37 | 2025-01-13 | Returned |
order_items · first 5 of 315 rows
| id | order_id | product_id | quantity | unit_price |
| 1 | 1001 | 10 | 10 | 12 |
| 2 | 1002 | 7 | 3 | 25 |
| 3 | 1002 | 8 | 2 | 69 |
| 4 | 1003 | 12 | 8 | 9 |
| 5 | 1003 | 9 | 6 | 42 |
Show the answer
SELECT c.name, SUM(oi.quantity * oi.unit_price) AS total
FROM customers c
JOIN orders o ON o.customer_id = c.id
JOIN order_items oi ON oi.order_id = o.id
WHERE c.city = 'Austin'
GROUP BY c.id, c.name
Result · 5 rows
| name | total |
| Rhea Reyes | 3053 |
| Edith Halvorsen | 5175 |
| Rhea Jovanic | 3161 |
| Jonas Reyes | 702 |
| Stefan Corvin | 2968 |
The amounts live in order_items, the city in customers, and orders is the table that links them: two joins. Each line is worth quantity × unit_price; SUM adds a customer's lines.
Exercise 4Rows with no match
Which customers have never placed an order? Return their names.
Tables: customers, orders
customers · first 5 of 40 rows
| id | name | city | state | segment | signup_date |
| 1 | Nils Engel | Seattle | WA | Consumer | 2023-10-07 |
| 2 | Pablo Voss | Denver | CO | Business | 2023-12-22 |
| 3 | Felix Solberg | Seattle | WA | Business | 2024-07-27 |
| 4 | Rhea Reyes | Austin | TX | Business | 2024-07-18 |
| 5 | Rhea Ostrander | Chicago | IL | Consumer | 2024-03-17 |
orders · first 5 of 150 rows
| id | customer_id | order_date | status |
| 1001 | 4 | 2025-01-05 | Shipped |
| 1002 | 10 | 2025-01-07 | Delivered |
| 1003 | 19 | 2025-01-08 | Delivered |
| 1004 | 27 | 2025-01-12 | Delivered |
| 1005 | 37 | 2025-01-13 | Returned |
Show the answer
SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL
Result · 2 rows
| name |
| Mira Moreau |
| Bruno Reyes |
LEFT JOIN keeps every customer; where there is no order, the order's columns are NULL, and WHERE o.id IS NULL keeps exactly those. WHERE id NOT IN (SELECT customer_id FROM orders) gives the same names.
Exercise 5Filter the groups
Which products sold more than 80 units in total? Return the product name and its units.
Tables: products, order_items
products · first 5 of 13 rows
| id | name | category | price |
| 1 | Desk Chair | Furniture | 189 |
| 2 | Standing Desk | Furniture | 449 |
| 3 | Bookcase | Furniture | 129 |
| 4 | Filing Cabinet | Furniture | 159 |
| 5 | Monitor | Technology | 229 |
order_items · first 5 of 315 rows
| id | order_id | product_id | quantity | unit_price |
| 1 | 1001 | 10 | 10 | 12 |
| 2 | 1002 | 7 | 3 | 25 |
| 3 | 1002 | 8 | 2 | 69 |
| 4 | 1003 | 12 | 8 | 9 |
| 5 | 1003 | 9 | 6 | 42 |
Show the answer
SELECT p.name, SUM(oi.quantity) AS units
FROM products p
JOIN order_items oi ON oi.product_id = p.id
GROUP BY p.id, p.name
HAVING SUM(oi.quantity) > 80
Result · 5 rows
| name | units |
| Standing Desk | 87 |
| Copy Paper | 177 |
| Gel Pens | 208 |
| Binders | 115 |
| Sticky Notes | 162 |
A condition on a total can't go in WHERE, which looks at one row at a time, before the groups exist. HAVING filters the groups after SUM has run.
Exercise 6A date range and a status
What was the revenue (quantity × unit price) of the orders placed in March 2025, not counting cancelled orders?
Tables: orders, order_items
orders · first 5 of 150 rows
| id | customer_id | order_date | status |
| 1001 | 4 | 2025-01-05 | Shipped |
| 1002 | 10 | 2025-01-07 | Delivered |
| 1003 | 19 | 2025-01-08 | Delivered |
| 1004 | 27 | 2025-01-12 | Delivered |
| 1005 | 37 | 2025-01-13 | Returned |
order_items · first 5 of 315 rows
| id | order_id | product_id | quantity | unit_price |
| 1 | 1001 | 10 | 10 | 12 |
| 2 | 1002 | 7 | 3 | 25 |
| 3 | 1002 | 8 | 2 | 69 |
| 4 | 1003 | 12 | 8 | 9 |
| 5 | 1003 | 9 | 6 | 42 |
Show the answer
SELECT SUM(oi.quantity * oi.unit_price)
FROM orders o
JOIN order_items oi ON oi.order_id = o.id
WHERE o.order_date >= '2025-03-01' AND o.order_date < '2025-04-01'
AND o.status <> 'Cancelled'
Result · 1 row
| SUM(oi.quantity * oi.unit_price) |
| 6265 |
The date range is "on or after March 1 and before April 1", which works whatever the last day of the month is. The status condition goes in the same WHERE.
Exercise 7Compare with an average
How many orders are worth more than the average order? An order's value is the sum of its lines (quantity × unit price).
Table: order_items
order_items · first 5 of 315 rows
| id | order_id | product_id | quantity | unit_price |
| 1 | 1001 | 10 | 10 | 12 |
| 2 | 1002 | 7 | 3 | 25 |
| 3 | 1002 | 8 | 2 | 69 |
| 4 | 1003 | 12 | 8 | 9 |
| 5 | 1003 | 9 | 6 | 42 |
Show the answer
WITH totals AS (
SELECT order_id, SUM(quantity * unit_price) AS total
FROM order_items
GROUP BY order_id
)
SELECT COUNT(*)
FROM totals
WHERE total > (SELECT AVG(total) FROM totals)
Two steps. First one row per order with its total: that is the CTE. Then compare each total with the average of those totals. Averaging the lines instead of the orders answers a different question.
Exercise 8Top three
Who are the three customers who spent the most? Return name and total (quantity × unit price, every order counted), highest first.
Tables: customers, products, orders, order_items
customers · first 5 of 40 rows
| id | name | city | state | segment | signup_date |
| 1 | Nils Engel | Seattle | WA | Consumer | 2023-10-07 |
| 2 | Pablo Voss | Denver | CO | Business | 2023-12-22 |
| 3 | Felix Solberg | Seattle | WA | Business | 2024-07-27 |
| 4 | Rhea Reyes | Austin | TX | Business | 2024-07-18 |
| 5 | Rhea Ostrander | Chicago | IL | Consumer | 2024-03-17 |
products · first 5 of 13 rows
| id | name | category | price |
| 1 | Desk Chair | Furniture | 189 |
| 2 | Standing Desk | Furniture | 449 |
| 3 | Bookcase | Furniture | 129 |
| 4 | Filing Cabinet | Furniture | 159 |
| 5 | Monitor | Technology | 229 |
orders · first 5 of 150 rows
| id | customer_id | order_date | status |
| 1001 | 4 | 2025-01-05 | Shipped |
| 1002 | 10 | 2025-01-07 | Delivered |
| 1003 | 19 | 2025-01-08 | Delivered |
| 1004 | 27 | 2025-01-12 | Delivered |
| 1005 | 37 | 2025-01-13 | Returned |
order_items · first 5 of 315 rows
| id | order_id | product_id | quantity | unit_price |
| 1 | 1001 | 10 | 10 | 12 |
| 2 | 1002 | 7 | 3 | 25 |
| 3 | 1002 | 8 | 2 | 69 |
| 4 | 1003 | 12 | 8 | 9 |
| 5 | 1003 | 9 | 6 | 42 |
Show the answer
SELECT c.name, SUM(oi.quantity * oi.unit_price) AS total
FROM customers c
JOIN orders o ON o.customer_id = c.id
JOIN order_items oi ON oi.order_id = o.id
GROUP BY c.id, c.name
ORDER BY total DESC
LIMIT 3
Result · 3 rows
| name | total |
| Dario Fontaine | 7026 |
| Alma Bianchi | 6824 |
| Pablo Petrov | 5801 |
The same two joins and SUM as before, for every customer, then sorted from the largest total down and cut at three rows. LIMIT is SQLite, PostgreSQL and MySQL syntax.
Questions people ask
Where can I get a sample database to practice SQL?
This page has one: four tables, 518 rows in all, free to download as a .sql script, a SQLite file or CSV. It needs no account.
Can I practice SQL on a sample database online, without installing anything?
Yes. The query boxes on this page run on the same database in your browser, on SQLite. Nothing is installed and nothing you type is sent anywhere.
Does it work in MySQL, PostgreSQL or SQL Server?
The script was tested on SQLite only. It is written with standard column types and plain INSERT statements so that it loads in PostgreSQL and MySQL; SQL Server was not considered. The practice queries use standard SQL, except LIMIT in the last one.
Is the data real?
No. Names, cities, orders and prices are generated. No real person or company is in it.
Keep going
Ten questions on a real database, from SELECT to JOINs and subqueries. Free, no sign-up.
Written and tested by Nico, who builds SheetDrill (about). Every query on this page was run on SQLite, and every exercise checked against right and wrong answers, before the page was published. Other databases differ in places; where they do, the page says so. Last checked November 2026.