-
Notifications
You must be signed in to change notification settings - Fork 0
/
raj_fk_part2.sql
58 lines (52 loc) · 2.1 KB
/
raj_fk_part2.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
-- EMPLOYEES
SELECT * from tw_employees;
-- TW_DEPARTMENTSS
SELECT * from tw_departments;
/* Fill in your assignments below */
--
--1. What is the total count of employees in each of the departments ?
--
SELECT TW_DEPARTMENTS.DEPT_NAME,COUNT(1) FROM TW_EMPLOYEES INNER JOIN TW_DEPARTMENTS
ON TW_DEPARTMENTS.DEPT_ID = TW_EMPLOYEES.DEPT_ID
GROUP BY (TW_EMPLOYEES.DEPT_ID);
--
--2. How many direct report employees are present for every HOD ?
--
SELECT TW_DEPARTMENTS.HOD,count(TW_EMPLOYEES.DEPT_ID)
FROM TW_EMPLOYEES INNER JOIN TW_DEPARTMENTS
ON TW_DEPARTMENTS.DEPT_ID = TW_EMPLOYEES.DEPT_ID
group by (TW_DEPARTMENTS.DEPT_ID);
--
--3. List all employees along with their department names, including who are not staffed yet.
--
SELECT TW_EMPLOYEES.EMP_NAME,TW_DEPARTMENTS.DEPT_NAME
FROM TW_EMPLOYEES LEFT JOIN TW_DEPARTMENTS
ON TW_EMPLOYEES.DEPT_ID = TW_DEPARTMENTS.DEPT_ID;
--
--4. List all departments with the names of the HOD, including departments that dont have any staff.
--
SELECT TW_DEPARTMENTS.DEPT_NAME,TW_EMPLOYEES.EMP_NAME
from TW_DEPARTMENTS LEFT JOIN TW_EMPLOYEES
on TW_DEPARTMENTS.HOD = TW_EMPLOYEES.EMP_ID;
--
--5. Get a list of all employee id, employee name and the department names. Include all employees and all departments even if they are not staffed.
--
SELECT TW_EMPLOYEES.EMP_ID,TW_EMPLOYEES.EMP_NAME,TW_DEPARTMENTS.DEPT_NAME
from TW_EMPLOYEES RIGHT OUTER JOIN TW_DEPARTMENTS
on TW_EMPLOYEES.DEPT_ID = TW_DEPARTMENTS.DEPT_ID
UNION
SELECT TW_EMPLOYEES.EMP_ID,TW_EMPLOYEES.EMP_NAME,TW_DEPARTMENTS.DEPT_NAME
from TW_EMPLOYEES LEFT OUTER JOIN TW_DEPARTMENTS
on TW_EMPLOYEES.DEPT_ID = TW_DEPARTMENTS.DEPT_ID;
--
--6. How many employees in each TW_DEPARTMENTS are not managers ? Give me the count by each department with its name
--
SELECT TW_DEPARTMENTS.DEPT_NAME,count(TW_DEPARTMENTS.DEPT_ID) as Employees
from TW_EMPLOYEES RIGHT OUTER JOIN TW_DEPARTMENTS
ON TW_EMPLOYEES.DEPT_ID = TW_DEPARTMENTS.DEPT_ID
and TW_EMPLOYEES.MGR_ID <> TW_EMPLOYEES.EMP_ID
group by (TW_DEPARTMENTS.DEPT_ID);
--
--7. What is the total strength employees in the company ?
--
SELECT count(1) from TW_EMPLOYEES;