GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Oracle  >  SQL
Go To First  |  Previous Question  |  Next Question 
 SQL  |  Question 16 of 171    Print  
When do you use WHERE clause and when do you use HAVING clause?
HAVING clause is used when you want to specify a condition for a group function and it is written after GROUP BY clause. The WHERE clause is used when you want to specify a condition for columns, single row functions except group functions and it is written before GROUP BY clause if it is used.



  
Total Answers and Comments: 8 Last Update: January 29, 2008   
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: rupaliss
 

Where Clause :- Used to filter the records from the table before group by cluse.

Having Clause :- Used to filter the grouped records after group By clause.



Above answer was rated as good by the following members:
satyam_Ora, checkout_rajesh, kishorebabu.yenumula
December 20, 2005 05:12:57   #1  
rupaliss Member Since: December 2005   Contribution: 1    

RE: When do you use WHERE clause and when do ...

Where Clause :- Used to filter the records from the table before group by cluse.

Having Clause :- Used to filter the grouped records after group By clause.


 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
March 29, 2006 05:55:28   #2  
Sunil        

RE: When do you use WHERE clause and when do ...
To Restrict the group by clause Having is used .
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
January 29, 2007 03:58:21   #3  
Rohan Deshpande        

RE: When do you use WHERE clause and when do ...
WHERE clause is used to restrict the rows.WHEREAS HAVING clause is used to restrict the groups.
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
June 19, 2007 02:41:50   #4  
saranya        

RE: When do you use WHERE clause and when do ...
When we have to group a number of records in the table using a specific field name say in the database of a bank we would wish to group the records based on their branch name we would use having clause.
When selecting i.e viewing the records in order to fetch tuples subjected to certain conditions where clause is used.

Thanx!

 
Is this answer useful? Yes | No
August 02, 2007 06:16:32   #5  
anil kumar        

RE: When do you use WHERE clause and when do ...
The WHERE clause is used to depend on the table while checking the condition..
AND
The HAVING clause is used where the data is having in the perticular table...while checking the condition...

 
Is this answer useful? Yes | No
August 03, 2007 08:58:19   #6  
LRANI Member Since: July 2007   Contribution: 6    

RE: When do you use WHERE clause and when do ...
WHERE and HAVING clause both restricts the data but WHERE clause used on the columns/expression whereas HAVING clause can be used on group of data.
For example : to find out employees who are working in deptno 30 from emp table:
SQL statement :
SELECT * FROM emp WHERE deptno 30;
will return the data.
SELECT * FROM emp HAVING deptno 30;
will give an error: "Not a GROUP BY expression"

 
Is this answer useful? Yes | No
November 12, 2007 08:09:47   #7  
deviji2000 Member Since: July 2007   Contribution: 11    

RE: When do you use WHERE clause and when do ...
When you want to mention any condition in the SQL statement we use WHERE Clause but you will get an error when you have group functions in your SQL statement and trying to include group functions in your WHERE clause.

To avoid the error you have to mention the group condition in HAVING clause.

example:

SELECT emp_no emp_name sal
FROM emp
WHERE sal >10000;

but you cannot use WHERE in

SELECT min(sal) deptno
FROM emp
WHERE min(sal) >10000
GROUP BY deptno;

the above statement raises error so replace 'WHERE' by 'HAVING' like

SELECT min(sal) deptno
FROM emp
GROUP BY deptno
HAVING min(sal)>10000;
this will work out.

bye!
viji

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
January 29, 2008 08:49:30   #8  
patilpravin_1981 Member Since: December 2007   Contribution: 29    

RE: When do you use WHERE clause and when do you use HAVING clause?
when query is based on condition we use where clause & there is no any aggregate function
when query is base on contion & aggregate function has to be use then we can use
having clause.

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape