InterviewPrepKit

Home / SQL / Aggregation & Grouping

Ticket Breakdown Per Agent

medium
Solving tips
  • To pivot categories into columns, aggregate a CASE expression: SUM(CASE WHEN cond THEN 1 ELSE 0 END).
  • COUNT(*) FILTER (WHERE cond) is the cleaner Postgres form of the same conditional count.
  • A boolean column can be counted directly with FILTER (WHERE flag) — no comparison needed.

Given a support ticket log, produce one row per agent that breaks their tickets down by priority and shows how many were resolved.

Schema

CREATE TABLE tickets (
  ticket_id INTEGER PRIMARY KEY,
  agent     TEXT NOT NULL,
  priority  TEXT NOT NULL,     -- 'high' or 'low'
  resolved  BOOLEAN NOT NULL
);

Sample data:

ticket_idagentpriorityresolved
1Annhightrue
2Annlowfalse
3Annhighfalse
4Boblowtrue
5Boblowtrue
6Carahightrue

Task

For each agent, return:

  • agent
  • high_count — number of high priority tickets
  • low_count — number of low priority tickets
  • resolved_count — number of resolved tickets

Order the result by agent ascending.

Expected output

agenthigh_countlow_countresolved_count
Ann211
Bob022
Cara101
Your workspace Not runnable by design — this is your interview scratchpad. Saved on this device.