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 hidden or 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 hidden or 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