Skip to main content

SQL Interview Question - 4


Consider two tables: employees and departments.


The employees table contains columns emp_id, emp_name, dept_id, salary.

The departments table contains columns dept_id and dept_name.

Write a SQL query to find the department names along with the average salary of employees in each department. Include all departments, even if there are no employees in them.

Sample Data:

employees
emp_id emp_name dept_id salary
1 Alice 101 50000
2 Bob 102 60000
3 Charlie 101 55000
4 David 103 48000
5 Eve 102 62000
 
departments
dept_id dept_name
101 HR
102 Finance
103 Marketing
104 Operations

Solution:

SELECT d.dept_name, COALESCE(AVG(e.salary), 0) AS avg_salary
FROM departments d
LEFT JOIN employees e ON d.dept_id = e.dept_id
GROUP BY d.dept_name;

 

Share your alternate solution in comments.


Check our offerings below!


Success Stories

Real outcomes from learners who followed the process



See all the Success Stories - here
Testimonials - here


You can check out other TakeOff Talent offerings that have helped 8,000+ people land jobs.

Offerings
📄 CV Review
📘 200 most-asked SQL interview questions with detailed solutions
📘 200 most-asked Python interview questions with detailed solutions
📊 SQL Crash Course
✍️ CV Writing for freshers
✍️ CV Writing
🛠️ Portfolio Project
🗣️ English Speaking Practice (Live 1:1)
🎯 Job Search Mentorship Package

  In case of any questions around services above, write to us at vibhanshu@takeofftalent.com

  Connect with our founder on Linkedin - https://www.linkedin.com/in/vibvibgyor/



Video Gallery



Check more videos here>>