Skip to content

About

SQL analytics on e-commerce data: cohort retention, RFM segmentation, funnel conversion and channel LTV using window functions, plus data-quality checks.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

E-Commerce Analytics with SQL

End-to-end SQL analysis of an online retailer: revenue trends, cohort retention, RFM customer segmentation, funnel conversion, product concentration, channel lifetime value, and a data-quality test suite.

Every number below is produced by the queries in sql/ and can be reproduced with two commands.

Monthly revenue

Business questions answered

# Question Query SQL techniques
1 How are revenue, AOV and margin trending? 01_monthly_revenue_kpis.sql CTEs, LAG() for MoM and YoY growth, named WINDOW
2 Do customers come back after their first purchase? 02_cohort_retention.sql First-touch cohorts with MIN() OVER, month-offset arithmetic
3 Who are our best and at-risk customers? 03_rfm_segmentation.sql NTILE() scoring, rule-based CASE segments, share of total
4 Where do visitors drop out of the funnel? 04_conversion_funnel.sql UNION ALL unpivot, FIRST_VALUE(), step conversion with LAG()
5 Does the 80/20 rule hold for products? 05_category_pareto.sql Running totals, RANK(), ROWS BETWEEN frames
6 Which acquisition channel brings the most valuable customers? 06_channel_ltv.sql 12-month LTV window, ROW_NUMBER(), LEAD()
7 Can we trust the data? 07_data_quality_checks.sql Anti-joins, duplicate detection, payment reconciliation

Key findings

  • Revenue grew 30.6% year over year ($923K in 2024 to $1.21M in 2025), with Q4 alone contributing 32% of all revenue. Average order value stayed stable at $117 to $138, so growth came from more orders, not bigger baskets.
  • Retention is the biggest lever. On average only 19.9% of a monthly cohort buys again the following month, and 8.6% by month 3.
  • "At Risk" customers hold 33.9% of revenue. 2,296 customers who used to buy often (2+ orders on average) haven't purchased in ~17 months. They are a larger revenue pool than Champions (31.2%), which makes a win-back campaign the highest-value retention play.
  • Mobile converts at less than half the rate of desktop (3.8% vs. 9.1% of sessions purchase). The gap is widest at add-to-cart (22% vs. 30% of product viewers) and checkout completion (66% vs. 79%), pointing to mobile cart and checkout UX.
  • 25% of products drive 80% of revenue, a classic Pareto pattern useful for inventory and merchandising priorities.
  • Acquisition channels are close on 12-month LTV ($217 to $230), so channel budget decisions should lean on acquisition cost rather than downstream value.
  • Data quality: the test suite flags 35 customers with missing region and 20 duplicate payment rows; all referential-integrity and reconciliation checks pass.

Data model

Six tables in SQLite (sql/00_schema.sql):

customers (12,000)  ─┐
                     ├── orders (18.5K) ──┬── order_items (32.5K) ── products (400)
                     │                    └── payments (18.6K)
web_sessions (260K)  (funnel events: view → cart → checkout → purchase)

The dataset is synthetic and seeded (src/generate_data.py) so the project is fully reproducible without downloading anything. It is modelled on real retail behaviour: seasonality with a Q4 peak, a long tail of one-time buyers, Zipf-shaped product popularity, a device-dependent funnel, and a small share of deliberately dirty records for the data-quality checks to catch.

Run it

pip install -r requirements.txt
python src/generate_data.py     # builds data/ecommerce.db (~15 MB, a few seconds)
python src/run_analysis.py      # runs all queries → results/*.csv and images/*.png

You can also open data/ecommerce.db in DBeaver, DB Browser for SQLite, or VS Code and run any query in sql/ directly. The queries use standard SQL window functions and port to PostgreSQL or Snowflake with only small date-function changes (strftime → date_trunc / to_char).

Project structure

├── sql/                 # schema + 7 analysis queries (the core of the project)
├── src/
│   ├── generate_data.py # reproducible synthetic dataset → SQLite
│   └── run_analysis.py  # runs queries, saves CSVs, draws charts
├── results/             # query outputs as CSV
└── images/              # charts used in this README

Tools

SQL (SQLite) · Python · pandas · Matplotlib

About

SQL analytics on e-commerce data: cohort retention, RFM segmentation, funnel conversion and channel LTV using window functions, plus data-quality checks.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages