-
Notifications
You must be signed in to change notification settings - Fork 0
/
06.Joins, Subqueries, CTE and Indices.sql
212 lines (170 loc) · 7.09 KB
/
06.Joins, Subqueries, CTE and Indices.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
Joins, Subqueries, CTE and Indices Homework
01. -- Employee Address
select top 5 e.EmployeeID, e.JobTitle, e.AddressID, a.AddressText from Employees as e
join Addresses as a
on e.AddressID = a.AddressID
order by AddressID
----------------------------------------------------------------------------------------------------------------
02. -- Addresses with Towns
select top 50 e.FirstName, e.LastName, t.Name as Town, AddressText from Employees as e
join Addresses as a
on a.AddressID = e.AddressID
join Towns as t
on t.TownID = a.TownID
order by e.FirstName, e.LastName
-----------------------------------------------------------------------------------------------------------------
03. -- Sales Employees
select e.EmployeeID, e.FirstName, e.LastName, d.Name as DepartmentName from Employees as e
join Departments as d
on e.DepartmentID = d.DepartmentID
where d.Name = 'Sales'
order by EmployeeID
----------------------------------------------------------------------------------------------------------------
04. -- Employee Departments
select top 5 e.EmployeeID, e.FirstName, e.Salary, d.Name as DepartmentName from Employees as e
join Departments as d
on e.DepartmentID = d.DepartmentID
where e.Salary > 15000
order by e.DepartmentID
----------------------------------------------------------------------------------------------------------------
05. -- Employees Without Projects
select top 3 e.EmployeeID, e.FirstName from Employees as e
full join EmployeesProjects as ep
on e.EmployeeID = ep.EmployeeID
where ep.EmployeeID is null
order by e.EmployeeID
----------------------------------------------------------------------------------------------------------------
06. -- Employees Hired After
select e.FirstName, e.LastName, HireDate, d.Name as DeptName from Employees as e
join Departments as d
on e.DepartmentID = d.DepartmentID
where e.HireDate > '1999-01-01' and (d.Name = 'Sales' or d.Name = 'Finance')
----------------------------------------------------------------------------------------------------------------
07. -- Employees With Project
select top 5 e.EmployeeID, e.FirstName, p.Name as ProjectName from Employees as e
join EmployeesProjects as ep
on e.EmployeeID = ep.EmployeeID
join Projects as p
on ep.ProjectID = p.ProjectID
where p.StartDate > '2002-08-13' and p.EndDate is null
order by e.EmployeeID
----------------------------------------------------------------------------------------------------------------
08. -- Employee 24
select e.EmployeeID, e.FirstName,
case
when p.StartDate > '2005-01-01'
then NULL
else p.Name
end as ProjectName from Employees as e
join EmployeesProjects as ep
on e.EmployeeID = ep.EmployeeID
join Projects as p
on ep.ProjectID = p.ProjectID
where e.EmployeeID = 24
----------------------------------------------------------------------------------------------------------------
09. -- Employee Manager
select e.EmployeeID, e.FirstName, e.ManagerId, m.FirstName as ManagerName from Employees as e
join Employees as m
on e.ManagerID = m.EmployeeID
where e.ManagerID = 3 or e.ManagerID = 7
order by e.EmployeeID
----------------------------------------------------------------------------------------------------------------
10. -- Employees Summary
select top 50
e.EmployeeID,
e.FirstName + ' ' + e.LastName as EmployeeName,
m.FirstName + ' ' + m.LastName as ManagerName,
d.Name as DepartmentName
from Employees as e
join Employees as m
on e.ManagerID = m.EmployeeID
join Departments as d
on e.DepartmentID = d.DepartmentID
order by e.EmployeeID
----------------------------------------------------------------------------------------------------------------
11. -- Min Average Salary
select top 1 avg(Salary) as MinAverageSalary from Employees
group by DepartmentID
order by avg(Salary)
----------------------------------------------------------------------------------------------------------------
12. -- Highest Peaks in Bulgaria
SELECT c.CountryCode, m.MountainRange,
p.PeakName, p.Elevation
FROM MountainsCountries as c
JOIN Mountains as m
ON c.MountainId = m.Id
RIGHT JOIN Peaks p
ON p.MountainId = m.Id
WHERE c.CountryCode = 'BG'
AND p.Elevation > 2835
ORDER BY p.Elevation DESC
----------------------------------------------------------------------------------------------------------------
13. -- Count Mountain Ranges
SELECT c.CountryCode, count(m.MountainRange) as MountainRanges
FROM MountainsCountries as c
JOIN Mountains as m
ON c.MountainId = m.Id
WHERE c.CountryCode in ('BG', 'RU', 'US')
group by C.CountryCode
----------------------------------------------------------------------------------------------------------------
14. -- Countries With or Without Rivers
select top 5 c.CountryName, r.RiverName from Countries as c
full join CountriesRivers as cr
on c.CountryCode = cr.CountryCode
full join Rivers as r
on cr.RiverId = r.Id
where c.ContinentCode = 'AF'
order by c.CountryName
----------------------------------------------------------------------------------------------------------------
15. -- *Continents and Currencies
select c.ContinentCode, cc.CurrencyCode, COUNT(cc.CountryCode) AS CurrencyUsage from Continents as c
join Countries as cc
on c.ContinentCode = cc.ContinentCode
group by c.ContinentCode, cc.CurrencyCode
having count(cc.CountryCode) = (select max(xxx.CurrencyXX) from (SELECT cx.ContinentCode, ccx.CurrencyCode,
COUNT(ccx.COUNTryCode) AS CurrencyXX
FROM Continents cx
JOIN Countries ccx
ON cx.ContinentCode = ccx.ContinentCode
WHERE c.ContinentCode = cx.ContinentCode
GROUP BY cx.ContinentCode , ccx.CurrencyCode) AS xxx)
AND COUNT(cc.CountryCode) > 1
ORDER BY c.ContinentCode
----------------------------------------------------------------------------------------------------------------
16. -- Countries Without any Mountains
SELECT count(c.CountryCode) - count(e.MountainId) as CountryCode FROM MountainsCountries as e
full join Countries as c
on e.CountryCode = c.CountryCode
----------------------------------------------------------------------------------------------------------------
17. -- Highest Peak and Longest River by Country
SELECT TOP 5 c.CountryName,
MAX(p.Elevation) AS HighestPeakElevation,
MAX(r.Length) AS LongestRiverLength
FROM Countries as c
LEFT JOIN MountainsCountries as mc
ON c.CountryCode = mc.CountryCode
LEFT JOIN Peaks as p
ON mc.MountainId = p.MountainId
LEFT JOIN CountriesRivers as cr
ON c.CountryCode = cr.CountryCode
LEFT JOIN Rivers as r
ON cr.RiverId = r.Id
GROUP BY c.CountryName
ORDER BY HighestPeakElevation DESC,
LongestRiverLength DESC, c.CountryName
----------------------------------------------------------------------------------------------------------------
18. -- *Highest Peak Name and Elevation by Country
SELECT TOP 5
c.CountryName AS Country,
ISNULL(p.PeakName, '(no highest peak)') AS HighestPeakName,
ISNULL(MAX(p.Elevation), 0) AS HighestPeakElevation,
ISNULL(m.MountainRange, '(no mountain)') AS Mountain
FROM Countries c
LEFT JOIN MountainsCountries mc
ON c.CountryCode = mc.CountryCode
LEFT JOIN Peaks p
ON mc.MountainId = p.MountainId
LEFT JOIN Mountains m
ON mc.MountainId = m.Id
GROUP BY c.CountryName, p.Elevation, p.PeakName, m.MountainRange
ORDER BY c.CountryName, p.PeakName