-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.sql
208 lines (178 loc) · 5.27 KB
/
task.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
--1
SELECT DISTINCT primary_skill
FROM student s
WHERE primary_skill like '% %'
OR primary_skill like '%-%';
--2
SELECT *
FROM student s
WHERE surname like '%.%'
OR length(surname) = 1;
--3
SELECT phone_numbers
FROM student s
INNER JOIN results r ON s.id = r.student_id
WHERE mark > 7
ORDER BY phone_numbers DESC
--4
SELECT subquery2.std_id
FROM
(SELECT rst.student_id AS std_id,
COUNT (rst.mark) AS count_mark
FROM results rst
GROUP BY rst.student_id,
rst.mark) subquery1
INNER JOIN
(SELECT rst.student_id AS std_id,
COUNT (rst.mark) AS count_mark
FROM results rst
GROUP BY rst.student_id) subquery2 ON subquery1.std_id = subquery2.std_id
AND subquery1.count_mark = subquery2.count_mark
WHERE subquery2.count_mark > 1
ORDER BY subquery2.std_id ASC
--5
SELECT st.id AS student_id
FROM student st
INNER JOIN results rs ON st.id = rs.student_id
WHERE rs.mark > 7 GROUP BY st.id
HAVING Count(rs.student_id) >= 2
--6
SELECT rst.student_id AS std_id
FROM results rst
WHERE rst.mark > 7
GROUP BY rst.subject_id,
rst.student_id
HAVING count(rst.mark) > 1
--7
SELECT rst.subject_id AS sbj_id
FROM student std
INNER JOIN results rst ON std.id = rst.student_id
WHERE rst.mark > 7
GROUP BY rst.subject_id
HAVING COUNT (rst.student_id) > 1
AND count(DISTINCT std.primary_skill) = 1
--8
SELECT sbj_id
FROM
(SELECT count(DISTINCT std.primary_skill) AS count_skills,
rst.subject_id AS sbj_id,
COUNT (rst.student_id) AS count_students
FROM student std
INNER JOIN results rst ON std.id = rst.student_id
WHERE rst.mark > 7
GROUP BY rst.subject_id
ORDER BY count_skills DESC) AS subquery
WHERE subquery.count_skills = subquery.count_students
--9
--outer join
SELECT s
FROM
(SELECT rs.student_id AS student_id,
Count(rs.student_id) AS count_marks
FROM results rs
WHERE rs.mark <= 7
GROUP BY rs.student_id) subquary1
FULL
OUTER JOIN
(SELECT rs.student_id AS student_id,
Count(rs.student_id) AS count_marks
FROM results rs
GROUP BY rs.student_id) subquary2 ON (subquary1.student_id = subquary2.student_id
AND subquary1.count_marks = subquary2.count_marks)
FULL
OUTER JOIN student s ON subquary1.student_id = s.id
WHERE (subquary1.student_id IS NOT NULL
OR subquary1.count_marks IS NOT NULL)
AND (subquary2.student_id IS NOT NULL
OR subquary2.count_marks IS NOT NULL)
--not in
SELECT std
FROM student std
INNER JOIN results rst ON std.id = rst.student_id
WHERE std.id not in
(SELECT rs.student_id AS student_id
FROM results rs
WHERE rs.mark > 7
GROUP BY rs.student_id)
GROUP BY std.id
--any
SELECT std
FROM student std
INNER JOIN results rst ON std.id = rst.student_id
WHERE std.id = any
(SELECT rs.student_id AS student_id
FROM results rs
WHERE rs.mark <= 7
GROUP BY rs.student_id)
GROUP BY std.id
--10
SELECT st
FROM student st
INNER JOIN
(SELECT rst.student_id AS std_id,
avg(rst.mark) AS avg_std_mark
FROM results rst
GROUP BY rst.student_id) subquery ON st.id = subquery.std_id
WHERE subquery.avg_std_mark >
(SELECT avg(rs.mark)
FROM results rs)
--11
SELECT st
FROM student st
INNER JOIN
(SELECT rst.student_id AS std_id,
max(rst.mark) AS std_mark
FROM results rst
WHERE rst.mark > 7
GROUP BY rst.student_id,
rst.mark
ORDER BY rst.mark ASC) subquery ON st.id = subquery.std_id
WHERE subquery.std_mark >
(SELECT avg(rs.mark)
FROM results rs)
LIMIT 5
--12
SELECT std.id,
coalesce((CASE
WHEN subquery.std_mark = 1
OR subquery.std_mark = 2
OR subquery.std_mark = 3 THEN 'bad'
WHEN subquery.std_mark = 4
OR subquery.std_mark = 5
OR subquery.std_mark = 6 THEN 'average'
WHEN subquery.std_mark = 7
OR subquery.std_mark = 8 THEN 'good'
WHEN subquery.std_mark = 9
OR subquery.std_mark = 10 THEN 'excellent'
END), 'not passed') AS mark_desc
FROM student std
LEFT JOIN
(SELECT rst.student_id AS std_id,
max(rst.mark) AS std_mark
FROM results rst
GROUP BY rst.student_id) subquery ON std.id = subquery.std_id
--13
SELECT mark_desc,
count(mark_desc)
FROM
(SELECT std.id,
(CASE
WHEN subquery.std_mark = 1
OR subquery.std_mark = 2
OR subquery.std_mark = 3 THEN 'bad'
WHEN subquery.std_mark = 4
OR subquery.std_mark = 5
OR subquery.std_mark = 6 THEN 'average'
WHEN subquery.std_mark = 7
OR subquery.std_mark = 8 THEN 'good'
WHEN subquery.std_mark = 9
OR subquery.std_mark = 10 THEN 'excellent'
ELSE 'not passed'
END) AS mark_desc
FROM student std
LEFT JOIN
(SELECT rst.student_id AS std_id,
max(rst.mark) AS std_mark
FROM results rst
GROUP BY rst.student_id) subquery ON std.id = subquery.std_id) subquery1
GROUP BY mark_desc