Worked Examples · Example 9
Q.Using the Employee table, write an SQL query to display each department along with the number of employees in it and the average salary of the department.
ChseodishaTextbookSubjectiveImportance★★★★★est
69% · 11/16 Questions
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 →When a summary is wanted for each department separately, we use the GROUP BY clause to split the rows into groups sharing the same Dept, and then apply the aggregate functions to each group.
SELECT Dept, COUNT(*) AS NumEmployees, AVG(Salary) AS AvgSalary
FROM Employee
GROUP BY Dept;
The result has one row per department:
| Dept | NumEmployees | AvgSalary |
|---|---|---|
| Sales | 2 | 27500 |
| Accounts | 2 | 30000 |
| IT | 2 | 40000 |
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.