SQL practice questions with answers
GROUP BY, JOIN, LEFT JOIN and filters on a four-table store database. Updated 3 November 2026.
Steps
- Download store-database.sql.
- Load it: sqlite3 store.db < store-database.sql (or run it in pgAdmin, MySQL Workbench or DBeaver).
- Write each query, then open the answer to compare your result and our SQL.
Questions
try first, then open the answer
Revenue by product category, excluding cancelled and returned orders?
Technology $537,612.64; Furniture $200,325.49; Office Supplies $67,416.80
SQL
SELECT p.category, ROUND(SUM(i.quantity*i.unit_price),2) AS revenue
FROM order_items i
JOIN orders o ON o.order_id = i.order_id
JOIN products p ON p.product_id = i.product_id
WHERE o.status NOT IN ('Cancelled','Returned')
GROUP BY p.category
ORDER BY revenue DESCFile: store-database
Top 5 customers by revenue (excluding cancelled and returned orders)?
Steven Lewis (CU10118) $21,983.66; Patricia Khan (CU10105) $15,506.69; Elizabeth Flores (CU10237) $14,375.98; Mei Nelson (CU10039) $14,273.55; Carol Clark (CU10292) $14,080.03
SQL
SELECT c.customer_id, c.first_name || ' ' || c.last_name AS name, ROUND(SUM(i.quantity*i.unit_price),2) AS revenue
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
JOIN order_items i ON i.order_id = o.order_id
WHERE o.status NOT IN ('Cancelled','Returned')
GROUP BY c.customer_id, name
ORDER BY revenue DESC
LIMIT 5File: store-database
How many customers have never placed an order?
8 customers
SQL
SELECT COUNT(*)
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULLFile: store-database
Orders by status?
Delivered 804; Shipped 66; Cancelled 51; Processing 47; Returned 32
SQL
SELECT status, COUNT(*) FROM orders GROUP BY status ORDER BY COUNT(*) DESCFile: store-database
What is the list price of product P0012?
Corner Bookcase, $188.81
SQL
SELECT product_name, unit_price FROM products WHERE product_id = 'P0012'File: store-database
Which region has the highest sales?
South, $299,380.41 (Midwest $234,801.89, West $217,262.68, Northeast $204,168.78)
SQL
SELECT region, ROUND(SUM(sales),2) AS total FROM sales_orders GROUP BY region ORDER BY total DESC, regionFile: sales-orders-1000
Profit margin (profit / sales) by category?
Office Supplies 26.98%; Technology 22.32%; Furniture 21.42%
SQL
SELECT category, ROUND(SUM(profit)/SUM(sales)*100,2) AS margin_pct FROM sales_orders GROUP BY category ORDER BY margin_pct DESC, categoryFile: sales-orders-1000
Average wait (seconds) and resolution rate by queue?
Billing 100.8 s, 86.2% resolved; Returns 103.3 s, 87.6% resolved; Sales 100.7 s, 85.7% resolved; Technical 112.8 s, 67.9% resolved
SQL
SELECT queue, ROUND(AVG(wait_seconds),1), ROUND(100.0*SUM(resolved)/COUNT(*),1) FROM call_center GROUP BY queue ORDER BY queueFile: call-center-1000