Filter groups with HAVING
Apply a condition to aggregated groups with HAVING.
What you are trying to do
A queue review should list teams with at least two open tickets.
Work through it
- Use WHERE to keep open tickets before grouping.
- Group the remaining rows by Team.
- Use HAVING to keep groups whose count is at least two.
Check your work
Only Care appears, with two open tickets. South has one and does not meet the HAVING condition.
Practice SQL
Open in SQL FlowSELECT Team, COUNT(*) AS OpenTickets
FROM (VALUES (1, N'Care', N'Open'), (2, N'Care', N'Open'), (3, N'Care', N'Closed'), (4, N'South', N'Open')) AS Ticket(TicketId, Team, State)
WHERE State = N'Open'
GROUP BY Team
HAVING COUNT(*) >= 2
ORDER BY Team;
Knowledge check
Commands in this lesson
Next lesson Combine compatible result sets