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.

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
idnamecitystatesegmentsignup_date
1Nils EngelSeattleWAConsumer2023-10-07
2Pablo VossDenverCOBusiness2023-12-22
3Felix SolbergSeattleWABusiness2024-07-27
4Rhea ReyesAustinTXBusiness2024-07-18
5Rhea OstranderChicagoILConsumer2024-03-17
products · first 5 of 13 rows
idnamecategoryprice
1Desk ChairFurniture189
2Standing DeskFurniture449
3BookcaseFurniture129
4Filing CabinetFurniture159
5MonitorTechnology229
orders · first 5 of 150 rows
idcustomer_idorder_datestatus
100142025-01-05Shipped
1002102025-01-07Delivered
1003192025-01-08Delivered
1004272025-01-12Delivered
1005372025-01-13Returned
order_items · first 5 of 315 rows
idorder_idproduct_idquantityunit_price
11001101012
210027325
310028269
410031289
510039642
Live queryLook at a table
Tables: customers, products, orders, order_items
customers · first 5 of 40 rows
idnamecitystatesegmentsignup_date
1Nils EngelSeattleWAConsumer2023-10-07
2Pablo VossDenverCOBusiness2023-12-22
3Felix SolbergSeattleWABusiness2024-07-27
4Rhea ReyesAustinTXBusiness2024-07-18
5Rhea OstranderChicagoILConsumer2024-03-17
products · first 5 of 13 rows
idnamecategoryprice
1Desk ChairFurniture189
2Standing DeskFurniture449
3BookcaseFurniture129
4Filing CabinetFurniture159
5MonitorTechnology229
orders · first 5 of 150 rows
idcustomer_idorder_datestatus
100142025-01-05Shipped
1002102025-01-07Delivered
1003192025-01-08Delivered
1004272025-01-12Delivered
1005372025-01-13Returned
order_items · first 5 of 315 rows
idorder_idproduct_idquantityunit_price
11001101012
210027325
310028269
410031289
510039642
Ctrl/⌘ + Enter to run
Result · 5 rows
idnamecitystatesegmentsignup_date
1Nils EngelSeattleWAConsumer2023-10-07
2Pablo VossDenverCOBusiness2023-12-22
3Felix SolbergSeattleWABusiness2024-07-27
4Rhea ReyesAustinTXBusiness2024-07-18
5Rhea OstranderChicagoILConsumer2024-03-17
Live queryOne order, through all four tables
Tables: customers, products, orders, order_items
customers · first 5 of 40 rows
idnamecitystatesegmentsignup_date
1Nils EngelSeattleWAConsumer2023-10-07
2Pablo VossDenverCOBusiness2023-12-22
3Felix SolbergSeattleWABusiness2024-07-27
4Rhea ReyesAustinTXBusiness2024-07-18
5Rhea OstranderChicagoILConsumer2024-03-17
products · first 5 of 13 rows
idnamecategoryprice
1Desk ChairFurniture189
2Standing DeskFurniture449
3BookcaseFurniture129
4Filing CabinetFurniture159
5MonitorTechnology229
orders · first 5 of 150 rows
idcustomer_idorder_datestatus
100142025-01-05Shipped
1002102025-01-07Delivered
1003192025-01-08Delivered
1004272025-01-12Delivered
1005372025-01-13Returned
order_items · first 5 of 315 rows
idorder_idproduct_idquantityunit_price
11001101012
210027325
310028269
410031289
510039642
Ctrl/⌘ + Enter to run
Result · 3 rows
idorder_datenameproductquantityunit_price
10092025-01-24Viktor ReyesBookcase4129
10092025-01-24Viktor ReyesGel Pens812
10092025-01-24Viktor ReyesFiling Cabinet1159
Live queryRevenue per category
Tables: customers, products, orders, order_items
customers · first 5 of 40 rows
idnamecitystatesegmentsignup_date
1Nils EngelSeattleWAConsumer2023-10-07
2Pablo VossDenverCOBusiness2023-12-22
3Felix SolbergSeattleWABusiness2024-07-27
4Rhea ReyesAustinTXBusiness2024-07-18
5Rhea OstranderChicagoILConsumer2024-03-17
products · first 5 of 13 rows
idnamecategoryprice
1Desk ChairFurniture189
2Standing DeskFurniture449
3BookcaseFurniture129
4Filing CabinetFurniture159
5MonitorTechnology229
orders · first 5 of 150 rows
idcustomer_idorder_datestatus
100142025-01-05Shipped
1002102025-01-07Delivered
1003192025-01-08Delivered
1004272025-01-12Delivered
1005372025-01-13Returned
order_items · first 5 of 315 rows
idorder_idproduct_idquantityunit_price
11001101012
210027325
310028269
410031289
510039642
Ctrl/⌘ + Enter to run
Result · 3 rows
categoryrevenue
Furniture69519
Technology20851
Office Supplies13573

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
idnamecitystatesegmentsignup_date
1Nils EngelSeattleWAConsumer2023-10-07
2Pablo VossDenverCOBusiness2023-12-22
3Felix SolbergSeattleWABusiness2024-07-27
4Rhea ReyesAustinTXBusiness2024-07-18
5Rhea OstranderChicagoILConsumer2024-03-17
Ctrl/⌘ + Enter to run
Show the answer
SELECT name, city
FROM customers
WHERE segment = 'Business' AND city = 'Denver'
ORDER BY name
Result · 2 rows
namecity
Greta LombardDenver
Pablo VossDenver

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
idcustomer_idorder_datestatus
100142025-01-05Shipped
1002102025-01-07Delivered
1003192025-01-08Delivered
1004272025-01-12Delivered
1005372025-01-13Returned
Ctrl/⌘ + Enter to run
Show the answer
SELECT status, COUNT(*)
FROM orders
GROUP BY status
Result · 4 rows
statusCOUNT(*)
Cancelled10
Delivered109
Returned14
Shipped17

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
idnamecitystatesegmentsignup_date
1Nils EngelSeattleWAConsumer2023-10-07
2Pablo VossDenverCOBusiness2023-12-22
3Felix SolbergSeattleWABusiness2024-07-27
4Rhea ReyesAustinTXBusiness2024-07-18
5Rhea OstranderChicagoILConsumer2024-03-17
products · first 5 of 13 rows
idnamecategoryprice
1Desk ChairFurniture189
2Standing DeskFurniture449
3BookcaseFurniture129
4Filing CabinetFurniture159
5MonitorTechnology229
orders · first 5 of 150 rows
idcustomer_idorder_datestatus
100142025-01-05Shipped
1002102025-01-07Delivered
1003192025-01-08Delivered
1004272025-01-12Delivered
1005372025-01-13Returned
order_items · first 5 of 315 rows
idorder_idproduct_idquantityunit_price
11001101012
210027325
310028269
410031289
510039642
Ctrl/⌘ + Enter to run
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
nametotal
Rhea Reyes3053
Edith Halvorsen5175
Rhea Jovanic3161
Jonas Reyes702
Stefan Corvin2968

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
idnamecitystatesegmentsignup_date
1Nils EngelSeattleWAConsumer2023-10-07
2Pablo VossDenverCOBusiness2023-12-22
3Felix SolbergSeattleWABusiness2024-07-27
4Rhea ReyesAustinTXBusiness2024-07-18
5Rhea OstranderChicagoILConsumer2024-03-17
orders · first 5 of 150 rows
idcustomer_idorder_datestatus
100142025-01-05Shipped
1002102025-01-07Delivered
1003192025-01-08Delivered
1004272025-01-12Delivered
1005372025-01-13Returned
Ctrl/⌘ + Enter to run
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
idnamecategoryprice
1Desk ChairFurniture189
2Standing DeskFurniture449
3BookcaseFurniture129
4Filing CabinetFurniture159
5MonitorTechnology229
order_items · first 5 of 315 rows
idorder_idproduct_idquantityunit_price
11001101012
210027325
310028269
410031289
510039642
Ctrl/⌘ + Enter to run
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
nameunits
Standing Desk87
Copy Paper177
Gel Pens208
Binders115
Sticky Notes162

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
idcustomer_idorder_datestatus
100142025-01-05Shipped
1002102025-01-07Delivered
1003192025-01-08Delivered
1004272025-01-12Delivered
1005372025-01-13Returned
order_items · first 5 of 315 rows
idorder_idproduct_idquantityunit_price
11001101012
210027325
310028269
410031289
510039642
Ctrl/⌘ + Enter to run
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
idorder_idproduct_idquantityunit_price
11001101012
210027325
310028269
410031289
510039642
Ctrl/⌘ + Enter to run
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)
Result · 1 row
COUNT(*)
55

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
idnamecitystatesegmentsignup_date
1Nils EngelSeattleWAConsumer2023-10-07
2Pablo VossDenverCOBusiness2023-12-22
3Felix SolbergSeattleWABusiness2024-07-27
4Rhea ReyesAustinTXBusiness2024-07-18
5Rhea OstranderChicagoILConsumer2024-03-17
products · first 5 of 13 rows
idnamecategoryprice
1Desk ChairFurniture189
2Standing DeskFurniture449
3BookcaseFurniture129
4Filing CabinetFurniture159
5MonitorTechnology229
orders · first 5 of 150 rows
idcustomer_idorder_datestatus
100142025-01-05Shipped
1002102025-01-07Delivered
1003192025-01-08Delivered
1004272025-01-12Delivered
1005372025-01-13Returned
order_items · first 5 of 315 rows
idorder_idproduct_idquantityunit_price
11001101012
210027325
310028269
410031289
510039642
Ctrl/⌘ + Enter to run
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
nametotal
Dario Fontaine7026
Alma Bianchi6824
Pablo Petrov5801

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.

More to practice on small tables

If one of these was hard, the page for that topic explains it on a table of a few rows, with more exercises: joins, GROUP BY, WHERE vs HAVING, subqueries and CTEs. For a job interview, see the SQL interview questions with answers.

Working in a spreadsheet instead? There is sample data for Excel too.

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.

Related

SQL JOIN: INNER and LEFT, with live queriesSQL GROUP BY, with live queriesSQL WHERE vs HAVING, with live queriesSQL subquery vs CTE: the same question three waysSQL interview questions, with answersSQL JOIN practice exercisesSQL practice online

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.