Home >Backend Development >PHP Tutorial >Usage of HAVING clause in SQL_PHP tutorial
HAVING
One problem that users may want to solve when using SQL language is to limit the output of sum or other aggregate function operation results. For example, we may only want to see information about stores with total sales of more than $1,500 in the Store_Information data table. In this case, we need to use the HAVING clause. The syntax format is:
SELECT "column_name1", SUM("column_name2")
FROM "table_name"
GROUP BY "column_name1"
HAVING (arithematic function condition)
(GROUP BY clause can Select)
Thus, we can use the following command to achieve the above query purpose:
SELECT store_name, SUM(sales)
FROM Store_Information
GROUP BY store_name
HAVING SUM(sales) > 1500
The query results are displayed as:
store_name SUM(Sales)
Los Angeles $1800
Minor note:
Use the HAVING clause instead of the WHERE clause when setting query conditions for aggregate functions in SQL language. Typically, the HAVING clause is placed at the end of the SQL command