[SQL] “Average Salaries” – Salesforce/Glassdoor

Photo by Monstera from Pexels

 

The dataset for this SQL interview question contains information on several employees working in different departments of a given company. The table includes information about the first- and last names of the employees, age, sex, bonus, and contact information such as the city, address and email. The goal here would be to write a query, with which we can compare the salary of each of the employees with the average salary in the department in which this employee is working.

This is the code that solves the question:

SELECT e.department, e.first_name, e.salary, dept_avgs.avg_salary
FROM employee e
JOIN (SELECT department, AVG(salary) avg_salary FROM employee GROUP BY department) dept_avgs
ON e.department = dept_avgs.department

 

The complete solution and walkthrough for this puzzle can be found here:

 

 

 

Related Images: