diff --git a/README.md b/README.md index ca188b7..6b9e040 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ Have a good contributing! - [1148. Article Views I](./leetcode/easy/1148.%20Article%20Views%20I.sql) - [1211. Queries Quality and Percentage](./leetcode/easy/1211.%20Queries%20Quality%20and%20Percentage.sql) - [1251. Average Selling Price](./leetcode/easy/1251.%20Average%20Selling%20Price.sql) + - [1280. Students and Examinations](./leetcode/easy/1280.%20Students%20and%20Examinations.sql) - [1327. List the Products Ordered in a Period](./leetcode/easy/1327.%20List%20the%20Products%20Ordered%20in%20a%20Period.sql) - [1378. Replace Employee ID With The Unique Identifier](./leetcode/easy/1378.%20Replace%20Employee%20ID%20With%20The%20Unique%20Identifier.sql) - [1407. Top Travellers](./leetcode/easy/1407.%20Top%20Travellers.sql) diff --git a/leetcode/easy/1280. Students and Examinations.sql b/leetcode/easy/1280. Students and Examinations.sql new file mode 100644 index 0000000..ee4995d --- /dev/null +++ b/leetcode/easy/1280. Students and Examinations.sql @@ -0,0 +1,59 @@ +/* +Question 1280. Students and Examinations +Link: https://leetcode.com/problems/students-and-examinations/description/?envType=study-plan-v2&envId=top-sql-50 + +Table: Students + ++---------------+---------+ +| Column Name | Type | ++---------------+---------+ +| student_id | int | +| student_name | varchar | ++---------------+---------+ +student_id is the primary key (column with unique values) for this table. +Each row of this table contains the ID and the name of one student in the school. + + +Table: Subjects + ++--------------+---------+ +| Column Name | Type | ++--------------+---------+ +| subject_name | varchar | ++--------------+---------+ +subject_name is the primary key (column with unique values) for this table. +Each row of this table contains the name of one subject in the school. + + +Table: Examinations + ++--------------+---------+ +| Column Name | Type | ++--------------+---------+ +| student_id | int | +| subject_name | varchar | ++--------------+---------+ +There is no primary key (column with unique values) for this table. It may contain duplicates. +Each student from the Students table takes every course from the Subjects table. +Each row of this table indicates that a student with ID student_id attended the exam of subject_name. + + +Write a solution to find the number of times each student attended each exam. + +Return the result table ordered by student_id and subject_name. +*/ + +SELECT + st.student_id, + st.student_name, + s.subject_name, + COUNT(e.student_id) AS attended_exams +FROM Students AS st +CROSS JOIN Subjects AS s +LEFT JOIN + Examinations AS e + ON + st.student_id = e.student_id + AND s.subject_name = e.subject_name +GROUP BY st.student_id, st.student_name, s.subject_name +ORDER BY st.student_id, st.student_name, s.subject_name