The difference between the having and where clause in SQL is that the where clause cannot be used with aggregates, but the having clause can.
The where clause works on row’s data, not on aggregated data. Let us consider below table ‘Marks’.
Student Course Score
a c1 40
a c2 50
b c3 60
d c1 70
e c2 80
Consider the query
This would select data row by row basis.
The having clause works on aggregated data.
For example, output of below query
When we apply having in above query, we get
Note: It is not a predefined rule but in a good number of the SQL queries, we use WHERE prior to GROUP BY and HAVING after GROUP BY. The Where clause acts as a pre filter where as Having as a post filter.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- SQL | WITH clause
- SQL | WHERE Clause
- SQL | SELECT TOP Clause
- SQL | Union Clause
- SQL | Distinct Clause
- MySQL | PARTITION BY Clause
- Combining aggregate and non-aggregate values in SQL using Joins and Over clause
- Difference between Relational Algebra and Relational Calculus
- Difference between Strong and Weak Entity
- Difference between Super Key and Candidate Key
- Difference between Normalization and Denormalization
- Difference between Primary and Candidate Key
- DBMS | Domain Relational Calculus
- Security Management System