You are on page 1of 1

Difference b/w WHERE and HAVING in SQL:

1. WHERE clause is used while fetching data (rows) from table, and data which do
esn't pass the condition will not be fetched into result set, on the other hand
HAVING clause is later used to filter summarized data or grouped data. In short
if both WHERE and HAVING clause is used in a SELECT query with aggregate functio
n or GROUP BY clause, it will execute before HAVING clause.
When WHERE and HAVING clause are used together in a SELECT query with aggregate
function, WHERE clause is applied first on individual rows and only rows which
pass the condition is included for creating groups. Once group is created, HAVIN
G clause is used to filter groups based upon condition specified.
So, WHERE clause is used for filtering rows and it applies on each and every row
, while HAVING clause is used to filter groups in SQL.
2. Apart from SELECT queries, you can use WHERE clause with UPDATE and DELETE cl
ause
HAVING clause can only be used with SELECT query
3. One syntax level difference between WHERE and HAVING clause is that, former i
s used before GROUP BY clause, while later is used after GROUP BY clause.
Questions: http://java67.blogspot.sg/2013/04/10-frequently-asked-sql-query-inter
view-questions-answers-database.html
Question 2: SQL Query to find Max Salary from each department.
Answer :
SELECT DeptID, MAX(Salary) FROM Employee GROUP BY DeptID.
Question 5: Write a SQL Query to print the name of distinct employee whose DOB i
s between 01/01/1960 to 31/12/1975.
Ans:
SELECT DISTINCT EmpName FROM Employees WHERE DOB BETWEEN `01/01/1960' AND `31/12/1975';
Question 6:Write an SQL Query find number of employees according to gender whos
e DOB is between 01/01/1960 to 31/12/1975.
Answer : SELECT COUNT(*), sex from Employees WHERE DOB BETWEEN `01/01/1960 ' AND
`31/12/1975' GROUP BY sex;
Question 8:Write an SQL Query to find name of employee whose name Start with `M'
Ans: SELECT * FROM Employees WHERE EmpName like 'M%';

You might also like