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:
Note: Output table is ordered by student_id in ascending order.
Solutions for this problem are being prepared.
Try solving it yourselfThe unfair way I got good at Leetcode • Dave Burji • 596,394 views views
Watch 9 more video solutions →Practice Find Top Scoring Students with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor