1
+ -- Question 23
2
+ -- Table: Students
3
+
4
+ -- +---------------+---------+
5
+ -- | Column Name | Type |
6
+ -- +---------------+---------+
7
+ -- | student_id | int |
8
+ -- | student_name | varchar |
9
+ -- +---------------+---------+
10
+ -- student_id is the primary key for this table.
11
+ -- Each row of this table contains the ID and the name of one student in the school.
12
+
13
+
14
+ -- Table: Subjects
15
+
16
+ -- +--------------+---------+
17
+ -- | Column Name | Type |
18
+ -- +--------------+---------+
19
+ -- | subject_name | varchar |
20
+ -- +--------------+---------+
21
+ -- subject_name is the primary key for this table.
22
+ -- Each row of this table contains the name of one subject in the school.
23
+
24
+
25
+ -- Table: Examinations
26
+
27
+ -- +--------------+---------+
28
+ -- | Column Name | Type |
29
+ -- +--------------+---------+
30
+ -- | student_id | int |
31
+ -- | subject_name | varchar |
32
+ -- +--------------+---------+
33
+ -- There is no primary key for this table. It may contain duplicates.
34
+ -- Each student from the Students table takes every course from Subjects table.
35
+ -- Each row of this table indicates that a student with ID student_id attended the exam of subject_name.
36
+
37
+
38
+ -- Write an SQL query to find the number of times each student attended each exam.
39
+
40
+ -- Order the result table by student_id and subject_name.
41
+
42
+ -- The query result format is in the following example:
43
+
44
+ -- Students table:
45
+ -- +------------+--------------+
46
+ -- | student_id | student_name |
47
+ -- +------------+--------------+
48
+ -- | 1 | Alice |
49
+ -- | 2 | Bob |
50
+ -- | 13 | John |
51
+ -- | 6 | Alex |
52
+ -- +------------+--------------+
53
+ -- Subjects table:
54
+ -- +--------------+
55
+ -- | subject_name |
56
+ -- +--------------+
57
+ -- | Math |
58
+ -- | Physics |
59
+ -- | Programming |
60
+ -- +--------------+
61
+ -- Examinations table:
62
+ -- +------------+--------------+
63
+ -- | student_id | subject_name |
64
+ -- +------------+--------------+
65
+ -- | 1 | Math |
66
+ -- | 1 | Physics |
67
+ -- | 1 | Programming |
68
+ -- | 2 | Programming |
69
+ -- | 1 | Physics |
70
+ -- | 1 | Math |
71
+ -- | 13 | Math |
72
+ -- | 13 | Programming |
73
+ -- | 13 | Physics |
74
+ -- | 2 | Math |
75
+ -- | 1 | Math |
76
+ -- +------------+--------------+
77
+ -- Result table:
78
+ -- +------------+--------------+--------------+----------------+
79
+ -- | student_id | student_name | subject_name | attended_exams |
80
+ -- +------------+--------------+--------------+----------------+
81
+ -- | 1 | Alice | Math | 3 |
82
+ -- | 1 | Alice | Physics | 2 |
83
+ -- | 1 | Alice | Programming | 1 |
84
+ -- | 2 | Bob | Math | 1 |
85
+ -- | 2 | Bob | Physics | 0 |
86
+ -- | 2 | Bob | Programming | 1 |
87
+ -- | 6 | Alex | Math | 0 |
88
+ -- | 6 | Alex | Physics | 0 |
89
+ -- | 6 | Alex | Programming | 0 |
90
+ -- | 13 | John | Math | 1 |
91
+ -- | 13 | John | Physics | 1 |
92
+ -- | 13 | John | Programming | 1 |
93
+ -- +------------+--------------+--------------+----------------+
94
+ -- The result table should contain all students and all subjects.
95
+ -- Alice attended Math exam 3 times, Physics exam 2 times and Programming exam 1 time.
96
+ -- Bob attended Math exam 1 time, Programming exam 1 time and didn't attend the Physics exam.
97
+ -- Alex didn't attend any exam.
98
+ -- John attended Math exam 1 time, Physics exam 1 time and Programming exam 1 time.
99
+
100
+ -- Solution
101
+ Select a .student_id as student_id, a .student_name as student_name, a .subject_name as subject_name, coalesce(attended_exams,0 ) as attended_exams
102
+ from (
103
+ select *
104
+ from students
105
+ cross join subjects
106
+ group by student_id, student_name, subject_name) a
107
+ left join
108
+ (Select e .student_id , student_name, subject_name, count (* ) as attended_exams
109
+ from examinations e join students s
110
+ on e .student_id = s .student_id
111
+ group by e .student_id , student_name, subject_name) b
112
+ on a .student_id = b .student_id and a .subject_name = b .subject_name
113
+ order by a .student_id asc , a .subject_name asc
0 commit comments