Difference between Where and Having Clause in SQL
1. WHERE Clause:
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. It can be used with SELECT, UPDATE, DELETE statements.
Let us consider below table “Student”
Roll_no S_Name Age
1 a 17
2 b 20
3 c 21
4 d 18
5 e 20
6 f 17
7 g 21
8 h 17
Consider the Query:
SELECT S_Name, Age FROM Student
WHERE Age >=18
OUTPUT of above query is :
2. HAVING 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. HAVING Clause can only be used
with SELECT statement.
Let us consider Student table mentioned above and apply having clause on it:
SELECT Age, COUNT(Roll_No) AS No_of_Students
FROM Student GROUP BY Age
HAVING COUNT(Roll_No) > 1
OUTPUT of the above query is :
Difference between Where and Having Clause in SQL :
||WHERE Clause is used to filter the records from the table based on the specified condition.
||HAVING Clause is used to filter record from the groups based on the specified condition.
||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.
Level Up Your GATE Prep!
Embark on a transformative journey towards GATE success by choosing Data Science & AI
as your second paper choice with our specialized course. If you find yourself lost in the vast landscape of the GATE syllabus, our program is the compass you need.