Sunday, September 18, 2022

SQL Series - WHERE Clause vs HAVING clause

Difference between WHERE and HAVING clause in SQL


As a part SQL series, we are going to see the difference between WHERE and HAVING clauses and how they work in this post.


What are WHERE and HAVING clauses?

Both are part of SQL, which are used mainly to filter out the records of the table.


What condition can use them?

In some cases, you need to filter out the individual records. In such cases, you can use WHERE Clause, Whereas in other cases you need to filter the groups with the specific condition. In such cases, you can use HAVING Clause.


How do them work?

📌WHERE Clause filters the records tuple by tuple while HAVING Clause filters the whole group.A query may have both the clauses( WHERE and HAVING Clause)


What order can we use them?

📌Where Clause applied first and then Having Clause.


on which data do they apply?

📌WHERE Clause restricts records before GROUP BY Clause, whereas HAVING Clause restricts groups after GROUP BY Clause are performed. i.c WHERE Clause is applied on non-aggregated data where as Having clause is applied on aggregated data.


Where can we use them?

📌WHERE Clause can be utilized with SELECT, UPDATE, DELETE, and INSERT, whereas HAVING can be utilized only with SELECT statement.




No comments:

Post a Comment

Spark- Window Function

  Window functions in Spark ================================================ -> Spark Window functions operate on a group of rows like pa...