KatInterview

Quarter-over-Quarter Revenue Analysis with Same-Store Metrics

SQL & DataAuto-graded
MediumAuto-gradedSQL & Datasqlaggregationtime-series

Last updated

Build a SQL query that compares quarterly performance across different time periods, handling store openings/closings and computing both total and same-store growth rates.

You work as a data analyst for a retail beverage business. Leadership wants to understand whether the second quarter of 2024 showed genuine performance weakness. Your task is to write a single SQL query that produces a comprehensive comparison table.

You have three tables:

transactions table:

  • transaction_id (BIGINT): unique identifier
  • sale_date (DATE): when the transaction occurred
  • store_id (INT): which location
  • product_id (INT): which item
  • units (INT): quantity sold
  • revenue (DECIMAL(10,2)): dollar amount
  • is_return (BOOLEAN): whether this was a customer return

stores table:

  • store_id (INT): location identifier
  • region (VARCHAR): geographic area
  • opened_date (DATE): when the store started operating
  • closed_date (DATE, nullable): when the store shut down, or NULL if still open

products table:

  • product_id (INT): item identifier
  • category (VARCHAR): product type

Sample data:

transactions:
transaction_id | sale_date  | store_id | product_id | units | revenue | is_return
1              | 2024-03-30 | 101      | 11         | 10    | 50.00   | false
2              | 2024-04-02 | 101      | 11         | 8     | 44.00   | false
3              | 2024-04-15 | 102      | 12         | 5     | 40.00   | false
4              | 2024-05-01 | 103      | 11         | -1    | -5.50   | true
5              | 2024-06-20 | 101      | 12         | 7     | 63.00   | false
6              | 2023-06-10 | 101      | 11         | 9     | 45.00   | false

stores:
store_id | region | opened_date | closed_date
101      | West   | 2020-01-01  | NULL
102      | East   | 2021-05-10  | NULL
103      | North  | 2024-05-01  | 2024-07-15

products:
product_id | category
11         | Soda
12         | Juice

Your query must:

  1. Define quarters using calendar boundaries: Q1 2024 spans 2024-01-01 through 2024-03-31; Q2 2024 spans 2024-04-01 through 2024-06-30; Q2 2023 spans 2023-04-01 through 2023-06-30.

  2. Generate exactly two rows of output:

    • Q2_2024_vs_Q1_2024 (sequential quarter comparison)
    • Q2_2024_vs_Q2_2023 (year-over-year comparison)
  3. For each comparison row, calculate these columns:

    • total_revenue_period_a, total_revenue_period_b: aggregate revenue for each period
    • revenue_pct_change: percent change from period B to period A
    • total_units_period_a, total_units_period_b: aggregate unit sales
    • units_pct_change: percent change in units
    • avg_price_period_a, avg_price_period_b: calculated as total revenue divided by total units
    • same_store_revenue_pct_change: revenue growth considering only stores that operated continuously throughout both periods being compared
    • active_store_count_a, active_store_count_b: number of stores that were operational during each period
    • decline_flag: TRUE if both revenue and units fell by more than 5%, otherwise FALSE
  4. Handle returns correctly—they already appear as negative values in the data and should net against positive sales.

  5. A store qualifies for "same-store" analysis only if it was open for the complete duration of both periods in the comparison (opened on or before the earlier period begins, and either never closed or closed after the later period ends).

  6. Avoid data duplication issues from joins and compute aggregates at the correct level before calculating ratios.

Write standard ANSI SQL using CTEs and window functions as needed. No UDFs or database-specific extensions.

Hints

Hint 1

Aggregate transactions to the day-store-product grain before joining to other tables to prevent row duplication

Hint 2

Calculate period-level sums first, then compute ratios—never average pre-computed averages

Hint 3

The 'same-store' set differs between sequential and year-over-year comparisons since the eligibility windows are different