What is the difference between where and having clause

Where and Having both are used for filter purpose. Then what is the difference between them in sql statement.

The below table will summarize the difference between them.


WHERE HAVING
WHERE Clause is used to filter the records from the table or used while joining more than one table.Only those records will be extracted who are satisfying the specified condition in WHERE clause. HAVING Clause is used to filter the records from the groups based on the given condition in the HAVING Clause. Those groups who will satisfy the given condition will appear in the final result.
WHERE Clause can be used without GROUP BY Clause. HAVING Clause cannot be used without GROUP BY Clause.
WHERE Clause implements in row operations. HAVING Clause implements in column operation.
WHERE Clause cannot contain aggregate function. HAVING Clause can contain aggregate function.
WHERE Clause can be used with SELECT, UPDATE, DELETE statement. HAVING Clause can only be used with SELECT statement.
WHERE Clause is used before GROUP BY Clause. HAVING Clause is used after GROUP BY Clause.
WHERE Clause is used with single row function like UPPER, LOWER etc. HAVING Clause is used with multiple row function like SUM, COUNT etc.

As a simple example.


WHERE age >= 25

GROUP BY student_id
HAVING SUM(score) > 100

Comments

Back To Top

Popular posts from this blog

How to save video from Internet Explorer

error 18 at 0 depth lookup: self signed certificate

How to check fragmentation in MySQL tables