SQL practice problems
22 problems · 0 solved
SQL is the language for asking questions of data stored in tables. Even in ML teams, a large share of daily work is pulling numbers from logs, usage tables and experiment records. These problems use realistic data from LLM products, such as API calls, eval results and agent events, so the practice feels close to real work. You will total the tokens each model used on successful calls, find users whose spend is over a budget with HAVING, count failed training runs per project while keeping projects with zero failures, pick the best scoring model for each eval task with ROW_NUMBER, compute day-over-day percent change in cost per team with LAG, and split agent events into sessions by idle gap. Start with the Easy problems on GROUP BY and HAVING. Then do the outer join and ROW_NUMBER problems, and finish with LAG and sessionization, which use window functions in more involved ways. Most data and ML interviews include at least one SQL round, and window functions are where many candidates struggle, so steady practice here pays off quickly in both interviews and day to day analysis.
Easy 7
- Total the tokens each model used on successful API calls
- Find users whose total API spend is over a budget with HAVING
- Count how many different models each user called with COUNT DISTINCT
- Pick a final label from human and model labels with COALESCE
- Find training runs that were never evaluated with a LEFT JOIN anti-join
- Compute the prompt cache hit rate per model while ignoring unknown rows
- Show each team's share of total GPU hours with a window over all rows
Medium 8
- Count failed training runs per project, keeping projects with zero failures
- Pick the best scoring model for each eval task with ROW_NUMBER
- Pivot request statuses into columns with conditional aggregation
- Find eval tasks where a candidate model scores below the baseline with a self join
- Keep every run in the top two distinct scores per project with DENSE_RANK
- Keep the latest annotation per item, then count labels
- Measure how many days each model version stayed live with LEAD
- Find models slower than the overall average latency using CTEs
Hard 7
- Compute day-over-day percent change in API cost per team with LAGPro
- Split agent events into sessions by idle gap and report sessions per userPro
- Build a three-step signup funnel where steps must happen in orderPro
- Compute day-1 and day-7 retention per signup cohortPro
- Compute the median latency per model with window functionsPro
- Find the longest streak of consecutive failed runs per pipelinePro
- Find the day each user crossed a token quota with a running totalPro