|
| 1 | +/* |
| 2 | +Question 1280. Students and Examinations |
| 3 | +Link: https://leetcode.com/problems/students-and-examinations/description/?envType=study-plan-v2&envId=top-sql-50 |
| 4 | +
|
| 5 | +Table: Students |
| 6 | +
|
| 7 | ++---------------+---------+ |
| 8 | +| Column Name | Type | |
| 9 | ++---------------+---------+ |
| 10 | +| student_id | int | |
| 11 | +| student_name | varchar | |
| 12 | ++---------------+---------+ |
| 13 | +student_id is the primary key (column with unique values) for this table. |
| 14 | +Each row of this table contains the ID and the name of one student in the school. |
| 15 | + |
| 16 | +
|
| 17 | +Table: Subjects |
| 18 | +
|
| 19 | ++--------------+---------+ |
| 20 | +| Column Name | Type | |
| 21 | ++--------------+---------+ |
| 22 | +| subject_name | varchar | |
| 23 | ++--------------+---------+ |
| 24 | +subject_name is the primary key (column with unique values) for this table. |
| 25 | +Each row of this table contains the name of one subject in the school. |
| 26 | + |
| 27 | +
|
| 28 | +Table: Examinations |
| 29 | +
|
| 30 | ++--------------+---------+ |
| 31 | +| Column Name | Type | |
| 32 | ++--------------+---------+ |
| 33 | +| student_id | int | |
| 34 | +| subject_name | varchar | |
| 35 | ++--------------+---------+ |
| 36 | +There is no primary key (column with unique values) for this table. It may contain duplicates. |
| 37 | +Each student from the Students table takes every course from the Subjects table. |
| 38 | +Each row of this table indicates that a student with ID student_id attended the exam of subject_name. |
| 39 | + |
| 40 | +
|
| 41 | +Write a solution to find the number of times each student attended each exam. |
| 42 | +
|
| 43 | +Return the result table ordered by student_id and subject_name. |
| 44 | +*/ |
| 45 | + |
| 46 | +SELECT |
| 47 | + st.student_id, |
| 48 | + st.student_name, |
| 49 | + s.subject_name, |
| 50 | + COUNT(e.student_id) AS attended_exams |
| 51 | +FROM Students AS st |
| 52 | +CROSS JOIN Subjects AS s |
| 53 | +LEFT JOIN |
| 54 | + Examinations AS e |
| 55 | + ON |
| 56 | + st.student_id = e.student_id |
| 57 | + AND s.subject_name = e.subject_name |
| 58 | +GROUP BY st.student_id, st.student_name, s.subject_name |
| 59 | +ORDER BY st.student_id, st.student_name, s.subject_name |
0 commit comments