The WHERE clause filters rows in the SELECT statement and filters the underlying data based on row conditions. The HAVING clause filters groups in a GROUP BY statement, filtering groups based on their aggregate results. The main differences: WHERE processes individual rows and evaluates the conditions of the rows; HAVING processes groups and evaluates the aggregated results of the groups; WHERE affects which rows are included in the results, and HAVING affects which groups are included in the results.
WHERE and HAVING clauses are used in SQL
WHERE and HAVING clauses are used in SQL Filter datasets, but they differ in processing context and target:
#WHERE clause:
Example:
<code class="sql">SELECT * FROM employees WHERE salary > 50000;</code>
This query returns all employees with a salary greater than $50,000.
HAVING CLAUSE: The
Example:
<code class="sql">SELECT department, SUM(salary) AS total_salary FROM employees GROUP BY department HAVING total_salary > 100000;</code>
This query returns the total salary for all departments where the total salary is greater than $100,000.
Main differences:
The above is the detailed content of Usage of having and where in sql. For more information, please follow other related articles on the PHP Chinese website!