We use cookies for site analytics. Accept to help us understand how the site is used. See our Privacy Policy for details.
A medium sql problem, graded against 3 test cases (1 of them hidden).
Query-writing problems - joins, aggregation, window functions, and query tuning.
Reach for it when you see: A schema and a question about the data rather than a function signature.
More SQLproblems →```sql
SELECT date,
metric,
LAG(metric) OVER (ORDER BY date) AS prev_metric,
metric - LAG(metric) OVER (ORDER BY date) AS delta
FROM DailyMetric
ORDER BY date;
```
`LAG(col, n, default)` returns the value n rows back; `LEAD` does the opposite. The window's `ORDER BY` defines what "previous" means.
Per-group day-over-day: add `PARTITION BY tenant_id` to reset the window per tenant. Without it, the lag bleeds across tenants and gives wrong answers - a classic interview gotcha.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
Read off this problem's own test suite, so these are the cases a submission actually has to survive.
These apply to the pattern as a whole, not just this problem.