There is one cost row per subscription per day. baseline_avg = average cost of up to 7 PREVIOUS days of that subscription (the current day is not included). A day is an anomaly when at least 3 previous days exist in that window and cost is strictly greater than 2 * baseline_avg. Return subscription_id, cost_date, cost and baseline_avg (ROUND 2) for anomaly days. Order by subscription_id, cost_date.
| Column | Type |
|---|---|
| subscription_id | TEXT |
| cost_date | DATE |
| cost | REAL |
| subscription_id | cost_date | cost |
|---|---|---|
| sub-1 | 2026-09-01 | 100 |
| sub-1 | 2026-09-02 | 110 |
| sub-1 | 2026-09-03 | 90 |
| sub-1 | 2026-09-04 | 100 |
| sub-1 | 2026-09-05 | 250 |
| sub-1 | 2026-09-06 | 105 |
| sub-1 | 2026-09-07 | 120 |
| sub-1 | 2026-09-08 | 95 |
| subscription_id | cost_date | cost | baseline_avg |
|---|---|---|---|
| sub-1 | 2026-09-05 | 250 | 100 |
| sub-1 | 2026-09-09 | 400 | 124.29 |
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
{{ totalCount }} questions in the style asked at Amazon, Microsoft, Google, Meta, Flipkart and more — {{ hardPct }}% Hard. Write SQL in a real database engine in your browser, run it on sample data, then evaluate against hidden tests.
Company tags show where this type of question is commonly reported in interviews. Questions, data and solutions are written by TechTown.
{{ cur.statement }}
No submissions yet. Press Evaluate to submit your query.
{{ solutionSql }}
{{ out.error }}
You have solved your 3 free questions — nice work! Log in (free) to unlock all 150 questions, keep your streak and appear on the leaderboard.