The fastest way to get good at SQL is to solve real questions on real data. These 24 practice problems use one small online-store database and go from simple filters to window functions, with a tested solution and the expected result for every problem, so you can check your answer.

The problems follow the three levels of the Learn SQL series. If one stumps you, each solution links to the lesson that explains the idea. Try every problem on your own first, then open the solution and compare.

How to Use These Problems

  1. Set up the database. Download shop.sql and load it into any database, as shown in Part 1. It has three tables: customers (8 rows), products (6 rows), and orders (12 rows). Revenue is always orders.quantity * products.price.
  2. Write your query before opening the solution. Getting stuck for a few minutes is part of learning.
  3. Compare results, not just code. There’s usually more than one correct query. If your result matches, your answer is right.
  4. Read the note under each solution. It explains the key idea or a database difference.

Every solution was tested on PostgreSQL, MySQL 8, SQL Server, and SQLite and returns the same rows in all four. Results are shown as PostgreSQL prints them; other databases may show 0 as 0.00 or NULL as NULL.

Level 1: Foundations

Filtering, sorting, GROUP BY, and joins.

1. Electronics, Cheapest First

List the name and price of every product in the Electronics category, from cheapest to most expensive.

Show solution
SELECT name, price
FROM products
WHERE category = 'Electronics'
ORDER BY price;
  name   | price
---------+--------
 Monitor | 249.00
 Laptop  | 899.00

WHERE filters the rows and ORDER BY sorts them; ascending is the default.

2. Early California Customers

Find the customers from California (CA) who signed up before May 1, 2025. Show their names and signup date, oldest first.

Show solution
SELECT first_name, last_name, signup_date
FROM customers
WHERE state = 'CA'
  AND signup_date < '2025-05-01'
ORDER BY signup_date;
 first_name | last_name | signup_date
------------+-----------+-------------
 Maria      | Lopez     | 2025-01-15
 James      | Carter    | 2025-02-03
 Ana        | Martinez  | 2025-04-12

Combine conditions with AND. Dates written as 'YYYY-MM-DD' compare correctly in every database.

3. Missing Emails

Which customers don’t have an email address?

Show solution
SELECT first_name, last_name
FROM customers
WHERE email IS NULL;
 first_name | last_name
------------+-----------
 Daniel     | Brooks

Use IS NULL, never = NULL, which is never true. See the NULL values lesson.

4. Orders by Status

How many orders are there for each status? Show the most common status first.

Show solution
SELECT status, COUNT(*) AS orders
FROM orders
GROUP BY status
ORDER BY orders DESC, status;
  status   | orders
-----------+--------
 shipped   |      8
 pending   |      2
 cancelled |      1
 returned  |      1

GROUP BY status makes one row per status, and COUNT(*) counts the rows in each group.

5. Total Shipped Revenue

What is the total revenue from shipped orders? (Revenue is quantity times price.)

Show solution
SELECT SUM(o.quantity * p.price) AS shipped_revenue
FROM orders o
JOIN products p ON p.product_id = o.product_id
WHERE o.status = 'shipped';
 shipped_revenue
-----------------
         4649.00

The price lives in products, so join it first; then SUM the product of the two columns.

6. August Orders with Names

List every order placed in August 2025 with the customer’s name and the product name, in date order.

Show solution
SELECT o.order_id, o.order_date, c.first_name, c.last_name, p.name AS product, o.quantity
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
JOIN products p  ON p.product_id = o.product_id
WHERE o.order_date >= '2025-08-01' AND o.order_date < '2025-09-01'
ORDER BY o.order_date;
 order_id | order_date | first_name | last_name | product  | quantity
----------+------------+------------+-----------+----------+----------
      101 | 2025-08-02 | Maria      | Lopez     | Laptop   |        1
      102 | 2025-08-05 | James      | Carter    | Mouse    |        3
      103 | 2025-08-11 | Maria      | Lopez     | Keyboard |        2
      104 | 2025-08-19 | Sofia      | Nguyen    | Desk     |        1

Two joins bring in the names. The date range uses >= the first day and < the first day of the next month.

7. Average Price by Category

For each category, show how many products it has and the average price rounded to 2 decimals, most expensive category first.

