-
Notifications
You must be signed in to change notification settings - Fork 0
/
Databases Sample Exam - 12 October 2016.sql
280 lines (209 loc) · 8.67 KB
/
Databases Sample Exam - 12 October 2016.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
Databases Sample Exam - 12 October 2016 Practise
Section 1: DDL
create table DepositTypes
(
DepositTypeID int primary key,
Name varchar(20)
)
create table Deposits
(
DepositID int primary key identity,
Amount decimal(10,2),
StartDate date,
EndDate date,
DepositTypeID int foreign key references DepositTypes(DepositTypeID),
CustomerID int foreign key references Customers(CustomerID)
)
create table EmployeesDeposits
(
EmployeeID int,
DepositID int,
constraint PK_EmployeesDeposits primary key (EmployeeID, DepositID),
constraint FK_EmployeeID_EmployeeID foreign key (EmployeeID) references Employees(EmployeeID),
constraint FK_DepositID_DepositID foreign key (DepositID) references Deposits(DepositID)
)
create table CreditHistory
(
CreditHistoryID int primary key,
Mark char(1),
StartDate date,
EndDate date,
CustomerID int foreign key references Customers(CustomerID)
)
create table Payments
(
PayementID int primary key,
Date date,
Amount decimal(10,2),
LoanID int foreign key references Loans(LoanID)
)
create table Users
(
UserID int primary key,
UserName varchar(20),
Password varchar(20),
CustomerID int foreign key references Customers(CustomerID) unique
)
alter table Employees
add ManagerID int foreign key references Employees(EmployeeID)
----------------------------------------------------------------------------------------------------------------
Section 2: DML - P01. Inserts
insert into DepositTypes(DepositTypeID, Name)
values ( 1, 'Time Deposit'), (2, 'Call Deposit'), (3, 'Free Deposit')
insert into Deposits (Amount, StartDate, EndDate, DepositTypeID, CustomerID)
select case
when c.DateOfBirth > '1980-01-01' then 1000
else 1500
end
+
case
when c.Gender = 'M' then 100
when c.Gender = 'F' then 200
end as Amount,
GETDATE() as StartDate,
NULL as EndDate,
case when c.CustomerID > 15 then 3
when c.CustomerID % 2 = 0 then 2
when c.CustomerID % 2 != 0 then 1
end as DepositTypeID,
c.CustomerID from Customers as c where c.CustomerID < 20
insert into EmployeesDeposits (EmployeeID,DepositID)
values (15, 4), (20, 15), (8, 7), (4, 8), (3, 13), (3, 8), (4, 10), (10, 1), (13, 4), (14,9)
----------------------------------------------------------------------------------------------------------------
Section 2: DML - P02. Update
update Employees
set ManagerID = case when EmployeeID between 2 and 10 then 1
when EmployeeID between 12 and 20 then 11
when EmployeeID between 22 and 30 then 21
when EmployeeID = 11 or EmployeeID = 21 then 1
end
----------------------------------------------------------------------------------------------------------------
Section 2: DML - P03. Delete
delete from EmployeesDeposits
where DepositID = 9 or EmployeeID = 3
----------------------------------------------------------------------------------------------------------------
Section 3: Querying - P01. Employees’ Salary
select EmployeeID, HireDate, Salary, BranchID from Employees
where Salary > 2000 and HireDate > '2009-06-15'
----------------------------------------------------------------------------------------------------------------
Section 3: Querying - P02. Customer Age
select FirstName, DateOfBirth, datediff(year, DateOfBirth, '2016-10-01') as Age from Customers
where datediff(year, DateOfBirth, '2016-10-01') between 40 and 50
----------------------------------------------------------------------------------------------------------------
Section 3: Querying - P03. Customer City
select c.CustomerID, c.FirstName, LastName, c.Gender, ct.CityName from Customers as c
join Cities as ct on c.CityID = ct.CityID
where (substring(FirstName,len(FirstName), 1) = 'a' or substring(LastName,1, 2) = 'Bu') and len(ct.CityName) >= 8
order by CustomerID
----------------------------------------------------------------------------------------------------------------
Section 3: Querying - P04. Employee Accounts
select top 5 e.EmployeeID, e.FirstName, a.AccountNumber from Employees as e
join EmployeesAccounts as ea on e.EmployeeID = ea.EmployeeID
join Accounts as a on ea.AccountID = a.AccountID
where a.StartDate > '2012'
order by e.FirstName desc
----------------------------------------------------------------------------------------------------------------
Section 3: Querying - P05. Count Cities
select c.CityName,b.Name, count(e.EmployeeID) as EmployeesCount from Cities as c
join Branches as b on c.CityID = b.CityID
join Employees as e on b.BranchID = e.BranchID
where c.CityID not in (4,5)
group by c.CityName, b.Name
having count(e.EmployeeID) > 2
----------------------------------------------------------------------------------------------------------------
Section 3: Querying - P06. Loan Statistics
select sum(l.Amount) as TotalLoanAmount, max(l.Interest) as MaxInterest, min(e.Salary) as MinEmployeeSalary from Loans as l
join EmployeesLoans as el on l.LoanID = el.LoanID
join Employees as e on el.EmployeeID = e.EmployeeID
----------------------------------------------------------------------------------------------------------------
Section 3: Querying - P07. Unite People
select top 3 e.FirstName,c.CityName from Employees as e
join Branches as b on e.BranchID = b.BranchID
join Cities as c on b.CityID = c.CityID
union all
select top 3 cu.FirstName, c.CityName from Customers as cu
join Cities as c on cu.CityID = c.CityID
----------------------------------------------------------------------------------------------------------------
Section 3: Querying - P08. Customers w/o Accounts
select CustomerID , Height from Customers
where CustomerID not in (select CustomerID from Accounts) and Height between 1.74 and 2.04
----------------------------------------------------------------------------------------------------------------
Section 3: Querying - P09. Average Loans
select top 5 c.CustomerID, l.Amount from Customers as c
join Loans as l on c.CustomerID = l.CustomerID
where l.Amount > (select avg(l.Amount) from Loans as l
join Customers as c on l.CustomerID = c.CustomerID
where c.Gender = 'M')
order by c.LastName
----------------------------------------------------------------------------------------------------------------
Section 3: Querying - P10. Oldest Account
select top 1 c.CustomerID, c.FirstName, a.StartDate from Customers as c
join Accounts as a on c.CustomerID = a.CustomerID
order by a.StartDate
----------------------------------------------------------------------------------------------------------------
Section 4: Programmability - P01. String Joiner
create function udf_ConcatString (@Str1 varchar(max), @Str2 varchar(max))
returns varchar(max)
as
begin
return CONCAT(reverse(@Str1), reverse(@Str2))
end
----------------------------------------------------------------------------------------------------------------
Section 4: Programmability - P02. Inexpired Loans
create proc usp_CustomersWithUnexpiredLoans (@CustomerID int)
as
begin
if @CustomerID in (select c.CustomerID from Customers as c
join Loans as l on c.CustomerID = l.CustomerID
where l.ExpirationDate is NULL)
begin
select c.CustomerID, c.FirstName, l.LoanID from Customers as c
join Loans as l on c.CustomerID = l.CustomerID
where l.ExpirationDate is NULL and c.CustomerID = @CustomerID
end
else
begin
select c.CustomerID, c.FirstName, l.LoanID from Customers as c
join Loans as l on c.CustomerID = l.CustomerID
where 0=1
end
end
----------------------------------------------------------------------------------------------------------------
Section 4: Programmability - P03. Take Loan
create proc usp_TakeLoan (@CustomerID int, @LoanAmount money, @Interest money, @StartDate date)
as
begin
begin tran
if @LoanAmount not between 0.01 and 100000
begin
raiserror ('Invalid Loan Amount.',16,1)
rollback
end
else
begin
insert into Loans(StartDate, Amount, Interest, ExpirationDate, CustomerID)
values(@StartDate, @LoanAmount, @Interest, null, @CustomerID)
commit
end
end
----------------------------------------------------------------------------------------------------------------
Section 4: Programmability - P04. Hire Employee
create trigger TR_HireEmployee on Employees after insert
as
begin
update EmployeesLoans
set EmployeeID = i.EmployeeId
from EmployeesLoans as e
join inserted as i
on e.EmployeeID + 1 = i.EmployeeID
end
----------------------------------------------------------------------------------------------------------------
Section 5: Bonus - P01. Delete Trigger
create trigger TR_DelTrig on Accounts instead of delete
as begin
delete from EmployeesAccounts where AccountID in (select d.AccountID from deleted as d)
insert into AccountLogs
select * from deleted
delete Accounts
where AccountID in (select d.AccountID from deleted as d)
end