Another thing people may want to do is to limit the output based on the corresponding sum (or any other aggregate functions). For example, we might want to see only the stores with sales over $1,500. Instead of using the WHERE clause in the SQL statement, though, we need to use the HAVING clause, which is reserved for aggregate functions. The HAVING clause is typically placed near the end of the SQL statement, and a SQL statement with the HAVING clause may or may not include the GROUP BY clause. The syntax for HAVING is,
SELECT ["column_name1"], Function("column_name2")
[GROUP BY "column_name1"]
HAVING (arithmetic function condition);
Note: We may select zero, one, or more columns in addition to the aggregate function. If we select zero column, there is no need for the GROUP BY clause.
In our example, table Store_Information,
we would type,
SELECT Store_Name, SUM(Sales)
GROUP BY Store_Name
HAVING SUM(Sales) > 1500;
Next: SQL Alias
|| Copyright © 2013 1keydata.com All Rights Reserved.