Skip to content
Worked Examples · Example 10

Q.Using the Employee table, write an SQL query to display only those departments whose average salary is greater than 29000. Explain why HAVING is used here instead of WHERE.

ChseodishaTextbookSubjectiveImportance★★★★★est
75% · 12/16 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

We first group the employees by department, then keep only the groups whose average salary exceeds 29000. Because the condition is on an aggregate value of the group (its average), it must be written in a HAVING clause, which is applied after the grouping:

SELECT Dept, AVG(Salary) AS AvgSalary
FROM Employee
GROUP BY Dept
HAVING AVG(Salary) > 29000;

For the sample data the department averages are Sales 27500, Accounts 30000 and IT 40000, so the result lists Accounts (30000) and IT (40000); Sales is dropped. …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.