You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List employees who have the biggest salary IN their departments
SELECTe.nameAS'Employee Name', d.NameAS'Department', e.SalaryAS'Salary'FROM departments d JOIN employees e
ONe.department_id=d.department_idGROUP BYd.department_idORDER BYe.salaryDESC
List departments that have less than 3 people IN it
SELECTd.NameAS'Department'FROM departments d JOIN employees e
ONe.department_id=d.Department_idGROUP BYd.department_idHAVINGCOUNT(e.employee_id) <3
List ALL departments along WITH the NUMBER OF people there (tricky - people often do an "inner join" leaving OUT empty departments)
SELECTd.nameAS'Department', COUNT(e.employee_id) AS'# of Employees'FROM departments d LEFT OUTER JOIN employees e
ONe.department_id=d.department_idGROUP BYd.Department_id
List employees that don't have a boss in the same department
SELECTe.nameAS'Employee Name', e2.nameAS'Boss', d.nameAS"Employee's Department", d2.nameAS"Boss' Department"FROM employees e
JOIN employees e2 ONe.boss_id=e2.employee_idJOIN departments d ONe.department_id=d.department_idJOIN departments d2 ONe2.department_id=d2.department_idWHEREe.boss_id!=0ANDd.department_id!=d2.department_id
List all departments along with the total salary there
SELECTd.nameAS'Department', SUM(e.salary) AS'Total Salary'FROM departments d LEFT OUTER JOIN employees e
ONd.department_id=e.department_idGROUP BYd.department_id
I think chromano is correct. Here was my response:
SELECTd.nameas department, e.nameas max_earner, MAX(e.salary) as salary
FROM employees e
JOIN departments d ONe.department_id=d.idGROUP BYe.department_id;
Note that my solutions use a simple id column as a pk for each table instead of employee_id and department_id.
The solutions given for problem #2 contain data inconsistencies. The correct answer is: IT,emp3,95000
In the original posted answer, the query will return the wrong salary and wrong employee. The group by is executed before the order by clause and returns the first row that matches department_id. As a result, the salary is not really the greatest per department. Returns: IT,manager,80000. incorrect
In skandocious answer, this query will fail completely for other databases like MS SQL and Oracle, because all non-aggregated columns must be specified in the group by clause for consistency purposes. The correct salary is returned, but with the wrong associated employee. The max(salary) returns the max salary per department ID; however, since we didn't specify the column names in the group by clause, the data is no longer consistent. We now return the first possible employee name for the matching department courtesy of the join clause, but with the maximum salary per department. Returns: IT, manager, 95000. incorrect
Below are two possible solutions:
First solution - Returns only one person for each department with the highest salary:
SELECT*FROM ( SELECTdept.Name Department, emp.Name Employee, emp.Salary Salary
FROM Departments dept
JOIN Employees emp ONemp.department_id=dept.department_idORDER BY salary desc ) result
GROUP BY Department;
We first get the departments, employees and their relative salaries, and organize salaries in descending order. The outer query uses a group by, grabbing the first row of each record associated with their department. Since the first result set is ordered by salary in descending order, the outer group by will choose the first record, which will be the highest salary. One issue with this solution is we will not return each employee that shares the highest salary.
Second solution - Returns one or more people for each department with the highest salary:
SELECTresult.Name Department, emp2.Name Employee, result.salary Salary
FROM ( SELECTdept.name, dept.department_id, max(emp1.salary) salary
FROM Departments dept
JOIN Employees emp1 ONemp1.department_id=dept.department_idGROUP BYdept.name, dept.department_id ) result
JOIN Employees emp2 ONemp2.department_id=result.department_idWHEREemp2.salary=result.salary;
This one will return each employee that shares the highest salary.
Solution to the 5th can be better written as follows select e2.*, e1.department_ID from #employees e1 inner join #employees e2 on e1.employee_id = e2.boss_id where e1.department_ID <> e2.department_ID
Solution to the 2nd is wrong.
kalypzo your query is fine but it gives the department not the employee
Question asked to find out the employee not the department. Therefore why do we need to join department table in first place.
Please comment if i'm wrong.
correct solution: Select A.Employee_id,A.name from (Select employee_id,name,DENSE_RANK() OVER (PARTITION BY department_id ORDER BY Salary DESC) AS Rnk from Employees) A where A.Rnk=1
Thank you so much for the Table Creation/Populating code. I want to practise these questions but do not want the answers. I could have whipped them up myself but.... this makes things so much more streamlined. thank you!
In #2, it is supposed to return one entry per department as I understood it.