Show solution
SELECT category,
       COUNT(*) AS products,
       ROUND(AVG(price), 2) AS avg_price
FROM products
GROUP BY category
ORDER BY avg_price DESC;
  category   | products | avg_price
-------------+----------+-----------
 Electronics |        2 |    574.00
 Furniture   |        2 |    250.00
 Accessories |        2 |     35.00

You can sort by a column alias like avg_price in every database.

8. Customers Who Never Ordered

Which customers have never placed an order?

Show solution
SELECT c.first_name, c.last_name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;
 first_name | last_name
------------+-----------
 Laura      | Chen

A LEFT JOIN keeps every customer; customers without orders get NULL in the order columns, which WHERE o.order_id IS NULL finds.

Level 2: Intermediate Queries

CASE, subqueries, CTEs, COALESCE, and your first window functions.

9. Price Bands

Label each product as Budget (under $50), Mid-range (under $300), or Premium, and count the products in each band.

Show solution
SELECT CASE
           WHEN price < 50  THEN 'Budget'
           WHEN price < 300 THEN 'Mid-range'
           ELSE 'Premium'
       END AS price_band,
       COUNT(*) AS products
FROM products
GROUP BY CASE
             WHEN price < 50  THEN 'Budget'
             WHEN price < 300 THEN 'Mid-range'
             ELSE 'Premium'
         END
ORDER BY products DESC, price_band;
 price_band | products
------------+----------
 Budget     |        2
 Mid-range  |        2
 Premium    |        2

SQL Server doesn’t let you GROUP BY an alias, so the CASE is repeated in GROUP BY. The other databases accept GROUP BY price_band. See CASE expressions.

10. Pricier Than Their Category

Which products cost more than the average price of their own category?

Show solution
SELECT p.name, p.category, p.price
FROM products p
WHERE p.price > (
    SELECT AVG(p2.price)
    FROM products p2
    WHERE p2.category = p.category
)
ORDER BY p.category;
   name   |  category   | price
----------+-------------+--------
 Keyboard | Accessories |  45.00
 Laptop   | Electronics | 899.00
 Desk     | Furniture   | 320.00

A correlated subquery runs once per product and compares it with its own category. See subqueries.

11. Customers per City

Count the customers in each city, showing Unknown for customers without a city.

Show solution
SELECT COALESCE(city, 'Unknown') AS city,
       COUNT(*) AS customers
FROM customers
GROUP BY COALESCE(city, 'Unknown')
ORDER BY customers DESC, city;
   city    | customers
-----------+-----------
 El Centro |         2
 San Diego |         2
 Las Vegas |         1
 Phoenix   |         1
 Unknown   |         1
 Yuma      |         1

COALESCE replaces NULL with a value. Without it, the NULL city still forms its own group, but with an empty label.

12. Electronics and Furniture Buyers

Which customers have ordered from both the Electronics and Furniture categories?

Show solution
SELECT c.first_name, c.last_name
FROM customers c
JOIN orders o   ON o.customer_id = c.customer_id
JOIN products p ON p.product_id = o.product_id
WHERE p.category IN ('Electronics', 'Furniture')
GROUP BY c.customer_id, c.first_name, c.last_name
HAVING COUNT(DISTINCT p.category) = 2
ORDER BY c.last_name;
 first_name | last_name
------------+-----------
 Maria      | Lopez
 Ana        | Martinez
 Sofia      | Nguyen

Filter to the two categories, then keep customers with two distinct categories. INTERSECT is another way, as shown in UNION, INTERSECT, and EXCEPT.

13. Best Seller in Each Category

For each category, find the product with the most shipped revenue.

Show solution
WITH product_revenue AS (
    SELECT p.category, p.name,
           SUM(o.quantity * p.price) AS revenue
    FROM orders o
    JOIN products p ON p.product_id = o.product_id
    WHERE o.status = 'shipped'
    GROUP BY p.category, p.name
),
ranked AS (
    SELECT category, name, revenue,
           ROW_NUMBER() OVER (PARTITION BY category ORDER BY revenue DESC) AS rn
    FROM product_revenue
)
SELECT category, name, revenue
FROM ranked
WHERE rn = 1
ORDER BY revenue DESC;
  category   |     name     | revenue
