Pages

11 August 2013

SQL- WHERE and HAVING

Q What is the difference between WHERE and HAVING clause ?
       WHERE clause is used to filter rows before aggregation or grouping of rows has been done. WHERE can be used in other than SELECT clause like UPDATE,DELETE etc. 
 Consider the below MarksInfo table for illustration


MarksInfo Table
In the below snapshot, the use of WHERE clause has been demonstrated. We are filtering the records based on condition in WHERE clause. Here, we are displaying only those records which have Marks greater than 80.



Use of WHERE clause



         HAVING clause is used to filter groups after aggregation has been done.It is mainly used with SELECT clause after GROUP BY clause.


In the below snapshot, the use of HAVING clause has been demonstrated.Firstly, the records has been grouped on the basis of Marks obtained and then count for each group is performed as number of students. After that, filter condition is applied on the groups. Here, we are displaying only those Marks groups which have count greater than 1.








No comments:

Post a Comment