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
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!
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