SQL Flow Field Guide

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

  1. Use WHERE to keep open tickets before grouping.
  2. Group the remaining rows by Team.
  3. 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 Flow
SELECT 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