This project walks through a complete SQL analysis of a small online store, the way you’d do it at work: explore the data, check its quality, calculate the key numbers, dig into products and customers, and finish with a short list of findings and recommendations. Every query was tested on PostgreSQL, MySQL, SQL Server, and SQLite.
Practice problems teach individual skills. A project teaches you how they fit together: which question to ask next, how to check that a number is right, and how to turn query results into something a manager can act on. This is the capstone of the Learn SQL series, so it uses a bit of everything from the earlier lessons.
The Scenario
You’ve just joined a small online store that sells electronics, accessories, and office furniture to customers in California, Arizona, and Nevada. The owner asks you three questions:
- How is the business doing? Revenue, orders, and trends.
- What sells, and who buys? The best products, categories, customers, and states.
- Where are we losing money? Returns, cancellations, and orders that are stuck.
Download shop.sql and load it as shown in Part 1. It contains three tables: customers, products, and orders, where each order is one product in some quantity. Revenue is orders.quantity * products.price.
Try to write each query yourself before reading the solution. The point isn’t to copy the SQL; it’s to practice deciding what to calculate next.
Step 1: Explore the Data
Before calculating anything, find out how much data there is and what period it covers.
SELECT 'customers' AS table_name, COUNT(*) AS row_count FROM customers
UNION ALL
SELECT 'products', COUNT(*) FROM products
UNION ALL
SELECT 'orders', COUNT(*) FROM orders;
table_name | row_count
------------+-----------
customers | 8
products | 6
orders | 12
UNION ALL stacks three one-row results into a single table, a trick from the set operations lesson.
SELECT MIN(order_date) AS first_order,
MAX(order_date) AS last_order,
COUNT(DISTINCT customer_id) AS customers_who_ordered
FROM orders;
first_order | last_order | customers_who_ordered
-------------+------------+-----------------------
2025-08-02 | 2025-11-02 | 7
The data covers August to early November 2025. Only 7 of the 8 customers have ordered, which you’ll come back to later.
Step 2: Check Data Quality
Analysts who skip this step end up presenting wrong numbers. Check for missing values, the list of possible statuses, and orders that point to customers or products that don’t exist.
SELECT COUNT(*) AS customers,
COUNT(*) - COUNT(email) AS missing_email,
COUNT(*) - COUNT(city) AS missing_city
FROM customers;
customers | missing_email | missing_city
-----------+---------------+--------------
8 | 1 | 1
COUNT(column) skips NULL values, so COUNT(*) - COUNT(email) is the number of missing emails. One customer has no email and one has no city.
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
There are four statuses, all spelled consistently, so filtering on 'shipped' is safe. If you saw Shipped and shipped here, you’d clean the data first.
SELECT COUNT(*) AS orphan_orders
FROM orders o
LEFT JOIN customers c ON c.customer_id = o.customer_id
LEFT JOIN products p ON p.product_id = o.product_id
WHERE c.customer_id IS NULL OR p.product_id IS NULL;
orphan_orders
---------------
0
No orphan orders: every order matches a real customer and product. The foreign keys in shop.sql guarantee this, but real data often comes from systems without them, so it’s worth checking.
Decision: count revenue only from shipped orders. Pending orders haven’t been delivered, and returned or cancelled orders aren’t income.
Step 3: Calculate the Headline Numbers
SELECT COUNT(*) AS orders,
SUM(o.quantity) AS units,
SUM(o.quantity * p.price) AS revenue,
ROUND(AVG(o.quantity * p.price), 2) AS avg_order_value
FROM orders o
JOIN products p ON p.product_id = o.product_id
WHERE o.status = 'shipped';
orders | units | revenue | avg_order_value
--------+-------+---------+-----------------
8 | 16 | 4649.00 | 581.13
The store shipped 8 orders and 16 units for $4,649 in revenue, with an average order value of $581.13. Those are the numbers to lead with.
To see the whole picture, including money that isn’t revenue yet or anymore, break all orders down by status:
SELECT o.status,
COUNT(*) AS orders,
SUM(o.quantity * p.price) AS order_value
FROM orders o
JOIN products p ON p.product_id = o.product_id
GROUP BY o.status
ORDER BY order_value DESC;
status | orders | order_value
-----------+--------+-------------
shipped | 8 | 4649.00
returned | 1 | 899.00
pending | 2 | 445.00
cancelled | 1 | 360.00
$899 was returned, $360 was cancelled, and $445 is still pending. Keep these numbers for Step 7.
Step 4: Look at the Trend
Monthly revenue shows whether the business is growing. The SUBSTRING(CAST(...)) expression turns each date into YYYY-MM in every database, and a window function adds a running total:
WITH monthly AS (
SELECT SUBSTRING(CAST(o.order_date AS CHAR(10)), 1, 7) AS month,
COUNT(*) AS orders,
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 SUBSTRING(CAST(o.order_date AS CHAR(10)), 1, 7)
)
SELECT month, orders, revenue,
SUM(revenue) OVER (ORDER BY month) AS cumulative_revenue
FROM monthly
ORDER BY month;
month | orders | revenue | cumulative_revenue
---------+--------+---------+--------------------
2025-08 | 4 | 1384.00 | 1384.00
2025-09 | 2 | 1218.00 | 2602.00
2025-10 | 2 | 2047.00 | 4649.00
Revenue dipped in September and jumped to $2,047 in October, the best month. November has no shipped orders yet, only a pending one. With three months of data, treat this as an early signal, not a trend. For a month-over-month percentage, use LAG() from window functions part 2.
Step 5: Products and Categories
SELECT p.category,
SUM(o.quantity) AS units,
SUM(o.quantity * p.price) AS revenue,
ROUND(100.0 * SUM(o.quantity * p.price) / SUM(SUM(o.quantity * p.price)) OVER (), 1) AS pct_of_revenue
FROM orders o
JOIN products p ON p.product_id = o.product_id
WHERE o.status = 'shipped'
GROUP BY p.category
ORDER BY revenue DESC;
category | units | revenue | pct_of_revenue
-------------+-------+---------+----------------
Electronics | 6 | 3444.00 | 74.1
Furniture | 5 | 1040.00 | 22.4
Accessories | 5 | 165.00 | 3.5
Electronics brings in 74.1% of revenue. SUM(SUM(...)) OVER () divides each category’s total by the grand total, as explained in window functions part 2.
Now each product, including any that never sold. The status condition goes in the ON clause of the LEFT JOIN, so products without shipped orders still appear with zeros:
SELECT p.name,
p.category,
COUNT(o.order_id) AS orders,
COALESCE(SUM(o.quantity), 0) AS units,
COALESCE(SUM(o.quantity * p.price), 0) AS revenue
FROM products p
LEFT JOIN orders o ON o.product_id = p.product_id
AND o.status = 'shipped'
GROUP BY p.product_id, p.name, p.category
ORDER BY revenue DESC;
name | category | orders | units | revenue
--------------+-------------+--------+-------+---------
Laptop | Electronics | 2 | 3 | 2697.00
Monitor | Electronics | 2 | 3 | 747.00
Office Chair | Furniture | 1 | 4 | 720.00
Desk | Furniture | 1 | 1 | 320.00
Keyboard | Accessories | 1 | 2 | 90.00
Mouse | Accessories | 1 | 3 | 75.00
The Laptop alone earned $2,697, 58% of all revenue. Every product sold at least once. Accessories sell plenty of units but at low prices, which makes them good add-ons to suggest with a laptop.
Step 6: Customers
Who are the best customers, and how much of the revenue depends on them?
WITH customer_spend AS (
SELECT c.customer_id,
c.first_name,
c.last_name,
c.state,
COUNT(*) AS orders,
SUM(o.quantity * p.price) AS revenue
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.first_name, c.last_name, c.state
)
SELECT first_name, last_name, state, orders, revenue,
RANK() OVER (ORDER BY revenue DESC) AS revenue_rank,
ROUND(100.0 * SUM(revenue) OVER (ORDER BY revenue DESC)
/ SUM(revenue) OVER (), 1) AS cumulative_pct
FROM customer_spend
ORDER BY revenue_rank;
first_name | last_name | state | orders | revenue | revenue_rank | cumulative_pct
------------+-----------+-------+--------+---------+--------------+----------------
Omar | Haddad | CA | 1 | 1798.00 | 1 | 38.7
Maria | Lopez | CA | 2 | 989.00 | 2 | 59.9
Kevin | Patel | NV | 1 | 720.00 | 3 | 75.4
Sofia | Nguyen | AZ | 2 | 569.00 | 4 | 87.7
Ana | Martinez | CA | 1 | 498.00 | 5 | 98.4
James | Carter | CA | 1 | 75.00 | 6 | 100.0
The running percentage tells the story: Omar alone brings in 38.7% of revenue, and the top three customers bring in 75.4%. That’s a lot of dependence on a few people.
How many customers come back?
WITH order_counts AS (
SELECT customer_id, COUNT(*) AS orders
FROM orders
GROUP BY customer_id
)
SELECT CASE WHEN orders = 1 THEN 'One order' ELSE 'Repeat customer' END AS customer_type,
COUNT(*) AS customers
FROM order_counts
GROUP BY CASE WHEN orders = 1 THEN 'One order' ELSE 'Repeat customer' END
ORDER BY customer_type;
customer_type | customers
-----------------+-----------
One order | 3
Repeat customer | 4
4 of the 7 customers who ordered have ordered more than once, a good sign for a young store. This counts orders of any status, since a repeat order shows interest even if it was returned.
And by state, including customers who haven’t ordered:
SELECT c.state,
COUNT(DISTINCT c.customer_id) AS customers,
COUNT(o.order_id) AS orders,
COALESCE(SUM(CASE WHEN o.status = 'shipped' THEN o.quantity * p.price END), 0) AS revenue
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.state
ORDER BY revenue DESC;
state | customers | orders | revenue
-------+-----------+--------+---------
CA | 4 | 8 | 3360.00
NV | 2 | 1 | 720.00
AZ | 2 | 3 | 569.00
California brings in the most revenue. Nevada has two customers but only one order: Laura signed up in June and has never bought anything.
Step 7: Where Money Is Lost
SELECT p.category,
COUNT(*) AS orders,
SUM(CASE WHEN o.status = 'returned' THEN 1 ELSE 0 END) AS returned,
SUM(CASE WHEN o.status = 'cancelled' THEN 1 ELSE 0 END) AS cancelled,
SUM(CASE WHEN o.status IN ('returned', 'cancelled')
THEN o.quantity * p.price ELSE 0 END) AS lost_value
FROM orders o
JOIN products p ON p.product_id = o.product_id
GROUP BY p.category
ORDER BY lost_value DESC;
category | orders | returned | cancelled | lost_value
-------------+--------+----------+-----------+------------
Electronics | 5 | 1 | 0 | 899.00
Furniture | 4 | 0 | 1 | 360.00
Accessories | 3 | 0 | 0 | 0
Losses come from one returned laptop ($899) and one cancelled furniture order ($360). One return is too few to call a pattern, but a returned $899 laptop is worth asking about.
Finally, the pending orders that need follow-up:
SELECT o.order_id, o.order_date, c.first_name, c.last_name,
p.name AS product, o.quantity * p.price AS order_value
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 = 'pending'
ORDER BY o.order_date;
order_id | order_date | first_name | last_name | product | order_value
----------+------------+------------+-----------+---------+-------------
108 | 2025-09-22 | Daniel | Brooks | Mouse | 125.00
112 | 2025-11-02 | Maria | Lopez | Desk | 320.00
Daniel’s order has been pending since September 22, and from Step 2 you know Daniel is the customer without an email address, so the store may not be able to reach him. That’s a concrete action item found by connecting two queries.
Step 8: Save the Work as a View
The same joins appeared in almost every step. A view saves them once so the next analysis starts faster:
CREATE VIEW sales_summary AS
SELECT o.order_id,
o.order_date,
o.status,
c.customer_id,
c.first_name,
c.last_name,
c.state,
p.name AS product,
p.category,
o.quantity,
o.quantity * p.price AS order_value
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
JOIN products p ON p.product_id = o.product_id;
Now follow-up questions are short. Shipped revenue by category in California:
SELECT category, SUM(order_value) AS revenue
FROM sales_summary
WHERE status = 'shipped' AND state = 'CA'
GROUP BY category
ORDER BY revenue DESC;
category | revenue
-------------+---------
Electronics | 3195.00
Accessories | 165.00
When you’re done practicing, you can remove it with DROP VIEW sales_summary;.
Step 9: Write Up the Findings
Queries aren’t the deliverable; the conclusions are. A short summary for the owner might look like this:
- Revenue: $4,649 from 8 shipped orders, with an average order of $581.13. October was the best month ($2,047).
- Products: Electronics is 74.1% of revenue, and the Laptop alone is 58%. Accessories sell in volume at low prices and are natural add-ons.
- Customers: the top three customers bring in 75.4% of revenue, and 4 of 7 buyers have ordered more than once. Keeping the top customers happy matters a lot.
- Risks: $899 in returns, $360 in cancellations, and $445 in pending orders.
Recommendations:
- Contact Daniel Brooks about his order pending since September 22, and collect an email address for him.
- Follow up with Laura Chen, who signed up in June but never ordered, perhaps with a first-order discount.
- Suggest accessories at checkout when someone buys a laptop or monitor.
- Find out why the laptop was returned before it happens again.
- Re-run this analysis monthly. With the view in place, it takes minutes.
Notice that every number in the write-up comes from a query above, and the recommendations name people and actions, not just statistics. That’s what makes an analysis useful.
Take It Further
- Add a question of your own, like “Which products do repeat customers buy?” or “What’s the average time between a customer’s orders?”
- Fill in missing months with a calendar and a cross join, from the self joins and cross joins lesson, so months without sales show 0.
- Turn Step 5 into a pivot with categories as rows and months as columns, using the pivot lesson.
- Chart the results in Excel, Power BI, or Python, and present them as a one-page report.
- Practice more with the 24 SQL practice problems and the SQL interview questions.
Conclusion
A real analysis follows the same path every time: explore the data, check its quality, agree on definitions (here, “revenue means shipped”), calculate the headline numbers, break them down by time, product, and customer, look for problems, and write up findings with clear recommendations. The SQL in each step is something you learned earlier in the series; the new skill is choosing the next question.
Next in the Learn SQL series, test yourself under interview conditions with SQL interview questions for data science.
