|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](https://github.com/openset/leetcode/tree/master/problems/traffic-light-controlled-intersection "Traffic Light Controlled Intersection") |
| 9 | + |
| 10 | +[Next >](https://github.com/openset/leetcode/tree/master/problems/subtract-the-product-and-sum-of-digits-of-an-integer "Subtract the Product and Sum of Digits of an Integer") |
| 11 | + |
| 12 | +## [1280. Students and Examinations (Easy)](https://leetcode.com/problems/students-and-examinations "") |
| 13 | + |
| 14 | +<p>Table: <code>Students</code></p> |
| 15 | +<pre> |
| 16 | ++---------------+---------+ |
| 17 | +| Column Name | Type | |
| 18 | ++---------------+---------+ |
| 19 | +| student_id | int | |
| 20 | +| student_name | varchar | |
| 21 | ++---------------+---------+ |
| 22 | +student_id is the primary key for this table. |
| 23 | +Each row of this table contains the ID and the name of one student in the school. |
| 24 | +</pre> |
| 25 | + |
| 26 | +<p>Table: <code>Subjects</code></p> |
| 27 | +<pre> |
| 28 | ++--------------+---------+ |
| 29 | +| Column Name | Type | |
| 30 | ++--------------+---------+ |
| 31 | +| subject_name | varchar | |
| 32 | ++--------------+---------+ |
| 33 | +subject_name is the primary key for this table. |
| 34 | +Each row of this table contains a name of one subject in the school. |
| 35 | +</pre> |
| 36 | + |
| 37 | +<p>Table: <code>Examinations</code></p> |
| 38 | +<pre> |
| 39 | ++--------------+---------+ |
| 40 | +| Column Name | Type | |
| 41 | ++--------------+---------+ |
| 42 | +| student_id | int | |
| 43 | +| subject_name | varchar | |
| 44 | ++--------------+---------+ |
| 45 | +There is no primary key for this table. It may contain duplicates. |
| 46 | +Each student from Students table takes every course from Subjects table. |
| 47 | +Each row of this table indicates that a student with ID student_id attended the exam of subject_name. |
| 48 | +</pre> |
| 49 | + |
| 50 | +Write an SQL query to find the number of times each student attended each exam. |
| 51 | + |
| 52 | +Order the result table by student_id and subject_name. |
| 53 | + |
| 54 | +The query result format is in the following example: |
| 55 | +<pre> |
| 56 | +Students table: |
| 57 | ++------------+--------------+ |
| 58 | +| student_id | student_name | |
| 59 | ++------------+--------------+ |
| 60 | +| 1 | Alice | |
| 61 | +| 2 | Bob | |
| 62 | +| 13 | John | |
| 63 | +| 6 | Alex | |
| 64 | ++------------+--------------+ |
| 65 | +Subjects table: |
| 66 | ++--------------+ |
| 67 | +| subject_name | |
| 68 | ++--------------+ |
| 69 | +| Math | |
| 70 | +| Physics | |
| 71 | +| Programming | |
| 72 | ++--------------+ |
| 73 | +Examinations table: |
| 74 | ++------------+--------------+ |
| 75 | +| student_id | subject_name | |
| 76 | ++------------+--------------+ |
| 77 | +| 1 | Math | |
| 78 | +| 1 | Physics | |
| 79 | +| 1 | Programming | |
| 80 | +| 2 | Programming | |
| 81 | +| 1 | Physics | |
| 82 | +| 1 | Math | |
| 83 | +| 13 | Math | |
| 84 | +| 13 | Programming | |
| 85 | +| 13 | Physics | |
| 86 | +| 2 | Math | |
| 87 | +| 1 | Math | |
| 88 | ++------------+--------------+ |
| 89 | +Result table: |
| 90 | ++------------+--------------+--------------+----------------+ |
| 91 | +| student_id | student_name | subject_name | attended_exams | |
| 92 | ++------------+--------------+--------------+----------------+ |
| 93 | +| 1 | Alice | Math | 3 | |
| 94 | +| 1 | Alice | Physics | 2 | |
| 95 | +| 1 | Alice | Programming | 1 | |
| 96 | +| 2 | Bob | Math | 1 | |
| 97 | +| 2 | Bob | Physics | 0 | |
| 98 | +| 2 | Bob | Programming | 1 | |
| 99 | +| 6 | Alex | Math | 0 | |
| 100 | +| 6 | Alex | Physics | 0 | |
| 101 | +| 6 | Alex | Programming | 0 | |
| 102 | +| 13 | John | Math | 1 | |
| 103 | +| 13 | John | Physics | 1 | |
| 104 | +| 13 | John | Programming | 1 | |
| 105 | ++------------+--------------+--------------+----------------+ |
| 106 | +The result table should contain all students and all subjects. |
| 107 | +Alice attended Math exam 3 times, Physics exam 2 times and Programming exam 1 time. |
| 108 | +Bob attended Math exam 1 time, Programming exam 1 time and didn't attend the Physics exam. |
| 109 | +Alex didn't attend any exam. |
| 110 | +John attended Math exam 1 time, Physics exam 1 time and Programming exam 1 time. |
| 111 | +</pre> |
0 commit comments