← All SQL interview questions
Rank support agents within their team
MediumWindow FunctionsEXLRSystemsSwiggy
A support operations lead publishes a leaderboard per team without losing the individual rows.
From agent_performance, return agent_name, team, tickets_closed and team_rank, where team_rank is the agent's position within their own team ordered by tickets_closed descending. Agents tied on volume must share the same rank, and the next rank should not be skipped.
Sample data
Table: agent_performance
15 row(s) · showing first 8
| agent_idINT | agent_nameTEXT | teamTEXT | tickets_closedINT |
|---|---|---|---|
| 1 | Ishita Kar | Billing | 182 |
| 2 | Rehan Sheikh | Billing | 204 |
| 3 | Vidya Rao | Billing | 182 |
| 4 | Sanjay Mahto | Billing | 150 |
| 5 | Aparna Salvi | Onboarding | 221 |
| 6 | Deepak Rawat | Onboarding | 198 |
| 7 | Noor Fatima | Onboarding | 221 |
| 8 | Girish Kamath | Onboarding | 176 |
agent_performance
- agent_idINT
- agent_nameTEXT
- teamTEXT
- tickets_closedINT
- Step 1: The requirement keeps every agent row while adding a per-team ranking, which is exactly what a window function does — unlike `GROUP BY`, it does not collapse rows.
- Step 2: `PARTITION BY team` restarts the numbering for each team.
- Step 3: `ORDER BY tickets_closed DESC` decides the ranking direction inside each partition.
- Step 4: Choose `DENSE_RANK()` because ties must share a rank **and** the following rank must not be skipped. `RANK()` would jump (1,1,3) and `ROW_NUMBER()` would break ties arbitrarily.
- Step 5: Alias the window expression as `team_rank`.
SELECT agent_name,
team,
tickets_closed,
DENSE_RANK() OVER (
PARTITION BY team
ORDER BY tickets_closed DESC
) AS team_rank
FROM agent_performance;Learn the concept first
Stuck on Window Functions? These free SQL lessons teach it step by step.
More Window Functions questions
Practising alone only goes so far — our live cohorts pair SQL depth with dashboards, agentic analytics projects and mock interviews.
Explore the program