12. GROUP BY: One Row Per Group
12.6 Practice: Tickets by Agent and Status
Why. Ticket boards are natural GROUP BY stories.
What. Group interview_mini tickets by agent and by status.
How. One key, then two keys, with clear aliases.
Steps
- Count by agent.
- Count by status.
- Count by agent + status.
Examples
Example 1 · dataset: interview_mini
SELECT agent, COUNT(*) AS tickets
FROM tickets
GROUP BY agent
ORDER BY tickets DESC;Output
| agent | tickets |
|---|---|
| Elena | 3 |
| Sofia | 1 |
| Priya | 1 |
| Omar | 1 |
| James | 1 |
| Chen | 1 |
Meaning. Workload by agent.
Example 2 · dataset: interview_mini
SELECT agent, status, COUNT(*) AS n
FROM tickets
GROUP BY agent, status
ORDER BY agent, status;Output
| agent | status | n |
|---|---|---|
| Chen | Closed | 1 |
| Elena | Closed | 1 |
| Elena | Open | 2 |
| James | Open | 1 |
| Omar | Open | 1 |
| Priya | Open | 1 |
| Sofia | Closed | 1 |
Meaning. Agent × status grid in SQL form.
Ravindra Bagale's Tip
Chala mitrano — rebuild the agent×status query from memory tomorrow.
Ravindra Bagale's Tip – मराठी
चला मित्रांनो — उद्या स्मरणातून agent×status क्वेरी पुन्हा लिहा.
Ravindra Bagale's Tip – हिंदी
चलो दोस्तों — कल याद से agent×status क्वेरी फिर लिखें।