0% found this document useful (0 votes)
206 views

SQL Conditions Aggregate Functions: "Having Clauses"

The HAVING clause allows SQL SELECT statements to filter group rows based on aggregate functions. It was added to SQL because the WHERE clause cannot be used with aggregate functions. The HAVING clause filters at the group level after the GROUP BY clause has grouped the rows. It is used to view conditions formed by the GROUP BY clause and return groups where aggregate values meet specified conditions.

Uploaded by

shen shen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
206 views

SQL Conditions Aggregate Functions: "Having Clauses"

The HAVING clause allows SQL SELECT statements to filter group rows based on aggregate functions. It was added to SQL because the WHERE clause cannot be used with aggregate functions. The HAVING clause filters at the group level after the GROUP BY clause has grouped the rows. It is used to view conditions formed by the GROUP BY clause and return groups where aggregate values meet specified conditions.

Uploaded by

shen shen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

“Having Clauses”

Definition:

A HAVING clause in SQL specifies that an SQL SELECT statement should only return rows where
aggregate values meet the specified conditions. It was added to the SQL language because the
WHERE keyword could not be used with aggregate functions.

The HAVING clause filters the data on the group row but not on the individual row.

To view the present condition formed by the GROUP BY clause, the HAVING clause is used.
(Wikipedia.com).

The HAVING clause was added to SQL because the WHERE keyword could not be used with
aggregate functions.(w3School.com).

Template

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

Example:

To return a list of department IDs whose total sales exceeded $1000 on the date of January 1,
2000, along with the sum of their sales on that date:

SELECT DeptID, SUM(SaleAmount)


FROM Sales
WHERE SaleDate = '01-Jan-2000'
GROUP BY DeptID
HAVING SUM(SaleAmount) > 1000

Referring to the sample tables in the Join example, the following query will return the list of
departments which have more than 1 employee:

SELECT DepartmentName, COUNT(*)


FROM Employee, Department
WHERE Employee.DepartmentID = Department.DepartmentID
GROUP BY DepartmentName
HAVING COUNT(*)>1;
HAVING is convenient, but not necessary. Code equivalent to the example above, but without
using HAVING, might look like:

SELECT * FROM (
SELECT DepartmentName AS deptNam, COUNT(*) AS empCnt
FROM Employee AS emp, Department AS dept
WHERE emp.DepartmentID = dept.DepartmentID
GROUP BY deptNam
) AS grp
WHERE grp.empCnt > 1;

References:

https://en.wikipedia.org/wiki/Having_(SQL)

https://www.w3schools.com/sql/sql_having.asp
“Multiple Row Operators”

Definition:

Multiple row subquery returns one or more rows to the outer SQL statement. You may use the
IN, ANY, or ALL operator in outer query to handle a subquery that returns multiple rows.

You might also like