Pathubs SQL Curriculum • Capstone Applied Module

SQL Sales Analysis & Revenue Analytics

Combine JOINS, GROUP BY, aggregations, CTEs, Window Functions, and LAG/LEAD to answer real-world business questions: revenue tracking, customer LTV, AOV, category contributions, MoM growth, and product performance rankings in a live interactive SQL lab.

⏱️ Estimated Time:45 Minutes
🎯 Level:Applied Capstone • Real Business Analytics
📊 Track:E-commerce & SaaS Revenue Analytics
✨ Mode:Live Relational SQL Engine
1

Business Scenario & Relational Schema

You are a Data Analyst at Apex Retail, a fast-growing multi-channel e-commerce retailer. Executive leadership needs daily insights into product performance, regional demand, customer purchasing power, and revenue velocity.

Apex Retail Relational Database Schema

customers
customer_id (PK)
customer_name, city, region
──(1:N)──▶

orders
order_id (PK)
customer_id (FK), order_date, region
──(1:N)──▶

order_items
item_id (PK), order_id (FK)
product_id (FK), quantity, unit_price, discount
◀──(N:1)──

products
product_id (PK)
product_name, category, unit_price
2

The Sales Data Model: Order vs. Order Item

Crucial Analytical Distinction
An Order represents a single checkout transaction by a customer on a specific date.
An Order Item represents an individual product line inside that order.

For example, Order #1001 contains 2 distinct line items: 1 Laptop ($1,200) and 2 Keyboards ($120 each). Running COUNT(*) on order_items returns 2, but the number of customer checkout orders is 1!

3

Core Sales Metrics Definition

1. Net Revenue

SUM(qty * price * (1 - discount))

2. Average Order Value (AOV)

Total Net Revenue / COUNT(DISTINCT order_id)

3. Category Share %

Category Sales / SUM(Sales) OVER ()

4. MoM Growth %

(Sales - LAG(Sales)) / LAG(Sales) * 100

4

Total Sales & Net Revenue Calculation

-- Compute total net revenue after discounts
SELECT
    ROUND(SUM(quantity * unit_price * (1 - discount)), 2) AS net_revenue
FROM order_items;
6

Average Order Value (AOV)

SELECT
    ROUND(SUM(quantity * unit_price * (1 - discount)) / COUNT(DISTINCT order_id), 2) AS average_order_value
FROM order_items;
9

Monthly Sales & Month-over-Month Growth (LAG)

WITH monthly_sales AS (
    SELECT
        SUBSTRING(o.order_date, 1, 7) AS month,
        ROUND(SUM(oi.quantity * oi.unit_price * (1 - oi.discount)), 2) AS revenue
    FROM orders o
    JOIN order_items oi ON o.order_id = oi.order_id
    GROUP BY month
)
SELECT
    month,
    revenue,
    LAG(revenue) OVER (ORDER BY month) AS previous_month_revenue,
    ROUND(revenue - LAG(revenue) OVER (ORDER BY month), 2) AS revenue_variance
FROM monthly_sales;
12

Think Like a Data Analyst

Executive leaders do not ask for SQL queries — they ask strategic business questions. As an analytics professional, your job is to translate business ambiguity into rigorous SQL:

Executive Question:

"Which product lines should we discontinue or heavily promote?"

➔ Metric: Revenue ranking, contribution %, and unit margin volume.
Executive Question:

"Are our marketing campaigns acquiring high-value repeat buyers?"

➔ Metric: Customer LTV, multi-order frequency, and AOV distribution.
⚡Live Interactive Sales Analysis & Revenue Analytics Lab
Execute live SQL queries against realistic relational e-commerce tables: customers, products, orders, and order_items
Challenge 1 — Total Net RevenueReal Business Case

Calculate the total net sales revenue generated by the business, taking product quantities, unit prices, and discounts into account.

SQL Query Editor:
Query Execution Result:
TEST YOUR KNOWLEDGE

Knowledge Assessment: SQL Sales Analysis

Test your applied analytical reasoning on revenue calculations, AOV formulas, category contributions, MoM growth metrics, and ranking functions.

Question 1 of 10Current Score: 0 / 0
Q1

Why can "COUNT(*)" over the "order_items" table overcount the total number of customer orders?