โ
SQL Interview Challenge! ๐๐ง
๐๐ป๐๐ฒ๐ฟ๐๐ถ๐ฒ๐๐ฒ๐ฟ: Find the average salary of employees in each department.
๐ ๐ฒ: Using GROUP BY and AVG():
SELECT d.dept_name,
AVG(e.salary) AS avg_salary
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.dept_id
GROUP BY d.dept_name;
โ Why it works:
โ AVG() computes the mean salary per group, handling decimals nicely.
โ GROUP BY clusters rows by department name for department-level aggregates.
โ INNER JOIN links employee salaries to department names only for matching recordsโadd ORDER BY avg_salary DESC for ranked insights!
๐ฌ Tap โค๏ธ for more!
Post #2183
6.88K
- โค 10
- ๐ 1