bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/SQL/SQL Tutorial
SQL•SQL Tutorial

SQL HAVING Clause

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind SQL HAVING Clause?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___, aggregate_function(column2), column3, ...
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Unlike the WHERE clause, which filters individual rows before grouping, the HAVING clause filters groups after the aggregation has been performed.
The HAVING clause is used to filter the results of a GROUP BY query based on aggregate functions.
The SQL HAVING Clause

The SQL HAVING Clause

The HAVING clause is used to filter the results of a GROUP BY query based on aggregate functions.

Unlike the WHERE clause, which filters individual rows before grouping, the HAVING clause filters groups after the aggregation has been performed.

HAVING Syntax

SELECT
column1, aggregate_function(column2), column3, ...
FROM
table_name
WHERE
condition
GROUP BY
column1
,
column3
HAVING
condition
 -- The condition on grouped data
ORDER BY
column_name
;

Demo Database

Below is a selection from the "Customers" table in the Northwind sample database:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico
4Around the HornThomas Hardy120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpChristina BerglundBerguvsvägen 8LuleåS-958 22Sweden

Demo Database

Below is a selection from the "Orders" table in the Northwind sample database:

OrderIDCustomerIDEmployeeIDOrderDateShipperID
102489051996-07-043
102498161996-07-051
102503441996-07-082

And a selection from the "Employees" table:

EmployeeIDLastNameFirstNameBirthDatePhotoNotes
1DavolioNancy1968-12-08EmpID1.picEducation includes a BA....
2FullerAndrew1952-02-19EmpID2.picAndrew received his BTS....
3LeverlingJanet1963-08-30EmpID3.picJanet has a BS degree....

Previous

SQL GROUP BY Statement

Next

SQL EXISTS Operator