T-SQL reference › HAVING

HAVING in SQL Server: filter after the total

HAVING is WHERE for groups. It runs after GROUP BY. That is the only reason it exists. Longer version: <a href="/guides/where-vs-having">WHERE vs HAVING</a>.

Over £1,000

SELECT customer_id, SUM(total_amount) AS spent
FROM Orders
WHERE status <> 'Cancelled'
GROUP BY customer_id
HAVING SUM(total_amount) > 1000
ORDER BY spent DESC;

Try it on Harbour & Co

James Rowe (Sales Manager) needs this now: “Christmas hampers go to anyone who’s spent more than £1,000 with us. Who qualifies?” Write the query, run it, send the results. On the paid plan.

Take the ticket →

Learn it on a real ticket

Monday is free on WorkSQL. A colleague’s ticket lands, you write the T-SQL, we mark it. No videos. Five minutes.

Clock in free See Monday’s tickets