Skip to content

Top two agents per team by satisfaction, with tie-breakers

MediumFreeRanking and top NGroup and aggregate15 min2 hidden datasets

The task

The support lead wants to recognise the two happiest-making agents in each team. Use the surveys in satisfaction joined to their ticket's agent_id. An agent is eligible with at least 15 survey answers in total. Tickets with no agent or no survey are not counted.

Rank the eligible agents inside their team by exact average score, highest first. If two agents have the same average, the one with more survey answers comes first. If they are still tied, the lower agent_id comes first. Everyone gets a distinct position, and the top 2 of each team are returned. A team with fewer than two eligible agents returns fewer rows.

Return team, full_name, surveys (the number of survey answers), avg_score (the average score rounded to 2 decimal places) and team_position (1 or 2). Sort by team ascending, then team_position ascending.

Expected output

teamfull_namesurveysavg_scoreteam_position
  • Sorted by team ascending, then team_position ascending.
  • Numbers rounded to 2 decimal places.
  • Checked on the sample and on 2 hidden datasets. How grading works

Tables Customer support desk · click a name to insert it

agents14 rows in the sample

Support agents. Key: agent_id.

intUnique id.
textName.
textbilling, technical or general.
dateStart day.
tickets900 rows in the sample

One row per ticket. Key: ticket_id.

intUnique id.
intWho opened it.
intAssigned agent. NULL if never assigned.
textemail, chat or phone.
textlow, normal, high or urgent.
textbilling, bug, how_to or account.
timestampOpened (UTC).
timestampFirst reply from an agent. NULL if none yet.
timestampClosed as solved. NULL while open.
satisfaction366 rows in the sample

Survey answers, at most one per ticket. Key: ticket_id.

intThe ticket.
int1 (very unhappy) to 5 (very happy).
timestampWhen answered (UTC).

Related questions

Loading the editor and PostgreSQL