3. Modifying Data, Performance & Capstone

Capstone Project: E-Commerce Sales Analytics

Apply everything from the SQL course to design an e-commerce schema and solve 5 real business analytics questions step by step.

3 min readUpdated 2026-11-29
In This Lesson
  • Design a normalized 4-table e-commerce schema with Foreign Keys
  • Answer real revenue, top-product, and inactive-customer queries
  • Combine CTEs, JOINs, and indexes in a complete SQL analytics workflow

Let's bring every SQL skill together in a realistic scenario: you are the data & backend engineer for an online store and need to build the database schema and answer real business questions.

Schema Setup (schema.sql)

You can run all of this locally in SQLite (sqlite3 store.db) or PostgreSQL:

sql
CREATE TABLE customers (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  city TEXT NOT NULL,
  joined_at TEXT NOT NULL
);

CREATE TABLE products (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  category TEXT NOT NULL,
  price NUMERIC NOT NULL
);

CREATE TABLE orders (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER NOT NULL,
  status TEXT NOT NULL, -- 'paid', 'pending', 'cancelled'
  ordered_at TEXT NOT NULL,
  FOREIGN KEY (customer_id) REFERENCES customers(id)
);

CREATE TABLE order_items (
  id INTEGER PRIMARY KEY,
  order_id INTEGER NOT NULL,
  product_id INTEGER NOT NULL,
  quantity INTEGER NOT NULL,
  unit_price NUMERIC NOT NULL,
  FOREIGN KEY (order_id) REFERENCES orders(id),
  FOREIGN KEY (product_id) REFERENCES products(id)
);

Business Questions Solved

Question 1: What Is Total Revenue From Paid Orders Only?

sql
SELECT
  ROUND(SUM(oi.quantity * oi.unit_price), 2) AS total_paid_revenue
FROM orders o
INNER JOIN order_items oi ON oi.order_id = o.id
WHERE o.status = 'paid';

Question 2: Top 3 Best-Selling Products by Revenue

sql
SELECT
  p.name,
  p.category,
  SUM(oi.quantity) AS units_sold,
  ROUND(SUM(oi.quantity * oi.unit_price), 2) AS revenue
FROM products p
INNER JOIN order_items oi ON oi.product_id = p.id
INNER JOIN orders o ON o.id = oi.order_id
WHERE o.status = 'paid'
GROUP BY p.id, p.name, p.category
ORDER BY revenue DESC
LIMIT 3;

Question 3: Customers Who Have Never Placed an Order

sql
SELECT c.id, c.name, c.city
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;

Question 4: Revenue Breakdown by City (Using a CTE)

sql
WITH paid_order_totals AS (
  SELECT
    o.id AS order_id,
    o.customer_id,
    SUM(oi.quantity * oi.unit_price) AS order_total
  FROM orders o
  INNER JOIN order_items oi ON oi.order_id = o.id
  WHERE o.status = 'paid'
  GROUP BY o.id, o.customer_id
)
SELECT
  c.city,
  COUNT(p.order_id) AS paid_orders,
  ROUND(SUM(p.order_total), 2) AS city_revenue
FROM customers c
INNER JOIN paid_order_totals p ON p.customer_id = c.id
GROUP BY c.city
ORDER BY city_revenue DESC;

Question 5: Rank Products Within Each Category (Window Function)

sql
WITH product_revenue AS (
  SELECT
    p.name,
    p.category,
    ROUND(SUM(oi.quantity * oi.unit_price), 2) AS revenue
  FROM products p
  INNER JOIN order_items oi ON oi.product_id = p.id
  INNER JOIN orders o ON o.id = oi.order_id
  WHERE o.status = 'paid'
  GROUP BY p.id, p.name, p.category
)
SELECT
  name,
  category,
  revenue,
  RANK() OVER (PARTITION BY category ORDER BY revenue DESC) AS category_rank
FROM product_revenue;

Run the Capstone Queries Live

Try any of the 5 business questions against the pre-loaded customers, products, orders, and order_items tables right here:

SQL (SQLite)Live Capstone Sandbox: City Revenue CTE
Output

Click “Run Code” to execute this snippet right in your browser.

Practice & Extension Checklist

Run & Extend Your Analytics Project

0/5 done

You now have the core SQL toolkit used daily by software engineers and data analysts!