HAVING — Complete Guide
HAVING — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of SQL Server Tutorial on Toolliyo Academy.
On this page
SQL Server Tutorial · Lesson 15 of 100
HAVING
SQL basics → Queries → Advanced
SQL basics · 1 — SELECT · ~6 min · SQL — SQL Queries & Clauses
What is this?
HAVING filters groups after GROUP BY. WHERE filters rows before grouping; HAVING filters aggregated results.
Why should you care?
You want cities with revenue over 2000 — that condition needs SUM first, so HAVING applies.
See it live — copy this example
Run in SQL Server Management Studio (SSMS) or Azure Data Studio.
USE DataVerse;
SELECT City, SUM(Amount) AS Revenue, COUNT(*) AS OrderCount
FROM dbo.Orders
GROUP BY City
HAVING SUM(Amount) >= 2000
ORDER BY Revenue DESC;
What happened?
- Groups form by City, then HAVING drops cities whose SUM is under 2000.
- Pune’s 2000 total stays; adjust data if your numbers differ.
Practice next
- Run without HAVING and note all cities.
- Add HAVING COUNT(*) >= 2.
- Combine HAVING SUM(Amount) >= 2000 AND COUNT(*) >= 2.
- HAVING AVG(Amount) > 1000.
- HAVING MAX(Amount) >= 2500.
Remember
WHERE = row filter; HAVING = group filter. HAVING often uses aggregate expressions. Use the earliest filter that is correct.
Highlight high-revenue cities
Sales alert lists cities where DataVerse revenue crosses a threshold.
Outcome: Managers focus coaching where money is largest.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!