Learn With Grito

SQL HAVING Clause
Explained for Beginners

Filtering data is essential in SQL. But there are two very different kinds of filtering: filtering individual rows, and filtering grouped results.

Tutorial Series8 Mins ReadSQL Level 15

Filtering data is essential in SQL. But there are two very different kinds of filtering: filtering individual rows, and filtering grouped results.

That second case is where the SQL HAVING clause comes in. If WHERE filters raw rows, HAVING filters aggregated groups.

This is one of the most commonly misunderstood SQL concepts for beginners. And one of the most common SQL interview topics. If you are learning SQL for data analysis, HAVING matters because real business analysis often needs filtering after summarization.

Examples:

  • cities with revenue above ₹1 lakh
  • products with more than 100 orders
  • campaigns with conversion rate above 5%
  • subscription plans with fewer than 500 users
  • countries with high-value customers only

In this guide, you’ll learn:

  • what the SQL HAVING clause is
  • HAVING syntax
  • HAVING with aggregate functions
  • WHERE vs HAVING
  • HAVING with GROUP BY
  • real analyst examples
  • common mistakes
  • interview questions

This is where SQL grouping becomes truly powerful.

What Is the SQL HAVING Clause?

The SQL HAVING clause filters grouped results. It is used after GROUP BY.

Example
SELECT city, COUNT(*)
FROM customers
GROUP BY city
HAVING COUNT(*) > 100;

Meaning:

  • group customers by city
  • count customers in each city
  • keep only cities with more than 100 customers

Simple idea: HAVING filters groups, not individual rows.

Why SQL HAVING Matters for Data Analysts

Analysts constantly work with summarized metrics. Raw rows are often not enough.

Real questions:

  • Revenue Analysis: Which cities generated more than ₹1 lakh?
  • Customer Analysis: Which countries have more than 1,000 customers?
  • Product Analysis: Which categories sold more than 500 units?
  • Marketing Analysis: Which campaigns generated more than ₹50,000?

These require filtering after aggregation. That is exactly what HAVING does.

Basic SQL HAVING Syntax

Syntax
SELECT grouped_column, aggregate_function(column_name)
FROM table_name
GROUP BY grouped_column
HAVING aggregate_condition;

HAVING with COUNT(), SUM(), AVG(), MIN(), MAX()

Aggregates
-- Cities with >10 customers
SELECT city, COUNT(*)
FROM customers
GROUP BY city
HAVING COUNT(*) > 10;

-- High-revenue cities
SELECT city, SUM(revenue)
FROM orders
GROUP BY city
HAVING SUM(revenue) > 50000;

-- Cities with high average order value
SELECT city, AVG(order_value)
FROM orders
GROUP BY city
HAVING AVG(order_value) > 2000;

HAVING with Multiple Conditions

You can combine logic using AND or OR.

Multiple Conditions
SELECT city, SUM(revenue), COUNT(*)
FROM orders
GROUP BY city
HAVING SUM(revenue) > 50000
AND COUNT(*) > 20;

HAVING with WHERE

This is extremely common.

WHERE and HAVING
SELECT city, SUM(revenue)
FROM orders
WHERE status = 'Completed'
GROUP BY city
HAVING SUM(revenue) > 100000;

Execution:

  • Step 1: WHERE filters rows: Only completed orders.
  • Step 2: GROUP BY groups remaining rows.
  • Step 3: HAVING filters aggregated groups.

WHERE vs HAVING (Critical Section)

This is the biggest beginner confusion.

WHERE

Filters individual rows. WHERE revenue > 1000 means remove low-value rows before grouping.

HAVING

Filters grouped summaries. HAVING SUM(revenue) > 50000 means remove low-value groups after aggregation.

SQL Execution Order Connection Logical order: FROM → JOIN → WHERE → GROUP BY → HAVING → SELECT → ORDER BY. HAVING happens after grouping. That is why aggregate functions work here (HAVING COUNT(*) > 10) but not in WHERE.

Real Analyst Examples

Common queries:

  • High-Revenue Cities: HAVING SUM(revenue) > 100000;
  • High-Volume Products: HAVING COUNT(*) > 100;
  • Strong Campaigns: HAVING SUM(revenue) > 50000;
  • Valuable Customer Segments: HAVING AVG(lifetime_value) > 25000;

The Grito Factor Many SQL beginners think HAVING is “just another WHERE.” It is not. That misunderstanding alone causes a shocking number of failed interview answers—and broken analyst queries.

Common Beginner Mistakes

1. Using Aggregate Functions in WHERE

WHERE SUM(revenue) > 50000 is wrong. Correct: HAVING SUM(revenue) > 50000.

2. Confusing WHERE and HAVING

Remember: WHERE = rows, HAVING = groups.

3. Forgetting GROUP BY

SELECT city, COUNT(*) FROM customers HAVING COUNT(*) > 10; is bad. HAVING usually belongs with grouped queries.

4. Using HAVING When WHERE Is Better

HAVING city = 'Mumbai' is inefficient. Better: WHERE city = 'Mumbai'. Filter rows early when possible.

Practice Questions

  1. Cities with more than 100 customers.
  2. Products with revenue above ₹50,000.
  3. Cities with average salary above ₹60,000.

What Comes Next?

Now that you can filter grouped data, the next step is removing duplicates. Next: SQL DISTINCT Explained. Because analysts often need unique values.

Final Thoughts

SQL HAVING is one of the most important SQL concepts for analysts.

It helps you:

  • filter grouped summaries
  • analyze business segments
  • isolate top performers
  • build meaningful reports
  • answer real analytical questions

Master HAVING early. It appears everywhere in practical SQL.

Grit Over Excuses.

— The Grito Team