An analyst needs a T-SQL query against a Warehouse table of inter-branch loan records that reports only the branches that have sent out more than 20 loans this year, along with each qualifying branch's total loan count. Which clause is required, in addition to GROUP BY, to filter on the aggregated count itself?
Select an answer to reveal the explanation.
Short Explanation
Think of GROUP BY as tallying votes by district and HAVING as the rule that only announces districts where the tally cleared a threshold. HAVING is the filter that runs after the counting is done, since you can't check a total before it exists.
Full Explanation
HAVING filters groups after aggregation has already produced a value like COUNT() per branch, which is exactly what's needed to keep only branches whose total loan count exceeds 20 — the aggregate has to exist before it can be compared to a threshold. GROUP BY alone only creates the per-branch buckets and computes the count; it has no filtering behavior of its own; without a HAVING clause, every branch's count would appear regardless of size. WHERE is evaluated before grouping and aggregation happen, operating on individual rows, so it cannot reference an aggregate function like COUNT() that doesn't exist yet at that stage of query processing — most SQL engines will reject that syntax outright. ORDER BY only controls the sequence in which the final rows are returned; sorting branches by loan count doesn't remove any branch from the result set, so branches with 20 or fewer loans would still show up, just lower in the list. Before running this in production, confirm whether “more than 20” should be a strict greater-than or an inclusive threshold, since HAVING count() > 20 and >= 20 return different branch lists.