返回介绍

solution / 1200-1299 / 1280.Students and Examinations / README_EN

发布于 2024-06-17 01:03:21 字数 4852 浏览 0 评论 0 收藏 0

1280. Students and Examinations

中文文档

Description

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.

The result format is in the following example.

 

Example 1:

Input: 
Students table:
+------------+--------------+
| student_id | student_name |
+------------+--------------+
| 1      | Alice    |
| 2      | Bob      |
| 13     | John     |
| 6      | Alex     |
+------------+--------------+
Subjects table:
+--------------+
| subject_name |
+--------------+
| Math     |
| Physics    |
| Programming  |
+--------------+
Examinations table:
+------------+--------------+
| student_id | subject_name |
+------------+--------------+
| 1      | Math     |
| 1      | Physics    |
| 1      | Programming  |
| 2      | Programming  |
| 1      | Physics    |
| 1      | Math     |
| 13     | Math     |
| 13     | Programming  |
| 13     | Physics    |
| 2      | Math     |
| 1      | Math     |
+------------+--------------+
Output: 
+------------+--------------+--------------+----------------+
| student_id | student_name | subject_name | attended_exams |
+------------+--------------+--------------+----------------+
| 1      | Alice    | Math     | 3        |
| 1      | Alice    | Physics    | 2        |
| 1      | Alice    | Programming  | 1        |
| 2      | Bob      | Math     | 1        |
| 2      | Bob      | Physics    | 0        |
| 2      | Bob      | Programming  | 1        |
| 6      | Alex     | Math     | 0        |
| 6      | Alex     | Physics    | 0        |
| 6      | Alex     | Programming  | 0        |
| 13     | John     | Math     | 1        |
| 13     | John     | Physics    | 1        |
| 13     | John     | Programming  | 1        |
+------------+--------------+--------------+----------------+
Explanation: 
The result table should contain all students and all subjects.
Alice attended the Math exam 3 times, the Physics exam 2 times, and the Programming exam 1 time.
Bob attended the Math exam 1 time, the Programming exam 1 time, and did not attend the Physics exam.
Alex did not attend any exams.
John attended the Math exam 1 time, the Physics exam 1 time, and the Programming exam 1 time.

Solutions

Solution 1: Two Joins + Grouping

We can first join the Students table and the Subjects table to obtain all combinations of students and subjects, and then join the Examinations table with the condition of student_id and subject_name. This way, we can get the number of times each student has taken each subject's test. Finally, we can group by student_id and subject_name to count the number of times each student has taken each subject's test.

# Write your MySQL query statement below
SELECT student_id, student_name, subject_name, COUNT(e.student_id) AS attended_exams
FROM
  Students
  JOIN Subjects
  LEFT JOIN Examinations AS e USING (student_id, subject_name)
GROUP BY 1, 3
ORDER BY 1, 3;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文