-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3182-FindTopScoringStudents.sql
More file actions
172 lines (164 loc) · 6.84 KB
/
3182-FindTopScoringStudents.sql
File metadata and controls
172 lines (164 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
-- 3182. Find Top Scoring Students
-- Table: students
-- +-------------+----------+
-- | Column Name | Type |
-- +-------------+----------+
-- | student_id | int |
-- | name | varchar |
-- | major | varchar |
-- +-------------+----------+
-- student_id is the primary key (combination of columns with unique values) for this table.
-- Each row of this table contains the student ID, student name, and their major.
-- Table: courses
-- +-------------+----------+
-- | Column Name | Type |
-- +-------------+----------+
-- | course_id | int |
-- | name | varchar |
-- | credits | int |
-- | major | varchar |
-- +-------------+----------+
-- course_id is the primary key (combination of columns with unique values) for this table.
-- Each row of this table contains the course ID, course name, the number of credits for the course, and the major it belongs to.
-- Table: enrollments
-- +-------------+----------+
-- | Column Name | Type |
-- +-------------+----------+
-- | student_id | int |
-- | course_id | int |
-- | semester | varchar |
-- | grade | varchar |
-- +-------------+----------+
-- (student_id, course_id, semester) is the primary key (combination of columns with unique values) for this table.
-- Each row of this table contains the student ID, course ID, semester, and grade received.
-- Write a solution to find the students who have taken all courses offered in their major and have achieved a grade of A in all these courses.
-- Return the result table ordered by student_id in ascending order.
-- The result format is in the following example.
-- Example:
-- Input:
-- students table:
-- +------------+------------------+------------------+
-- | student_id | name | major |
-- +------------+------------------+------------------+
-- | 1 | Alice | Computer Science |
-- | 2 | Bob | Computer Science |
-- | 3 | Charlie | Mathematics |
-- | 4 | David | Mathematics |
-- +------------+------------------+------------------+
-- courses table:
-- +-----------+-----------------+---------+------------------+
-- | course_id | name | credits | major |
-- +-----------+-----------------+---------+------------------+
-- | 101 | Algorithms | 3 | Computer Science |
-- | 102 | Data Structures | 3 | Computer Science |
-- | 103 | Calculus | 4 | Mathematics |
-- | 104 | Linear Algebra | 4 | Mathematics |
-- +-----------+-----------------+---------+------------------+
-- enrollments table:
-- +------------+-----------+----------+-------+
-- | student_id | course_id | semester | grade |
-- +------------+-----------+----------+-------+
-- | 1 | 101 | Fall 2023| A |
-- | 1 | 102 | Fall 2023| A |
-- | 2 | 101 | Fall 2023| B |
-- | 2 | 102 | Fall 2023| A |
-- | 3 | 103 | Fall 2023| A |
-- | 3 | 104 | Fall 2023| A |
-- | 4 | 103 | Fall 2023| A |
-- | 4 | 104 | Fall 2023| B |
-- +------------+-----------+----------+-------+
-- Output:
-- +------------+
-- | student_id |
-- +------------+
-- | 1 |
-- | 3 |
-- +------------+
-- Explanation:
-- Alice (student_id 1) is a Computer Science major and has taken both "Algorithms" and "Data Structures", receiving an 'A' in both.
-- Bob (student_id 2) is a Computer Science major but did not receive an 'A' in all required courses.
-- Charlie (student_id 3) is a Mathematics major and has taken both "Calculus" and "Linear Algebra", receiving an 'A' in both.
-- David (student_id 4) is a Mathematics major but did not receive an 'A' in all required courses.
-- Note: Output table is ordered by student_id in ascending order.
-- CREATE TABLE if not exists students (
-- student_id INT ,
-- name VARCHAR(255),
-- major VARCHAR(255)
-- )
-- CREATE TABLE if not exists courses (
-- course_id INT ,
-- name VARCHAR(255),
-- credits INT,
-- major VARCHAR(255)
-- )
-- CREATE TABLE if not exists enrollments (
-- student_id INT,
-- course_id INT,
-- semester VARCHAR(255),
-- grade VARCHAR(10)
-- );
-- Truncate table students
-- insert into students (student_id, name, major) values ('1', 'Alice', 'Computer Science')
-- insert into students (student_id, name, major) values ('2', 'Bob', 'Computer Science')
-- insert into students (student_id, name, major) values ('3', 'Charlie', 'Mathematics')
-- insert into students (student_id, name, major) values ('4', 'David', 'Mathematics')
-- Truncate table courses
-- insert into courses (course_id, name, credits, major) values ('101', 'Algorithms', '3', 'Computer Science')
-- insert into courses (course_id, name, credits, major) values ('102', 'Data Structures', '3', 'Computer Science')
-- insert into courses (course_id, name, credits, major) values ('103', 'Calculus', '4', 'Mathematics')
-- insert into courses (course_id, name, credits, major) values ('104', 'Linear Algebra', '4', 'Mathematics')
-- Truncate table enrollments
-- insert into enrollments (student_id, course_id, semester, grade) values ('1', '101', 'Fall 2023', 'A')
-- insert into enrollments (student_id, course_id, semester, grade) values ('1', '102', 'Fall 2023', 'A')
-- insert into enrollments (student_id, course_id, semester, grade) values ('2', '101', 'Fall 2023', 'B')
-- insert into enrollments (student_id, course_id, semester, grade) values ('2', '102', 'Fall 2023', 'A')
-- insert into enrollments (student_id, course_id, semester, grade) values ('3', '103', 'Fall 2023', 'A')
-- insert into enrollments (student_id, course_id, semester, grade) values ('3', '104', 'Fall 2023', 'A')
-- insert into enrollments (student_id, course_id, semester, grade) values ('4', '103', 'Fall 2023', 'A')
-- insert into enrollments (student_id, course_id, semester, grade) values ('4', '104', 'Fall 2023', 'B')
# Write your MySQL query statement below
-- SELECT
-- s.student_id,
-- -- c.course_id,
-- -- e.grade,
-- count(*) AS cnt,
-- count(IF(e.grade = "A", 1, null)) AS cnta
-- FROM
-- students AS s
-- LEFT JOIN
-- courses AS c
-- ON
-- c.major = s.major
-- LEFT JOIN
-- enrollments AS e
-- ON
-- e.student_id = s.student_id AND c.course_id = e.course_id
-- GROUP BY
-- s.student_id
SELECT
student_id
FROM
(
SELECT
s.student_id,
-- c.course_id,
-- e.grade,
count(*) AS cnt,
count(IF(e.grade = "A", 1, null)) AS cnta
FROM
students AS s
LEFT JOIN
courses AS c
ON
c.major = s.major
LEFT JOIN
enrollments AS e
ON
e.student_id = s.student_id AND c.course_id = e.course_id
GROUP BY
s.student_id
) AS t
WHERE
cnt = cnta
ORDER BY
student_id