-------------+--------------+---------
 Electronics | Laptop       | 2697.00
 Furniture   | Office Chair |  720.00
 Accessories | Keyboard     |   90.00

Total by product in one CTE, number the products within each category with ROW_NUMBER(), then keep number 1. See CTEs.

14. Running Total of Revenue

Show each shipped order’s value and a running total of revenue in date order.

Show solution
SELECT o.order_date,
       o.order_id,
       o.quantity * p.price AS order_value,
       SUM(o.quantity * p.price) OVER (ORDER BY o.order_date, o.order_id) AS running_total
FROM orders o
JOIN products p ON p.product_id = o.product_id
WHERE o.status = 'shipped'
ORDER BY o.order_date, o.order_id;
 order_date | order_id | order_value | running_total
------------+----------+-------------+---------------
 2025-08-02 |      101 |      899.00 |        899.00
 2025-08-05 |      102 |       75.00 |        974.00
 2025-08-11 |      103 |       90.00 |       1064.00
 2025-08-19 |      104 |      320.00 |       1384.00
 2025-09-01 |      105 |      498.00 |       1882.00
 2025-09-15 |      107 |      720.00 |       2602.00
 2025-10-03 |      109 |     1798.00 |       4400.00
 2025-10-10 |      110 |      249.00 |       4649.00

SUM(...) OVER (ORDER BY ...) keeps every row and adds up as it goes. Adding order_id to the sort makes ties predictable. See window functions.

15. Above-Average Orders

Which shipped orders were worth more than the average shipped order (about $581.13)?

Show solution
SELECT o.order_id, o.quantity * p.price AS order_value
FROM orders o
JOIN products p ON p.product_id = o.product_id
WHERE o.status = 'shipped'
  AND o.quantity * p.price > (
      SELECT AVG(o2.quantity * p2.price)
      FROM orders o2
      JOIN products p2 ON p2.product_id = o2.product_id
      WHERE o2.status = 'shipped'
  )
ORDER BY order_value DESC;
 order_id | order_value
----------+-------------
      109 |     1798.00
      101 |      899.00
      107 |      720.00

The subquery calculates the average once; the outer query compares every order with it.

16. Orders per Month

Count the orders placed each month, and how many of them were shipped.

Show solution
SELECT SUBSTRING(CAST(order_date AS CHAR(10)), 1, 7) AS month,
       COUNT(*) AS orders,
       SUM(CASE WHEN status = 'shipped' THEN 1 ELSE 0 END) AS shipped
FROM orders
GROUP BY SUBSTRING(CAST(order_date AS CHAR(10)), 1, 7)
ORDER BY month;
  month  | orders | shipped
---------+--------+---------
 2025-08 |      4 |       4
 2025-09 |      4 |       2
 2025-10 |      3 |       2
 2025-11 |      1 |       0

SUBSTRING(CAST(order_date AS CHAR(10)), 1, 7) turns a date into YYYY-MM in all four databases. See date and time functions for each database’s own month function.

Level 3: Advanced Queries

Rankings, LAG, EXISTS, percent of total, and putting it all together.

17. Repeat Customers

Find customers with two or more orders, with their first and last order dates.

Show solution
SELECT c.first_name, c.last_name,
       COUNT(*)          AS orders,
       MIN(o.order_date) AS first_order,
       MAX(o.order_date) AS last_order
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.first_name, c.last_name
HAVING COUNT(*) >= 2
ORDER BY orders DESC, c.last_name;
 first_name | last_name | orders | first_order | last_order
------------+-----------+--------+-------------+------------
 Maria      | Lopez     |      3 | 2025-08-02  | 2025-11-02
 James      | Carter    |      2 | 2025-08-05  | 2025-09-07
 Ana        | Martinez  |      2 | 2025-09-01  | 2025-10-21
 Sofia      | Nguyen    |      2 | 2025-08-19  | 2025-10-10

HAVING filters groups after GROUP BY, while WHERE filters rows before it.

18. Rank Customers Within Each State

Rank customers by shipped spending within their state.

