-
Notifications
You must be signed in to change notification settings - Fork 4
/
01280-students-and-examinations.sql
36 lines (28 loc) · 1.11 KB
/
01280-students-and-examinations.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
-- first, create a cross table between students and subjects
-- then, left join Examination table, and count from Examination table
-- group by and order by
-- BETTER TIME COMPLEXITY from Leetcode
with Cross_All as (
select st.student_id, st.student_name, sb.subject_name
from Students st cross join Subjects sb
)
select c.student_id, c.student_name, c.subject_name, count(e.subject_name) as attended_exams
from Cross_All c
left join Examinations e
on c.student_id = e.student_id
and c.subject_name = e.subject_name
group by student_id, subject_name
order by student_id, subject_name
-------------------------------------------------------------------------------------------------------------------
-- same as above, but code is simplified. Removed a layer, joined all 3 tables
select st.student_id, st.student_name, sb.subject_name, count(e.subject_name) as attended_exams
from Students st
cross join Subjects sb
left join Examinations e
on st.student_id = e.student_id
and sb.subject_name = e.subject_name
group by student_id, subject_name
order by student_id, subject_name
-- amazon- 3
-- yahoo- 2
-- roblox- 1