HAVING

SQL · Grouping Rows

Theory

HAVING filters groups after GROUP BY has built them and the aggregates are computed. WHERE asks "should this row be counted?" while HAVING asks "should this group be shown?". GROUP BY dept HAVING COUNT(*) > 1 keeps only departments with more than one employee.

Because HAVING runs after grouping, it can use aggregate functions like COUNT, SUM and AVG, which WHERE cannot. A query can use both: WHERE city = 'Jaipur' first removes rows, then GROUP BY forms groups from what remains, then HAVING SUM(amount) > 10000 removes small groups. Conditions that do not involve aggregates belong in WHERE, because filtering early means less data to group. Most engines do not let HAVING use SELECT aliases, so repeat the aggregate expression.

Why we use it

Many questions are about groups that meet a threshold: customers with more than five orders, cities with sales above a target, departments with an average salary over ₹60,000. HAVING filters on those aggregate results.

When to use it

Use HAVING only for conditions on aggregates. Put plain row conditions in WHERE so they run before grouping. Without GROUP BY, HAVING treats the whole table as one group, which is rarely what you want.

How it works

  1. FROM and WHERE produce the filtered rows
  2. GROUP BY forms the groups
  3. Compute aggregates for each group
  4. Does the group satisfy the HAVING condition? Yes: Keep the group. No: Discard the group.
  5. SELECT and ORDER BY the kept groups

Syntax

SELECT column, AGG(column)
FROM table
GROUP BY column
HAVING AGG(column) condition;

Example

SELECT department, COUNT(*) FROM employees
GROUP BY department HAVING COUNT(*) > 1;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic