Saturday 9 November 2013

Difference between having and where clause in SQL

           Difference between having and where clause in SQL


It is very important to know the difference between Having and Where in order to get the desired results from the query

WHERE clause:
  • can be used with Select,Insert,Delete statements
  • can't use aggregate functions in where clause
  • Where clause filter results before the results are Grouped as Where clause can only be written before Group clause 
  • Where clause applies to rows
Example:

Select fname,country_name from person
where country_name in ('US','UK')

HAVING clause:
  • can be used with Select statement only
  • can use aggregate functions in Having clause
  • Having clause filters results after the results are grouped as having clause can be used after Group clause only
  • Having clause applies to groups
Example:

Select fname,max(sal) from person
group by fname having max(sal) > 10000


Similar Posts:  CTE vs VIEW, TEMP TABLE vs CTE

No comments:

Post a Comment