The Data Management Center

SQL HAVING


David Haertzen David Haertzen, Tutorial Author
Check out our
Data Warehousing Tutorial.

HOME | SQL OVERVIEW | SQL BASICS | SQL ADMIN | SQL ADVANCED | SQL SYNTAX | Bookmark and Share
Previous | Next

SQL > SQL Advanced > SQL HAVING


What is SQL HAVING?

The SQL HAVING is a clause that enables conditions at the aggregate level. It is used instead of the WHERE clause when Aggregate Functions are used.  

 

The SQL HAVING clause supports the same operators as the WHERE clause. Grouped results can be checked using this clause.

Why Use SQL HAVING?

The HAVING clause is used to SELECT information based aggregate information. For example, one may want to list sales representatives that have sales totaling over $10,000. This is very useful for analytical reporting.


How To Use SQL HAVING

SQL HAVING is used as follows.

SQL HAVING Syntax

 
SELECT <column_name1>, <column_name2> <aggregate_function>
FROM <table_name>
GROUP BY <column_name1>, <column_name2>
HAVING <having_condition>
 

SQL HAVING Example

The following example regions from the branch table that have total employee_count greater than 5.

Here are the contents of the table:

Table: BRANCH
branch_nbrbranch_nameregion_nbremployee_count
108New York10010
110Boston1006
212Chicago2005
404San Diego4006
415San Jose4003

This SQL Statement with HAVING is executed:

 
SELECT region_nbr, sum(employee_count)
FROM branch
GROUP BY region_nbr
HAVING sum(employee_count) > 5
ORDER BY region_nbr
 

Here is the result.

region_nbrsum(employee_count)
10016
4009

HOME | SQL OVERVIEW | SQL BASICS | SQL ADMINISTRATION | SQL ADVANCED | SQL SYNTAX
infogoal.com HOME
SQL OVERVIEW
SQL SYNTAX
SQL BOOKREVIEWS
SQL BASICS
SQL SELECT
SQL WHERE
SQL INSERT
SQL UPDATE
SQL DELETE
SQL ADMINISTRATION
SQL CREATE DATABASE
SQL DROP DATABASE
SQL CREATE TABLE
SQL ALTER TABLE
SQL DROP TABLE
SQL CREATE INDEX
SQL DROP INDEX
SQL ADD FOREIGN KEY
SQL DROP FOREIGN KEY
SQL CREATE VIEW
SQL DROP VIEW
SQL ADVANCED
SQL CONCAT
SQL SUBSTRING
SQL TRIM
SQL AND & OR
SQL IN
SQL BETWEEN
SQL LIKE
SQL DISTINCT
SQL GROUP BY
SQL AGGREGATE
SQL HAVING
SQL ORDER BY
SQL JOIN
SQL OUTER JOIN

Copyright© 1999-2014, First Place Software, Inc.