-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectDB DML Command (1280827).sql
216 lines (178 loc) · 6.26 KB
/
ProjectDB DML Command (1280827).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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
--DML COMMAND
USE ProjectDB
GO
INSERT INTO Project_T
VALUES
('PC01', 'Pensions System', 800000),
('PC04', 'Salary System', 900000),
('PC06', 'HR System', 600000)
GO
INSERT INTO Department_T
VALUES
('L004', 'IT'),
('L023', 'HR'),
('L008', 'Pay Roll'),
('L009', 'Sales');
GO
INSERT INTO Emplopyee_T
VALUES
('S1000', 'Allen', 'Smith', 'L004'),
('S1003', 'Lewis', 'Jones', 'L023'),
('S2101', 'Prince', 'Lewis', 'L004'),
('S1001', 'Barbara', 'Jones', 'L004'),
('S3100', 'Tony', 'Gilbert', 'L023'),
('S1321', 'Frank', 'Richo', 'L008'),
('S1004', 'Robert', 'John', 'L009');
GO
INSERT INTO Associate_T
VALUES
('PC01', 'S1000', 220.00),
('PC01', 'S1003', 180.50),
('PC01', 'S2101', 210.00),
('PC04', 'S1001', 210.00),
('PC04', 'S1000', 180.00),
('PC04', 'S3100', 25.00),
('PC04', 'S1321', 170.00),
('PC06', 'S3100', 230.00),
('PC06', 'S2101', 170.00),
('PC06', 'S1004', 160.00);
GO
--A Query For delete Column
DELETE FROM Associate_T
WHERE ProjectCode = 'PC01';
GO
-- An Update Query
UPDATE Associate_T
SET HourlyRate=30.00
WHERE ProjectCode='PC04' AND DepartmentCode='L023';
GO
--- A query to retrieve top 3 employee
SELECT TOP 3 EmployeeCode, EmployeeFName,EmployeeLName,DepartmentCode
FROM Emplopyee_T
ORDER BY EmployeeCode DESC;
--- A query to retrieve projects which budget is more than 800000
SELECT ProjectTittle, Budget FROM Associate_T A JOIN Project_T P
ON A.ProjectCode=P.ProjectCode
WHERE Budget > 800000;
--- Example of in/not in
select E.EmployeeCode, (e.EmployeeFName+' '+e.EmployeeLName) AS EmployeeName ,d.DepartmentName
from Emplopyee_T e join Department_T d
on e.DepartmentCode=d.DepartmentCode
where d.DepartmentName in ('IT','HR')
select E.EmployeeCode, (e.EmployeeFName+' '+e.EmployeeLName) AS EmployeeName ,d.DepartmentName
from Emplopyee_T e join Department_T d
on e.DepartmentCode=d.DepartmentCode
where d.DepartmentName not in ('IT','HR')
---example of between
select ProjectTittle, Budget from Project_T
where Budget between 600000 and 900000
--- Example of %
select ProjectTittle, Budget from Project_T
where ProjectTittle like 'HR%'
GO
select
* from Emplopyee_T
where EmployeeFName like 'B[A-J]%'
GO
select
* from Emplopyee_T
where EmployeeFName like 'L[^X-Z]%'
---A query to retrieve those employee whose FName in (a, e, i, o, u)
select ProjectCode, EmployeeFName,EmployeeLName from Associate_T A JOIN Emplopyee_T E
ON A.EmployeeCode=E.EmployeeCode
WHERE EmployeeFName LIKE '[aeiou]%';
go
--A join query to retrieve all info using group by / having
SELECT DepartmentName, Count(Emplopyee_T.EmployeeCode) AS TotalEmployee, SUM(Budget) AS TotalBudget
FROM Associate_T
JOIN Project_T ON Associate_T.ProjectCode=Project_T.ProjectCode
JOIN Emplopyee_T ON Emplopyee_T.EmployeeCode=Associate_T.EmployeeCode
JOIN Department_T ON Department_T.DepartmentCode=Emplopyee_T.DepartmentCode
GROUP BY DepartmentName
HAVING SUM(Budget)>800000;
--- EXAMPLE OF CUBE
SELECT Budget, DepartmentName, Count(Emplopyee_T.EmployeeCode) AS TotalEmployee
FROM Associate_T
JOIN Project_T ON Associate_T.ProjectCode=Project_T.ProjectCode
JOIN Emplopyee_T ON Emplopyee_T.EmployeeCode=Associate_T.EmployeeCode
JOIN Department_T ON Department_T.DepartmentCode=Emplopyee_T.DepartmentCode
GROUP BY CUBE (Budget, DepartmentName)
---EXAMPLE OF ROLLUP
SELECT Budget, DepartmentName, Count(Emplopyee_T.EmployeeCode) AS TotalEmployee
FROM Associate_T
JOIN Project_T ON Associate_T.ProjectCode=Project_T.ProjectCode
JOIN Emplopyee_T ON Emplopyee_T.EmployeeCode=Associate_T.EmployeeCode
JOIN Department_T ON Department_T.DepartmentCode=Emplopyee_T.DepartmentCode
GROUP BY ROLLUP (Budget, DepartmentName)
---EXAMPLE OF GROUPING SETS
SELECT Budget, DepartmentName, Count(Emplopyee_T.EmployeeCode) AS TotalEmployee
FROM Associate_T
JOIN Project_T ON Associate_T.ProjectCode=Project_T.ProjectCode
JOIN Emplopyee_T ON Emplopyee_T.EmployeeCode=Associate_T.EmployeeCode
JOIN Department_T ON Department_T.DepartmentCode=Emplopyee_T.DepartmentCode
GROUP BY GROUPING SETS (Budget, DepartmentName)
-- SUB QUERY to show all information of Project HR SYSTEM
GO
SELECT Project_T.ProjectCode, ProjectTittle, Emplopyee_T.EmployeeCode, EmployeeFName, Budget, DepartmentName, HourlyRate
FROM Associate_T
JOIN Project_T ON Associate_T.ProjectCode=Project_T.ProjectCode
JOIN Emplopyee_T ON Emplopyee_T.EmployeeCode=Associate_T.EmployeeCode
JOIN Department_T ON Department_T.DepartmentCode=Emplopyee_T.DepartmentCode
WHERE Project_T.ProjectCode IN
(SELECT ProjectCode FROM Project_T WHERE ProjectTittle='HR SYSTEM');
--- example of exists
select EmployeeFName,EmployeeLName from Emplopyee_T
where exists
(select * from Project_T)
GO
--justify view
select * from ProjectBudgetView
EXEC sp_helptext ProjectBudgetView
--JUSTIFY STORE PROCEDURE
EXEC spInsertUpdateDeleteAndOutputParameter 'SELECT','','','';
EXEC spInsertUpdateDeleteAndOutputParameter 'Insert','L100','Marketing','';
EXEC spInsertUpdateDeleteAndOutputParameter 'Update','L100','MKT','';
EXEC spInsertUpdateDeleteAndOutputParameter 'Delete','L100','','';
GO
--Showing process of ERROR HANDLING
BEGIN TRY
INSERT INTO Department_T
VALUES
('L004', 'IT'),
('L023', 'HR'),
('L008', 'Pay Roll'),
('L009', 'Sales')
PRINT 'Query Execueed Successfully'
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS ErrorMessage,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_LINE() AS ErrorLine,
ERROR_NUMBER() AS ErrorNumber
END CATCH;
GO
--Creating simple case and a search case
select ProjectTittle,Budget,
case
when Budget >800000 then 'Budget is more than 800000'
when Budget =800000 then 'Budget is 800000'
else 'Budget is less than 800000'
end as BudgetDetails
from Project_T;
go
select ProjectCode, ProjectTittle,Budget,
case
when Budget >800000 then 'Budget is more than 800000'
when Budget =800000 then 'Budget is 800000'
when Budget =600000 then 'Budget is 600000'
else 'Budget is invalid'
end as BudgetDetails
from Project_T;
----------
DELETE FROM Associate_T WHERE EmployeeCode= 'pc04'
-------------
INSERT INTO Department_T values ('123','it')
----------------------
SELECT * FROM fnbudget(8000)
---------------------------------
SELECT dbo.fnprojrct('pc01')