Show solution
WITH spend AS (
    SELECT c.state, c.first_name, c.last_name,
           SUM(o.quantity * p.price) AS total_spent
    FROM orders o
    JOIN customers c ON c.customer_id = o.customer_id
    JOIN products p  ON p.product_id = o.product_id
    WHERE o.status = 'shipped'
    GROUP BY c.customer_id, c.state, c.first_name, c.last_name
)
SELECT state, first_name, last_name, total_spent,
       RANK() OVER (PARTITION BY state ORDER BY total_spent DESC) AS state_rank
FROM spend
ORDER BY state, state_rank;
 state | first_name | last_name | total_spent | state_rank
-------+------------+-----------+-------------+------------
 AZ    | Sofia      | Nguyen    |      569.00 |          1
 CA    | Omar       | Haddad    |     1798.00 |          1
 CA    | Maria      | Lopez     |      989.00 |          2
 CA    | Ana        | Martinez  |      498.00 |          3
 CA    | James      | Carter    |       75.00 |          4
 NV    | Kevin      | Patel     |      720.00 |          1

PARTITION BY state restarts the ranking for each state. RANK() gives ties the same number.

19. Lost-Order Rate by Category

For each category, what percentage of orders were returned or cancelled?

Show solution
SELECT p.category,
       COUNT(*) AS orders,
       SUM(CASE WHEN o.status IN ('returned', 'cancelled') THEN 1 ELSE 0 END) AS lost,
       ROUND(100.0 * SUM(CASE WHEN o.status IN ('returned', 'cancelled') THEN 1 ELSE 0 END)
             / COUNT(*), 1) AS lost_pct
FROM orders o
JOIN products p ON p.product_id = o.product_id
GROUP BY p.category
ORDER BY lost_pct DESC;
  category   | orders | lost | lost_pct
-------------+--------+------+----------
 Furniture   |      4 |    1 |     25.0
 Electronics |      5 |    1 |     20.0
 Accessories |      3 |    0 |      0.0

Count the lost orders with SUM(CASE ...) and divide by COUNT(*). Using 100.0 instead of 100 avoids integer division.

20. Month-over-Month Change

For each month, show the number of orders and the change from the previous month.

Show solution
WITH monthly AS (
    SELECT SUBSTRING(CAST(order_date AS CHAR(10)), 1, 7) AS month,
           COUNT(*) AS orders
    FROM orders
    GROUP BY SUBSTRING(CAST(order_date AS CHAR(10)), 1, 7)
)
SELECT month, orders,
       orders - LAG(orders) OVER (ORDER BY month) AS vs_previous
FROM monthly
ORDER BY month;
  month  | orders | vs_previous
---------+--------+-------------
 2025-08 |      4 |
 2025-09 |      4 |           0
 2025-10 |      3 |          -1
 2025-11 |      1 |          -2

LAG() reads the previous row’s value. See window functions part 2.

21. Customers with Only Shipped Orders

Which customers have placed at least one order, and every one of their orders was shipped?

Show solution
SELECT c.first_name, c.last_name
FROM customers c
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id)
  AND NOT EXISTS (
      SELECT 1 FROM orders o
      WHERE o.customer_id = c.customer_id
        AND o.status <> 'shipped'
  )
ORDER BY c.last_name;
 first_name | last_name
------------+-----------
 Omar       | Haddad
 Sofia      | Nguyen
 Kevin      | Patel

EXISTS checks that a customer has orders; NOT EXISTS checks that none of them has another status.

22. Largest Order for Each Product

For each product, show the order (or orders, if tied) with the largest quantity.

Show solution
SELECT o.order_id, p.name AS product, o.quantity
FROM orders o
JOIN products p ON p.product_id = o.product_id
WHERE o.quantity = (
    SELECT MAX(o2.quantity) FROM orders o2 WHERE o2.product_id = o.product_id
)
ORDER BY p.name, o.order_id;
 order_id |   product    | quantity
----------+--------------+----------
      104 | Desk         |        1
      112 | Desk         |        1
      103 | Keyboard     |        2
      109 | Laptop       |        2
      105 | Monitor      |        2
      108 | Mouse        |        5
      107 | Office Chair |        4

Comparing with MAX in a correlated subquery keeps ties, which ROW_NUMBER() would not.

23. Share of Units Sold

For each product, show the shipped units and their share of all shipped units.

