Arrange the following SQL steps in correct order to retrieve the Max Salary from the 'Employee' table for each Dept showing only those Dept where Max Salary is greater than 50000.
(A) HAVING MAX(Salary)> 50000
(B) GROUP BY Dept
(C) SELECT Dept, MAX(Salary) FROM Employee
(D) ORDER BY MAX(Salary)
Arrange the following SQL steps in correct order to retrieve the Max Salary from the 'Employee' table for each Dept showing only those Dept where Max Salary is greater than 50000.
(A) HAVING MAX(Salary)> 50000
(B) GROUP BY Dept
(C) SELECT Dept, MAX(Salary) FROM Employee
(D) ORDER BY MAX(Salary)
Solution
The clauses of an SQL query must follow a fixed order: SELECT ... FROM, then WHERE, GROUP BY, HAVING and finally ORDER BY. HAVING filters the groups made by GROUP BY, so it must come after it. ORDER BY sorts the final result, so it comes last.
(C) SELECT Dept, MAX(Salary) FROM Employee
(B) GROUP BY Dept
(A) HAVING MAX(Salary) > 50000
(D) ORDER BY MAX(Salary)
Option 3: ORDER BY cannot come before HAVING, because sorting is the last step.
Options 1 and 4: HAVING cannot appear before GROUP BY, and a query cannot start without SELECT.
WHERE filters rows before grouping, while HAVING filters groups after grouping, which is why a condition on MAX(Salary) must use HAVING.
Related questions:
2025: 21 May Shift 1
2025: 30 May Shift 2
2026: 26 May Shift 1
2026: 21 May Shift 1