Table: students
+-------------+----------+ | Column Name | Type | +-------------+----------+ | student_id | int | | name | varchar | | major | varchar | +-------------+----------+ student_id is the primary key for this table. Each row contains the student ID, student name, and their major.
Table: courses
+-------------+-------------------+
| Column Name | Type |
+-------------+-------------------+
| course_id | int |
| name | varchar |
| credits | int |
| major | varchar |
| mandatory | enum |
+-------------+-------------------+
course_id is the primary key for this table.
mandatory is an enum type of ('Yes', 'No').
Each row contains the course ID, course name, credits, major it belongs to, and whether the course is mandatory.
Table: enrollments
+-------------+----------+ | Column Name | Type | +-------------+----------+ | student_id | int | | course_id | int | | semester | varchar | | grade | varchar | | GPA | decimal | +-------------+----------+ (student_id, course_id, semester) is the primary key (combination of columns with unique values) for this table. Each row contains the student ID, course ID, semester, and grade received.
Write a solution to find the students who meet the following criteria:
GPA of at least 2.5 across all their courses (including those outside their major).Return the result table ordered by student_id in ascending order.
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 | mandatory| +-----------+-------------------+---------+------------------+----------+ | 101 | Algorithms | 3 | Computer Science | yes | | 102 | Data Structures | 3 | Computer Science | yes | | 103 | Calculus | 4 | Mathematics | yes | | 104 | Linear Algebra | 4 | Mathematics | yes | | 105 | Machine Learning | 3 | Computer Science | no | | 106 | Probability | 3 | Mathematics | no | | 107 | Operating Systems | 3 | Computer Science | no | | 108 | Statistics | 3 | Mathematics | no | +-----------+-------------------+---------+------------------+----------+
enrollments table:
+------------+-----------+-------------+-------+-----+ | student_id | course_id | semester | grade | GPA | +------------+-----------+-------------+-------+-----+ | 1 | 101 | Fall 2023 | A | 4.0 | | 1 | 102 | Spring 2023 | A | 4.0 | | 1 | 105 | Spring 2023 | A | 4.0 | | 1 | 107 | Fall 2023 | B | 3.5 | | 2 | 101 | Fall 2023 | A | 4.0 | | 2 | 102 | Spring 2023 | B | 3.0 | | 3 | 103 | Fall 2023 | A | 4.0 | | 3 | 104 | Spring 2023 | A | 4.0 | | 3 | 106 | Spring 2023 | A | 4.0 | | 3 | 108 | Fall 2023 | B | 3.5 | | 4 | 103 | Fall 2023 | B | 3.0 | | 4 | 104 | Spring 2023 | B | 3.0 | +------------+-----------+-------------+-------+-----+
Output:
+------------+ | student_id | +------------+ | 1 | | 3 | +------------+
Explanation:
Note: Output table is ordered by student_id in ascending order.
First, we filter out students with an average GPA greater than or equal to 2.5 and record them in table T.
Next, we join the T table with the students table based on student_id, then join with the courses table based on major, and finally perform a left join with the enrollments table based on student_id and course_id.
After that, we group by student ID, use the HAVING clause to filter out students who meet the conditions, and finally sort by student ID.
LeetCode was HARD until I Learned these 15 Patterns • Ashish Pratap Singh • 1,002,296 views views
Watch 9 more video solutions →Practice Find Top Scoring Students II with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor