e01.org

Having (SQL)

A HAVING clause in SQL specifies that an SQL SELECT statement must only return rows where aggregate values meet the specified conditions.

A HAVING clause in SQL specifies that an SQL SELECT statement must only return rows where aggregate values meet the specified conditions. SELECT DeptID, SUM(SaleAmount) FROM Sales WHERE SaleDate = '2000-01-01' 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 JOIN Department ON 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 empCount FROM Employee AS emp JOIN Department AS dept ON emp.DepartmentID = dept.DepartmentID GROUP BY deptNam ) AS grp WHERE grp.empCount > 1; ==External links== The HAVING and GROUP BY SQL clauses SQL Aggregate Functions

source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"Having (SQL)". 1766333311. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1328745217

JSON record · API collection · citation policy