The WHERE clause is used to filter the rows of the query results (for individual rows), while the HAVING clause is used to filter the groups produced by the GROUP BY clause (for the aggregated values in the group).
The difference between the WHERE clause and the HAVING clause in SQL
The WHERE clause and the HAVING clause are both Conditions used in SQL to filter data, but they apply to different data levels:
WHERE clause
HAVING clause
Example
WHERE clause:
<code class="sql">SELECT * FROM customers WHERE age > 25;</code>
This query will return all customer rows with an age greater than 25.
HAVING clause:
<code class="sql">SELECT region, COUNT(*) AS total_orders FROM orders GROUP BY region HAVING total_orders > 100;</code>
This query will return zone groups where the total number of orders exceeds 100.
The above is the detailed content of The difference between where and having in sql. For more information, please follow other related articles on the PHP Chinese website!