TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2845 425
SQL Interview Series โ€” Part 12

๐Ÿ“Œ Question 12: Find the Department with the Highest Average Salary

Suppose you have an Employee table:

employee_id | employee_name | department | salary
------------|---------------|------------|-------
1 | Amit | IT | 60000
2 | Rahul | IT | 80000
3 | Priya | HR | 70000
4 | Neha | HR | 90000
5 | Raj | Finance | 85000
6 | Karan | Finance | 95000


โ“ Find the department that has the highest average salary.

๐Ÿ’ก Approach

First, calculate the average salary for every department.

Then, find the department with the highest average.

Department averages:

IT โ†’ (60000 + 80000) / 2 = 70000

HR โ†’ (70000 + 90000) / 2 = 80000

Finance โ†’ (85000 + 95000) / 2 = 90000

Therefore:

Finance โ†’ 90000

๐Ÿ“Œ SQL Solution

SELECT
department,
AVG(salary) AS avg_salary
FROM Employee
GROUP BY department
ORDER BY avg_salary DESC
LIMIT 1;


Expected result:

department | avg_salary
-----------|-----------
Finance | 90000


๐Ÿ”Ž How does it work?

1๏ธโƒฃ "GROUP BY department"

Creates one group for each department.

2๏ธโƒฃ "AVG(salary)"

Calculates the average salary within each department.

3๏ธโƒฃ "ORDER BY avg_salary DESC"

Places the department with the highest average first.

4๏ธโƒฃ "LIMIT 1"

Returns only the top department.

โš ๏ธ Interview Trap

What if two departments have the same highest average salary?

For example:

IT โ†’ 80000

HR โ†’ 80000

Finance โ†’ 75000

"LIMIT 1" would return only one department.

If the interviewer asks:

๐Ÿ‘‰ "Find all departments having the highest average salary"

then use a ranking approach:

SELECT department, avg_salary
FROM (
SELECT
department,
AVG(salary) AS avg_salary,
DENSE_RANK() OVER (
ORDER BY AVG(salary) DESC
) AS rnk
FROM Employee
GROUP BY department
) d
WHERE rnk = 1;


Now both IT and HR would be returned.

๐ŸŽฏ Double Tap โค๏ธ For More
  • โค 7
  • ๐Ÿ‘ 1
More from @sqlanalyst
  1. Oct 17, 2026๐—œ๐—•๐—  ๐—™๐—ฅ๐—˜๐—˜ ๐—ง๐—˜๐—–๐—› ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ! ๐ŸŽ“ Explore IBM Cognitive Class and buildโ€ฆ
  2. Oct 16, 2026๐——๐—ฎ๐˜๐—ฎ ๐˜€๐—ฐ๐—ถ๐—ฒ๐—ป๐—ฐ๐—ฒ ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—ช๐—ถ๐˜๐—ต ๐—”๐—œ๐Ÿ˜ Future Ready Data Scienโ€ฆ
  3. Oct 16, 2026SQL Interview Series โ€” Part 11 ๐Ÿ“Œ Question 11: Calculate a Running Total of Sales Supposeโ€ฆ
  4. Oct 13, 2026SQL Interview Series โ€” Part 8 ๐Ÿ“Œ Question 8: Find Customers Who Have Never Placed an Orderโ€ฆ
  5. Oct 13, 2026๐—ง๐—ข๐—ฃ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐—ง๐—ข ๐—•๐—ข๐—ข๐—ฆ๐—ง ๐—ฌ๐—ข๐—จ๐—ฅ ๐—ฆ๐—ž๐—œ๐—Ÿ๐—Ÿ๐—ฆ ๐—œ๐—ก ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ! Explorโ€ฆ
  6. Oct 12, 2026SQL Interview Series โ€” Part 6 ๐Ÿ“Œ Question 6: Find the Top 2 Highest-Paid Employees in Eachโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’