Peak Season

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L4
Asked in
Lyft

Problem

Finance's peak-season forecast leans on a single region and calendar month as the profit high point, and nobody has confirmed which pairing actually earns that spot. Tie each row in orders to every row in transactions whose ID falls in the same bucket modulo 100, total the order profit by region and the transaction's calendar month, and return the top pairing with its total.

One region, one month. Peak profit.

Tables

orders

Columns: order_id INTEGER, status TEXT, region TEXT, profit REAL

orders: 10 of 200 rows
order_idstatusregionprofit
10097PendingEU69.37
10194CancelledAPAC88.74
10291ShippedLATAM108.11
10388ProcessingMEA127.48
10485ReturnedUS146.85
10582On HoldEU166.22
10679CompletedAPAC185.59
10776PendingLATAM204.96
10873CancelledMEA224.33
10970ShippedUS243.7

transactions

Columns: transaction_id INTEGER, user_id INTEGER, product_id INTEGER, quantity INTEGER, total_amount REAL, transaction_date TEXT

transactions: 10 of 200 rows
transaction_iduser_idproduct_idquantitytotal_amounttransaction_date
10671971001223.462026-02-02
11342941050336.932026-03-03
12013911099450.42026-04-04
12684881148563.872026-05-05
13355851197002026-06-06
14026821246290.812026-07-07
146977912953104.282026-08-08
153687613444117.752026-09-09
160397313935-452026-01-10
1670107014421144.692026-02-11

Expected output

First 1 of 1 rows
regiontxn_monthtotal_profit
US059653.5

Worked solution and explanation

What this is really asking

o.order_id % 100 = t.transaction_id % 100 is not a real key. It is a synthetic bucket join across 30M and 100M rows that fans into roughly a 30-trillion-row pre-aggregate. Every shape decision flows from that.


Break down the requirements

Step 1: Join on the modulo bucket

Match each order to every transaction sharing the same (id mod 100). This is a many-to-many bucket join, not a referential lookup.

Step 2: Group by region and transaction month

Pull the calendar month out of transaction_date with strftime('%m', t.transaction_date) so the same month across years collapses together, then SUM profit per (region, month). The result is a two-digit string like '05', which is exactly what the grader expects.

Step 3: Order by total_profit and LIMIT 1

The prompt asks for one winner. Sort descending on the aggregate and take the top row. Ties resolve arbitrarily without a tiebreaker column.


The solution

TOP REGION-MONTH BY PROFIT
SELECT
    o.region,
    strftime('%m', t.transaction_date) AS txn_month,
    SUM(o.profit) AS total_profit
FROM orders o
INNER JOIN transactions t
    ON (o.order_id % 100) = (t.transaction_id % 100)
GROUP BY o.region, strftime('%m', t.transaction_date)
ORDER BY total_profit DESC
LIMIT 1

COMMON FOLLOW-UP QUESTIONS

Common follow-up questions