-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07. Data Aggregation Homework.sql
227 lines (199 loc) · 4.44 KB
/
07. Data Aggregation Homework.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
USE Gringotts
--P1
SELECT
COUNT(*) AS [Count]
FROM WizzardDeposits
--P2
SELECT
MAX(MagicWandSize) AS [LongestMagicWand]
FROM WizzardDeposits
--P3
SELECT
DepositGroup,
MAX(MagicWandSize) AS [LongestMagicWand]
FROM WizzardDeposits
GROUP BY DepositGroup
--P4
SELECT
TOP(2)
DepositGroup
FROM WizzardDeposits
GROUP BY DepositGroup
ORDER BY AVG(MagicWandSize)
--P5
SELECT
DepositGroup,
SUM(DepositAmount) AS [TotalSum]
FROM WizzardDeposits
GROUP BY DepositGroup
--P6
--Select all deposit groups and their total deposit sums but only for the wizards who have their
--magic wands crafted by Ollivander family.
SELECT
DepositGroup,
SUM(DepositAmount) AS [TotalSum]
FROM WizzardDeposits
GROUP BY DepositGroup, MagicWandCreator
HAVING MagicWandCreator = 'Ollivander family'
--P7
SELECT
DepositGroup,
SUM(DepositAmount) AS [TotalSum]
FROM WizzardDeposits
GROUP BY DepositGroup, MagicWandCreator
HAVING MagicWandCreator = 'Ollivander family' AND SUM(DepositAmount) < 150000
ORDER BY SUM(DepositAmount) DESC
--P8
SELECT
DepositGroup,
MagicWandCreator,
MIN(DepositCharge) AS [MinDepositCharge]
FROM WizzardDeposits
GROUP BY DepositGroup, MagicWandCreator
ORDER BY MagicWandCreator, DepositGroup
--P9
SELECT
CASE
WHEN Age BETWEEN 0 AND 10 THEN '[0-10]'
WHEN Age BETWEEN 11 AND 20 THEN '[11-20]'
WHEN Age BETWEEN 21 AND 30 THEN '[21-30]'
WHEN Age BETWEEN 31 AND 40 THEN '[31-40]'
WHEN Age BETWEEN 41 AND 50 THEN '[41-50]'
WHEN Age BETWEEN 51 AND 60 THEN '[51-60]'
ELSE '[61+]'
END AS AgeGroup,
COUNT(*) AS [Count]
FROM WizzardDeposits
GROUP BY
(
CASE
WHEN Age BETWEEN 0 AND 10 THEN '[0-10]'
WHEN Age BETWEEN 11 AND 20 THEN '[11-20]'
WHEN Age BETWEEN 21 AND 30 THEN '[21-30]'
WHEN Age BETWEEN 31 AND 40 THEN '[31-40]'
WHEN Age BETWEEN 41 AND 50 THEN '[41-50]'
WHEN Age BETWEEN 51 AND 60 THEN '[51-60]'
ELSE '[61+]'
END
)
SELECT AgeGroupTable.AgeGroup,
COUNT(AgeGroupTable.AgeGroup) AS [WizardCount]
FROM (
SELECT
CASE
WHEN Age BETWEEN 0 AND 10 THEN '[0-10]'
WHEN Age BETWEEN 11 AND 20 THEN '[11-20]'
WHEN Age BETWEEN 21 AND 30 THEN '[21-30]'
WHEN Age BETWEEN 31 AND 40 THEN '[31-40]'
WHEN Age BETWEEN 41 AND 50 THEN '[41-50]'
WHEN Age BETWEEN 51 AND 60 THEN '[51-60]'
ELSE '[61+]'
END AS AgeGroup
FROM WizzardDeposits
) AS AgeGroupTable
GROUP BY AgeGroupTable.AgeGroup
SELECT
*
FROM WizzardDeposits
USE Gringotts
--P10
SELECT
LEFT(FirstName, 1) AS FirstLetter
FROM WizzardDeposits
WHERE DepositGroup = 'Troll Chest'
GROUP BY LEFT(FirstName, 1)
--P11
SELECT
DepositGroup,
IsDepositExpired,
AVG(DepositInterest) AS AverageInterest
FROM WizzardDeposits
WHERE YEAR(DepositStartDate) > 1984
GROUP BY DepositGroup, IsDepositExpired
ORDER BY DepositGroup DESC, IsDepositExpired
--P12
SELECT
wd.FirstName,
wd.DepositAmount,
(
SELECT
wd.DepositAmount
FROM WizzardDeposits AS w
WHERE w.Id = wd.Id + 1
) AS NextRecord
FROM WizzardDeposits AS wd
SELECT SUM(k.Diff) AS SumDifference
FROM(
SELECT
wd.DepositAmount - (
SELECT
w.DepositAmount
FROM WizzardDeposits AS w
WHERE w.Id = wd.Id + 1
) AS Diff
FROM WizzardDeposits AS wd
) AS k
--P13
USE SoftUni
SELECT
DepartmentID,
SUM(Salary) AS TotalSum
FROM Employees
GROUP BY DepartmentID
--P14
SELECT
DepartmentID,
MIN(Salary) AS TotalSum
FROM Employees
WHERE DepartmentID IN (2, 5, 7) AND HireDate > '01/01/2000'
GROUP BY DepartmentID
--P15
SELECT
*
INTO NewEmployeeTable
FROM Employees
WHERE Salary > 30000
DELETE FROM NewEmployeeTable
WHERE ManagerID = 42
UPDATE NewEmployeeTable
SET Salary = Salary + 5000
WHERE DepartmentID = 1
SELECT
DepartmentID,
AVG(Salary) AS [AverageSalary]
FROM NewEmployeeTable
GROUP BY DepartmentID
--P16
SELECT
DepartmentID,
MAX(Salary) AS MaxSalary
FROM Employees
GROUP BY DepartmentID
HAVING MAX(Salary) NOT BETWEEN 30000 AND 70000
--P17
SELECT
COUNT(*)
FROM Employees
WHERE ManagerID IS NULL
--P18
SELECT
DISTINCT
k.DepartmentID,
k.Salary
FROM (
SELECT
DepartmentID,
Salary,
DENSE_RANK() OVER (PARTITION BY DepartmentID ORDER BY Salary DESC) AS SalaryRank
FROM Employees
) AS k
WHERE k.SalaryRank = 3
--P19
SELECT
TOP(10)
FirstName,
LastName,
DepartmentID
FROM Employees AS e
WHERE Salary > (SELECT AVG(Salary) FROM Employees AS em WHERE em.DepartmentID = e.DepartmentID)
ORDER BY DepartmentID