Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
59 changes: 59 additions & 0 deletions leetcode/easy/1280. Students and Examinations.sql
Original file line number Diff line number Diff line change
@@ -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