SQL-Engineering-Handbook

03 · E-Commerce — Window Functions in Customer & Product Analytics

Customer order history annotated with first-order flag and running CLV

Introduction

E-commerce analytics blends the two patterns you’ve already built: peer comparison (Chapter 01) and time-based comparison (Chapter 02) - then adds a third dimension unique to this domain: the customer as the unit of analysis over their entire relationship with the business. Customer Lifetime Value, repeat purchase behavior, and order-level ranking all require looking at a customer’s or product’s full history, not just a single transaction.


Business Background

A typical e-commerce schema centers on orders and order line items:

Analytics and growth teams use this data to understand who is buying, what they’re buying, and how often they return.


Typical KPIs


Typical Dashboards


Business Problems

  1. “Rank our customers by total lifetime spend - who are our VIP customers?”
  2. “What’s the running revenue contributed by each customer over time, so we can see growth in their relationship with us?”
  3. “Rank our products within each category by revenue.”
  4. “What percentage of customers make more than one purchase, and how long is the gap between their first and second order?”
  5. “What’s the average order value, and how does it compare to each customer’s own historical average?”
  6. “Rank our largest baskets (orders) by total value, for a case study on high-value orders.”

Why Window Functions Are Needed

Customer Lifetime Value and repeat-purchase analysis both require comparing a customer’s current order to their own history - not to a fixed group like a department, and not strictly to a calendar period like a fiscal month, but to their own prior orders, in sequence. This is a natural extension of LAG()/LEAD() and running-total patterns, partitioned by customer_id instead of salesperson_id or dept_id. Product and category rankings reuse the exact RANK()/DENSE_RANK() leaderboard pattern from Chapters 01 and 02, now applied to product_id and category_id partitions.


Functions Used in This Chapter

Function Business Explanation
RANK() / DENSE_RANK() Customer value leaderboard; product ranking within category.
SUM() OVER (PARTITION BY customer_id ORDER BY order_date ...) Running lifetime revenue per customer.
LAG() Time between a customer’s consecutive orders (repeat purchase gap).
AVG() OVER (PARTITION BY customer_id) Customer’s own historical average order value, for anomaly comparison.
ROW_NUMBER() Identifying each customer’s first order (for cohort and CLV-start analysis).

SQL Concepts Reinforced


Performance Notes


Common Mistakes


Interview Questions

  1. “How would you calculate Customer Lifetime Value using window functions?” — Expect a running SUM(order_total) OVER (PARTITION BY customer_id ORDER BY order_date), and a discussion of the difference between “running CLV” and “final/total CLV.”
  2. “How would you identify each customer’s very first order?” — Expect ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date) = 1.
  3. “How would you calculate the repeat purchase rate for a cohort of customers?” — Expect a COUNT() OVER (PARTITION BY customer_id) (or a GROUP BY + HAVING COUNT(*) > 1) to identify repeat purchasers, divided by total customers in the cohort.
  4. “Rank products by revenue within their category - how would you write this?” — Expect RANK() OVER (PARTITION BY category_id ORDER BY SUM(revenue) DESC) over a pre-aggregated product-revenue CTE.
  5. “What’s the difference between average order value calculated globally vs. per customer, and why does the distinction matter for a churn model?” — Expect a discussion of how global AOV can mask individual behavioral drift that a churn model needs to detect.

Summary

E-commerce analytics is where peer comparison (Chapter 01) and time-based comparison (Chapter 02) combine and extend to a new grain: the customer’s own history. Running lifetime value, first-order detection, and repeat-purchase gap analysis are the foundational building blocks of nearly every growth and retention dashboard in the industry.


Further Practice


Next: 03_ECOMMERCE.sql — the fully engineered SQL chapter for this domain.


Previous: 02_SALES_ANALYTICS.md · Module: README · Next chapter: 04_BANKING.md