What’s wrong in the following query?

SELECT Name, MaxSal = Max(Salary)
FROM tblEmployees
WHERE Max(Salary) > 5000
GROUP BY Name;

 Posted by Rajnilari2015 on 10/23/2016 | Category: Sql Server Interview questions | Views: 264 | Points: 40
Answer:

The query is wrong because we cannot use Aggregate Function for filtering in a WHERE clause. Instead we need to use HAVING. The correct query is

SELECT Name, MaxSal = Max(Salary) 

FROM tblEmployees
GROUP BY Name
HAVING Max(Salary) > 5000;


| Alert Moderator 

Comments or Responses

Login to post response