Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Performance & internals
Stage 5: Specialization

Project

The store sales report

Start after Part 4

Stage 1

Goal: Answer fifteen real business questions about shopdb using only Stage 1 SQL — and package them as one commented, re-runnable report file, the way working analysts actually deliver. Time: ~3–4 hours

The brief

The store's owner wants a monthly health report. No dashboards yet — a single SQL file, report.sql, that anyone can run top to bottom with:

$ psql -d shopdb -f report.sql

Write it yourself first, one question at a time, in psql. Only then compare with the reference report.sql in this folder — the comments there explain each choice.

The fifteen questions

Catalogue and stock

  1. How many products do we sell, how many units are on the shelves, and what is that stock worth?
  2. Per category: product count, cheapest, priciest and average price.
  3. Which products are out of stock or running low (under 20 units)? Flag the empties loudly.

Orders 4. The order status mix, with percentages. 5. Orders per month — are we growing? 6. Top 5 products by revenue (from the order lines). 7. Top 5 products by units sold. 8. Total order value and the average order value.

Money 9. How do customers pay — count and amount per payment method? 10. Money collected per month.

Customers 11. Signups per month. 12. Customers per city, with the missing city shown as Unknown. 13. Which customers have ever placed an order? 14. Who has never ordered? (They get the next discount voucher.) 15. What do customers do on the site — events and distinct customers per event type?

Questions 13–14 need a (SELECT ...) inside the WHERE — the scalar-subquery preview from Part 4 stretched one step further. It is Stage 2 material officially; the reference file shows the pattern and marks the trap to revisit.

Self-check

Your numbers should match these, straight from a real run against a fresh shopdb:

Check Expected
Q1 shelf value 74275.00 across 490 units of 10 products
Q3 alerts 4 rows — Coffee Maker is OUT OF STOCK, 3 products low
Q4 delivered share 66.7 % (6 of 9)
Q6 top earner product 1 with revenue 2400.00
Q7 top by units product 5 with 5 units
Q8 average order value 659.00 over 9 orders
Q9 biggest method card: 3 payments, 2715.00 collected
Q12 Unknown city exactly 1 customer
Q14 never ordered exactly one row: Vikram Singh
Q15 event mix view 3, cart 2, purchase 2

A number off? Re-seed (psql -d shopdb -f shopdb.sql) and re-run — and if it still differs, read your WHERE/GROUP BY against the reference.

Stretch goals (optional)

  • Add "percentage of revenue" to Q6 with the scalar-subquery pattern.
  • Rewrite Q3 so the alert thresholds live in one obvious place.
  • Q8 counts only orders that have lines. Confirm every shopdb order does — then write the query that would prove it in one number.

Search the guide