Show solution
WITH product_sales AS (
    SELECT p.name, SUM(o.quantity) AS units
    FROM orders o
    JOIN products p ON p.product_id = o.product_id
    WHERE o.status = 'shipped'
    GROUP BY p.name
)
SELECT name, units,
       ROUND(100.0 * units / SUM(units) OVER (), 1) AS pct_of_units
FROM product_sales
ORDER BY units DESC, name;
     name     | units | pct_of_units
--------------+-------+--------------
 Office Chair |     4 |         25.0
 Laptop       |     3 |         18.8
 Monitor      |     3 |         18.8
 Mouse        |     3 |         18.8
 Keyboard     |     2 |         12.5
 Desk         |     1 |          6.3

An empty OVER () makes the window the whole result, so SUM(units) OVER () is the grand total on every row.

24. Customer Summary with Segments

Build one row per customer, including customers with no orders: order count, shipped spending, last order date, and a segment (VIP for $900 or more shipped, Never ordered, or Regular).

Show solution
SELECT c.first_name,
       c.last_name,
       COUNT(o.order_id) AS orders,
       COALESCE(SUM(CASE WHEN o.status = 'shipped' THEN o.quantity * p.price END), 0) AS shipped_spend,
       MAX(o.order_date) AS last_order,
       CASE
           WHEN COALESCE(SUM(CASE WHEN o.status = 'shipped' THEN o.quantity * p.price END), 0) >= 900 THEN 'VIP'
           WHEN COUNT(o.order_id) = 0 THEN 'Never ordered'
           ELSE 'Regular'
       END AS segment
FROM customers c
LEFT JOIN orders o   ON o.customer_id = c.customer_id
LEFT JOIN products p ON p.product_id = o.product_id
GROUP BY c.customer_id, c.first_name, c.last_name
ORDER BY shipped_spend DESC, c.last_name;
 first_name | last_name | orders | shipped_spend | last_order |    segment
------------+-----------+--------+---------------+------------+---------------
 Omar       | Haddad    |      1 |       1798.00 | 2025-10-03 | VIP
 Maria      | Lopez     |      3 |        989.00 | 2025-11-02 | VIP
 Kevin      | Patel     |      1 |        720.00 | 2025-09-15 | Regular
 Sofia      | Nguyen    |      2 |        569.00 | 2025-10-10 | Regular
 Ana        | Martinez  |      2 |        498.00 | 2025-10-21 | Regular
 James      | Carter    |      2 |         75.00 | 2025-09-07 | Regular
 Daniel     | Brooks    |      1 |             0 | 2025-09-22 | Regular
 Laura      | Chen      |      0 |             0 |            | Never ordered

LEFT JOIN keeps Laura, who never ordered. COUNT(o.order_id) gives her 0, and COALESCE turns her NULL spending into 0. The order of WHEN conditions matters: the first true one wins.

What to Do Next

  • Change the questions. Swap a state, a category, or a month and predict the result before you run it.
  • Solve each problem a second way. A join instead of a subquery, INTERSECT instead of HAVING, a window function instead of a correlated subquery. Comparing approaches is how you learn which one to reach for.
  • Work through a full analysis. The SQL practice project uses these same skills to answer business questions from start to finish.
  • Prepare for interviews. The SQL interview questions guide has interview-style problems on a bigger dataset.

Frequently Asked Questions

Where can I practice SQL for free?

Install SQLite (it’s free and needs no server) or PostgreSQL, load the sample shop.sql database, and work through these problems. The SQLite guide shows how to get started in minutes.

How long does it take to get comfortable with SQL?

Most people can write basic queries after a few days of practice, and become comfortable with joins, grouping, and subqueries in a few weeks. Window functions and query tuning take longer. Steady practice on real questions matters more than speed.

What should I do if my answer is different from the solution?

First compare the results. If they match, your query is correct, even if it’s written differently. If they don’t, run your query in pieces (the FROM and joins first, then the WHERE, then the grouping) to see where the rows change.

Conclusion

These 24 problems cover the skills most SQL work relies on: filtering and sorting, joins, grouping, CASE, subqueries, CTEs, and window functions. Come back to them after each level of the series, and you’ll notice the hard ones getting easier.

Next in the Learn SQL series, put everything together in a SQL practice project that analyzes the store’s sales from start to finish.