-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkforce Project Analysis Database.sql
More file actions
171 lines (125 loc) · 4.03 KB
/
Workforce Project Analysis Database.sql
File metadata and controls
171 lines (125 loc) · 4.03 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
USE NEW
CREATE TABLE Emp
(
EmployeeID INT,
Name VARCHAR(30),
DepartmentID INT,
Salary INT,
HireDate DATE
)
INSERT INTO Emp (EmployeeID, Name, DepartmentID, Salary, HireDate) VALUES
(101, 'Ravi', 2, 60000, '2021-06-15'),
(102, 'Sita', 1, 45000, '2022-01-10'),
(103, 'Amit', 3, 55000, '2020-09-25'),
(104, 'Neha', 2, 70000, '2019-11-05'),
(105, 'Kiran', 4, 40000, '2023-03-18');
CREATE TABLE Dept
(
DepartmentID INT,
DepartmentName VARCHAR(20)
)
INSERT INTO Dept (DepartmentID, DepartmentName) VALUES
(1, 'HR'),
(2, 'IT'),
(3, 'Finance'),
(4, 'Sales');
CREATE TABLE Project
(
ProjectID INT,
ProjectName VARCHAR(20),
StartDate DATE,
EndDate DATE
)
INSERT INTO Project (ProjectID, ProjectName, StartDate, EndDate) VALUES
(201, 'Payroll System', '2022-04-01', '2022-09-30'),
(202, 'Sales Analysis', '2023-01-15', '2023-06-30'),
(203, 'HR Automation', '2021-07-01', '2021-12-31');
CREATE TABLE Emp_project
(
EmployeeID INT,
ProjectID INT
)
INSERT INTO Emp_project (EmployeeID, ProjectID) VALUES
(101, 201),
(102, 203),
(103, 201),
(104, 202),
(105, 202),
(101, 202);
SELECT *
FROM EMP
SELECT *
FROM EMP_project
SELECT *
FROM Dept
SELECT *
FROM project
--1. Retrieve all employees who work in the 'IT' department.
SELECT A.name,B.departmentname
FROM Emp AS A JOIN Dept AS B ON A.departmentid=B.departmentid
WHERE B.departmentname='IT'
GROUP BY B.Departmentname,name
--2. List the names of employees whose salary is above 75000.
SELECT name,salary
FROM Emp --AS A JOIN Dept AS B ON A.departmentid=B.departmentid
WHERE Salary >75000
--3. Display employees who were hired after January 1, 2020.
SELECT Name,Hiredate
FROM EMP
WHERE Hiredate>'2020-01-01'
--4. Show the total salary paid in each department.
SELECT B.departmentname, SUM(A.Salary) Total_sal
FROM Emp AS A JOIN Dept AS B ON A.departmentid=B.departmentid
GROUP BY departmentname
--5. List the employees who are not assigned to any project.
SELECT A.*
FROM Emp AS A LEFT JOIN Emp_project AS B ON A.employeeid=B.employeeid
WHERE B.Projectid IS NULL
--6. Display the department name along with the count of employees in each department.
SELECT departmentname, COUNT(Employeeid) AS Total_emp
FROM Emp AS A JOIN Dept AS B ON A.departmentid=B.departmentid
GROUP BY Departmentname
--7. Retrieve the names of employees working on the 'Sales Anlaysis' project.
SELECT A.name
FROM Emp AS A JOIN Emp_project AS B ON A.employeeid=B.employeeid
JOIN project AS C ON B.projectid=C.projectid
WHERE Projectname = 'Sales Analysis';
--8. Show the highest salary in each department.
SELECT B.departmentname, MAX(A.Salary) Highest_sal
FROM Emp AS A JOIN Dept AS B ON A.departmentid=B.departmentid
GROUP BY departmentname
--9. List employees along with their department names.
SELECT name,departmentname
FROM Emp AS A JOIN Dept AS B ON A.departmentid=B.departmentid
--10. Find the average salary of employees hired before 2020.
SELECT ROUND(AVG(Salary), 2) AS Avg_Salary
FROM Emp
WHERE HireDate < '2020-01-01';
--11. Show employees who are part of multiple projects.
SELECT A.name,count(C.projectid) total_projects
FROM Emp AS A JOIN Emp_project AS B ON A.employeeid=B.employeeid
JOIN project AS C ON B.projectid=C.projectid
GROUP BY name
HAVING COUNT(C.Projectid) >1
--12. Retrieve employees who are working on projects that ended before 2022.
SELECT DISTINCT A.Name
FROM Emp A JOIN Emp_project B ON A.EmployeeID = B.EmployeeID
JOIN Project C ON B.ProjectID = C.ProjectID
WHERE C.EndDate < '2022-01-01';
--13. Display employees with the second highest salary.
SELECT Name, Salary
FROM Emp
WHERE Salary = (
SELECT MAX(Salary)
FROM Emp
WHERE Salary < (SELECT MAX(Salary) FROM Emp)
);
--14. List departments that have no employees assigned.
SELECT B.departmentname
FROM Emp AS A Right JOIN Dept AS B ON A.departmentid=B.departmentid
WHERE A.employeeid IS NULL
--15. Retrieve the total number of projects each employee is assigned to.
SELECT A.name,COUNT(C.projectid) AS Total_Projects
FROM Emp AS A JOIN Emp_project AS B ON A.employeeid=B.employeeid
JOIN project AS C ON B.projectid=C.projectid
GROUP BY A.name