Mastering MySQL for Data AnalystsLesson 42 of 42
Lesson 42 of 42

Executive Capstone: ScoopCraft Quick-Commerce MySQL Analytics Pipeline

Overview & Learning Objectives

This comprehensive capstone synthesizes all foundational and advanced competencies acquired across the 8-week MySQL analytics curriculum. You will architect an end-to-end analytics pipeline for ScoopCraft Quick-Commerce: establishing normalized schemas with foreign keys, ingesting transaction batches, computing executive cohort metrics using CTEs and Window Functions, and delivering an automated executive reporting view with zero manual touchpoints.

  • Architect a normalized relational schema in MySQL 8.0 with InnoDB constraints.
  • Perform advanced multi-table joins and window partitioning across millions of records.
  • Compute executive KPIs (MoM growth, cohort retention, SLA breach rate, product rankings) using CTEs.
  • Package analytical queries into secure, production-ready Database Views.

Industry Standard

Standard: Senior Data Analysts and Analytics Engineers at top quick-commerce enterprises (Zepto, Blinkit, Instacart) design production SQL models following these exact modular CTE patterns.

Real-World Example: ScoopCraft Executive Operations & Revenue Intelligence Engine

ScoopCraft deploys the capstone analytical query to power its executive leadership dashboard, tracking store velocity, margins, and rider SLAs.

Executive MetricSQL Technique AppliedBusiness Impact
Store MoM Revenue GrowthLAG() OVER (PARTITION BY store ORDER BY month)Monitors store expansion trajectory
Top 3 Flavors per RegionDENSE_RANK() OVER (PARTITION BY region ORDER BY rev DESC)Guides hyper-local inventory stocking
SLA Delivery Breach RateROUND(AVG(delivery_min > 20) * 100, 2)Detects rider shortages and fulfillment bottlenecks
Customer RFM QuantilesNTILE(4) OVER (ORDER BY lifetime_spend DESC)Automates VIP retention promotions

Syntax & Core SQL Implementation

-- ScoopCraft Quick-Commerce Executive Capstone Analytics Engine
WITH store_monthly_summary AS (
    SELECT 
        o.dark_store,
        DATE_FORMAT(o.order_date, '%Y-%m') AS sales_month,
        COUNT(o.order_id) AS total_orders,
        ROUND(SUM(o.order_total), 2) AS gross_revenue,
        ROUND(AVG(o.order_total), 2) AS avg_basket_size,
        -- SLA breach percentage (> 20 min)
        ROUND(AVG(IF(o.delivery_min > 20, 1, 0)) * 100, 2) AS sla_breach_rate
    FROM orders o
    WHERE o.order_status = 'Delivered'
    GROUP BY o.dark_store, DATE_FORMAT(o.order_date, '%Y-%m')
),
store_growth_metrics AS (
    SELECT 
        dark_store,
        sales_month,
        total_orders,
        gross_revenue,
        sla_breach_rate,
        LAG(gross_revenue, 1) OVER (PARTITION BY dark_store ORDER BY sales_month) AS prev_month_revenue,
        ROUND(((gross_revenue - LAG(gross_revenue, 1) OVER (PARTITION BY dark_store ORDER BY sales_month)) / 
               LAG(gross_revenue, 1) OVER (PARTITION BY dark_store ORDER BY sales_month)) * 100, 2) AS mom_growth_pct,
        DENSE_RANK() OVER (PARTITION BY sales_month ORDER BY gross_revenue DESC) AS national_store_rank
    FROM store_monthly_summary
)
SELECT 
    sales_month,
    national_store_rank,
    dark_store,
    gross_revenue,
    CONCAT(IFNULL(mom_growth_pct, 0.00), '%') AS mom_growth,
    total_orders,
    CONCAT(sla_breach_rate, '%') AS sla_breach_pct
FROM store_growth_metrics
WHERE sales_month = '2026-11'
ORDER BY national_store_rank ASC
LIMIT 10;

Parameters & Arguments Breakdown

  • Modular CTEs: Separates monthly aggregation from window lag growth calculations for complete readability.
  • IF(delivery_min > 20, 1, 0): Calculates SLA breach rates without complex subqueries.
  • LAG() OVER (PARTITION BY ...): Computes Month-over-Month growth partitioned by individual store.
  • DENSE_RANK() OVER (PARTITION BY ...): Ranks dark stores nationally per month without skipping ranks on ties.

Sample Walkthrough (Q&A)

Question: Why is structuring this capstone pipeline using CTEs superior to nesting multiple subqueries?

SQL Pattern: CTEs vs Nested Subqueries

Result: Clean top-to-bottom data lineage that any analyst can step through, test, and maintain in production

Explanation: CTEs isolate business logic steps (aggregation -> growth -> ranking -> final reporting) cleanly.

Common Mistakes & Limitations

  • Hardcoding month filters inside window functions instead of computing growth across all months and filtering in the final query (which breaks LAG!).
  • Forgetting that filtering before a window calculation prevents LAG from finding the previous month's data.

Keyboard Shortcuts (MySQL Workbench)

ActionWindowsMac
Execute Full PipelineCtrl + Shift + EnterCmd + Shift + Enter

Pro Tips & Modern Best Practices

  • To build a production dashboard, encapsulate this entire capstone CTE query inside `CREATE OR REPLACE VIEW v_executive_store_kpis AS ...`.
  • Combine this with a scheduled cron or Python script to export automated morning KPI reports to executive Slack channels.