-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPracticeSQL6.sql
149 lines (119 loc) · 4.28 KB
/
PracticeSQL6.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
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
CREATE TABLE Clients(
ClientID INT,
ClientName VARCHAR(50) NOT NULL,
Address VARCHAR(100),
Email VARCHAR(30) UNIQUE,
Phone INT,
Business VARCHAR(30) NOT NULL, --Business type of client like Manufacturer, Reseller ...
PRIMARY KEY (ClientID)
)
CREATE TABLE Projects(
ProjectID INT,
Description VARCHAR(100), --Description of project like Accounting, Payroll, ...
StartDate DATE, --Start date of project
PlannedEndDate DATE, --Planned end date of project
ActualEndDate DATE ,
Budget INT CHECK(Budget > 0),
ClientID INT,
CHECK(ActualEndDate > PlannedEndDate),
FOREIGN KEY (ClientID) REFERENCES Clients(ClientID)
)
CREATE TABLE Departments(
DepartmentNo INT,
DepartmentName VARCHAR(100) NOT NULL,
PRIMARY KEY (DepartmentNo)
)
CREATE TABLE Employees(
EmployeeNo INT,
EmplouyeeName VARCHAR(20) NOT NULL,
Job VARCHAR(20),
Salary INT CHECK(Salary > 1700),
DepartmentNo INT, --DepartmentNo as per departments table
PRIMARY KEY (EmployeeNo),
FOREIGN KEY (DepartmentNo) REFERENCES Departments(DepartmentNo)
)
CREATE TABLE EmployeeProjectTasks(
ProjectID INT,
EmployeeNo INT,
StartDate DATE, --Start date when employee begins task on this project
EndDate DATE, --End date when employee finishes task on this project
Task VARCHAR(30), --Task performed by employee like designing, coding ..
Status VARCHAR(30), -- Status of task like ‘in progress’, ‘complete’,‘cancelled’
PRIMARY KEY (ProjectID, EmployeeNo),
FOREIGN KEY (ProjectID) REFERENCES Projects(ProjectID),
FOREIGN KEY (EmployeeNo) REFERENCES Employees(EmployeeNo)
)
INSERT INTO Clients(ClientID, ClientName, Address, Email, Phone, Business)
VALUES
(1, 'Nayem', 'Portugal', '[email protected]', 920485163, 'MoslinMeta'),
(2, 'Belmahi', 'Morrocco', '[email protected]', 920485162, 'BelmahiMeta');
INSERT INTO Projects(ProjectID, Description, StartDate, PlannedEndDate, ActualEndDate, Budget, ClientID)
VALUES
(1, 'Accounting', 12/7/2021, 16/7/2021, 18/7/2021, 2500, 1),
(2, 'Payroll', 12/7/2021, 16/7/2021, 18/7/2021, 2502, 2);
INSERT INTO Departments(DepartmentNo, DepartmentName)
VALUES
(1, 'Main Department'),
(2, 'Secondary Department');
INSERT INTO Employees(EmployeeNo, EmployeeName, Job, Salary, DepartmentNo)
VALUES
(1, 'Nayem Pagla', 'Caretaker', 2500, 1);
(2, 'Maheera', 'Doctor', 4000, 2);
INSERT INTO EmployeeProjectTasks(ProjectID, EmployeeNo, StartDate, EndDate, Task, Status)
VALUES
(1, 1, 22/7/2021, 28/7/2021, 'Data Entry', 'in progress'),
(2, 2, 22/7/2021, 29/7/2021, 'Sergery', 'Complete');
select * FROM Employees where EmployeeName like 'M%';
select EmployeeNo, EmployeeName
from table Employees
order by len(EmployeeName) desc;
select Departments.DepartmentName, Employees.EmployeeName, Employees.Salary
from LAB5
order by Salary desc;
--task 1
WITH Clients_CTE(ClientName, Description, Status)
AS
(
SELECT Clients.ClientName, Projects.Description, EmployeeProjectTasks.Status
FROM Employees
GROUP BY ClientName
)
SELECT * FROM Clients_CTE;
-- task 2
WITH Departments_CTE(DepartmentNo, DepartmentName)
AS
(
SELECT Departments.DepartmentNo, DepartmentName
FROM Employees LEFT JOIN Departments
ON Employees.DepartmentNo = Departments.DepartmentNo
GROUP BY Departments.DepartmentNo, DepartmentName
HAVING Count(EmployeeNo) >= 3
)
SELECT * FROM Departments_CTE;
-- task 3
WITH Avg_salary(DepartmentNo, average)
AS
(
SELECT DepartmentNo, AVG(Salary)
FROM Employees
GROUP BY DepartmentNo
)
SELECT Avg_salary.DepartmentNo, average, SUM(CASE WHEN salary >= average THEN 1 ELSE 0 END) AS employees_equal_or_above_average
FROM employees LEFT JOIN Avg_salary ON employees.DepartmentNo = Avg_salary.DepartmentNo
GROUP BY Avg_salary.DepartmentNo, average
-- task 4
SELECT * FROM Employees
SELECT * FROM Employees WHERE EmployeeName LIKE 'M%';
SELECT * FROM Employees
WHERE
len(EmployeeName) =
(SELECT max(len(EmployeeName)) FROM Employees);
SELECT DepartmentName, EmployeeName, Salary FROM Employees
INNER JOIN Departments ON Employees.DepartmentNo=Departments.DepartmentNo
ORDER BY Salary DESC;
--task 5
SELECT d.DepartmentNo,d.DepartmentName,
COUNT(e.EmployeeNo) as number_of_employees
FROM Departments d,Employees e
WHERE d.DepartmentNo = e.DepartmentNo
GROUP BY d.DepartmentNo,d.DepartmentName