|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Hard |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README_EN.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3188. Find Top Scoring Students II 🔒](https://leetcode.com/problems/find-top-scoring-students-ii) |
| 10 | + |
| 11 | +[中文文档](/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README.md) |
| 12 | + |
| 13 | +## Description |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>Table: <code>students</code></p> |
| 18 | + |
| 19 | +<pre> |
| 20 | ++-------------+----------+ |
| 21 | +| Column Name | Type | |
| 22 | ++-------------+----------+ |
| 23 | +| student_id | int | |
| 24 | +| name | varchar | |
| 25 | +| major | varchar | |
| 26 | ++-------------+----------+ |
| 27 | +student_id is the primary key for this table. |
| 28 | +Each row contains the student ID, student name, and their major. |
| 29 | +</pre> |
| 30 | + |
| 31 | +<p>Table: <code>courses</code></p> |
| 32 | + |
| 33 | +<pre> |
| 34 | ++-------------+-------------------+ |
| 35 | +| Column Name | Type | |
| 36 | ++-------------+-------------------+ |
| 37 | +| course_id | int | |
| 38 | +| name | varchar | |
| 39 | +| credits | int | |
| 40 | +| major | varchar | |
| 41 | +| mandatory | enum | |
| 42 | ++-------------+-------------------+ |
| 43 | +course_id is the primary key for this table. |
| 44 | +mandatory is an enum type of ('Yes', 'No'). |
| 45 | +Each row contains the course ID, course name, credits, major it belongs to, and whether the course is mandatory. |
| 46 | +</pre> |
| 47 | + |
| 48 | +<p>Table: <code>enrollments</code></p> |
| 49 | + |
| 50 | +<pre> |
| 51 | ++-------------+----------+ |
| 52 | +| Column Name | Type | |
| 53 | ++-------------+----------+ |
| 54 | +| student_id | int | |
| 55 | +| course_id | int | |
| 56 | +| semester | varchar | |
| 57 | +| grade | varchar | |
| 58 | +| GPA | decimal | |
| 59 | ++-------------+----------+ |
| 60 | +(student_id, course_id, semester) is the primary key (combination of columns with unique values) for this table. |
| 61 | +Each row contains the student ID, course ID, semester, and grade received. |
| 62 | +</pre> |
| 63 | + |
| 64 | +<p>Write a solution to find the students who meet the following criteria:</p> |
| 65 | + |
| 66 | +<ul> |
| 67 | + <li>Have<strong> taken all mandatory courses</strong> and <strong>at least two</strong> elective courses offered in <strong>their major.</strong></li> |
| 68 | + <li>Achieved a grade of <strong>A</strong> in <strong>all mandatory courses</strong> and at least <strong>B</strong> in<strong> elective courses</strong>.</li> |
| 69 | + <li>Maintained an average <code>GPA</code> of at least <code>2.5</code> across all their courses (including those outside their major).</li> |
| 70 | +</ul> |
| 71 | + |
| 72 | +<p>Return <em>the result table ordered by</em> <code>student_id</code> <em>in <strong>ascending</strong> order</em>.</p> |
| 73 | + |
| 74 | +<p> </p> |
| 75 | +<p><strong class="example">Example:</strong></p> |
| 76 | + |
| 77 | +<div class="example-block"> |
| 78 | +<p><strong>Input:</strong></p> |
| 79 | + |
| 80 | +<p>students table:</p> |
| 81 | + |
| 82 | +<pre class="example-io"> |
| 83 | + +------------+------------------+------------------+ |
| 84 | + | student_id | name | major | |
| 85 | + +------------+------------------+------------------+ |
| 86 | + | 1 | Alice | Computer Science | |
| 87 | + | 2 | Bob | Computer Science | |
| 88 | + | 3 | Charlie | Mathematics | |
| 89 | + | 4 | David | Mathematics | |
| 90 | + +------------+------------------+------------------+ |
| 91 | + </pre> |
| 92 | + |
| 93 | +<p>courses table:</p> |
| 94 | + |
| 95 | +<pre class="example-io"> |
| 96 | + +-----------+-------------------+---------+------------------+----------+ |
| 97 | + | course_id | name | credits | major | mandatory| |
| 98 | + +-----------+-------------------+---------+------------------+----------+ |
| 99 | + | 101 | Algorithms | 3 | Computer Science | yes | |
| 100 | + | 102 | Data Structures | 3 | Computer Science | yes | |
| 101 | + | 103 | Calculus | 4 | Mathematics | yes | |
| 102 | + | 104 | Linear Algebra | 4 | Mathematics | yes | |
| 103 | + | 105 | Machine Learning | 3 | Computer Science | no | |
| 104 | + | 106 | Probability | 3 | Mathematics | no | |
| 105 | + | 107 | Operating Systems | 3 | Computer Science | no | |
| 106 | + | 108 | Statistics | 3 | Mathematics | no | |
| 107 | + +-----------+-------------------+---------+------------------+----------+ |
| 108 | + </pre> |
| 109 | + |
| 110 | +<p>enrollments table:</p> |
| 111 | + |
| 112 | +<pre class="example-io"> |
| 113 | + +------------+-----------+-------------+-------+-----+ |
| 114 | + | student_id | course_id | semester | grade | GPA | |
| 115 | + +------------+-----------+-------------+-------+-----+ |
| 116 | + | 1 | 101 | Fall 2023 | A | 4.0 | |
| 117 | + | 1 | 102 | Spring 2023 | A | 4.0 | |
| 118 | + | 1 | 105 | Spring 2023 | A | 4.0 | |
| 119 | + | 1 | 107 | Fall 2023 | B | 3.5 | |
| 120 | + | 2 | 101 | Fall 2023 | A | 4.0 | |
| 121 | + | 2 | 102 | Spring 2023 | B | 3.0 | |
| 122 | + | 3 | 103 | Fall 2023 | A | 4.0 | |
| 123 | + | 3 | 104 | Spring 2023 | A | 4.0 | |
| 124 | + | 3 | 106 | Spring 2023 | A | 4.0 | |
| 125 | + | 3 | 108 | Fall 2023 | B | 3.5 | |
| 126 | + | 4 | 103 | Fall 2023 | B | 3.0 | |
| 127 | + | 4 | 104 | Spring 2023 | B | 3.0 | |
| 128 | + +------------+-----------+-------------+-------+-----+ |
| 129 | + </pre> |
| 130 | + |
| 131 | +<p><strong>Output:</strong></p> |
| 132 | + |
| 133 | +<pre class="example-io"> |
| 134 | + +------------+ |
| 135 | + | student_id | |
| 136 | + +------------+ |
| 137 | + | 1 | |
| 138 | + | 3 | |
| 139 | + +------------+ |
| 140 | + </pre> |
| 141 | + |
| 142 | +<p><strong>Explanation:</strong></p> |
| 143 | + |
| 144 | +<ul> |
| 145 | + <li>Alice (student_id 1) is a Computer Science major and has taken both Algorithms and Data Structures, receiving an A in both. She has also taken Machine Learning and Operating Systems as electives, receiving an A and B respectively.</li> |
| 146 | + <li>Bob (student_id 2) is a Computer Science major but did not receive an A in all required courses.</li> |
| 147 | + <li>Charlie (student_id 3) is a Mathematics major and has taken both Calculus and Linear Algebra, receiving an A in both. He has also taken Probability and Statistics as electives, receiving an A and B respectively.</li> |
| 148 | + <li>David (student_id 4) is a Mathematics major but did not receive an A in all required courses.</li> |
| 149 | +</ul> |
| 150 | + |
| 151 | +<p><strong>Note:</strong> Output table is ordered by student_id in ascending order.</p> |
| 152 | +</div> |
| 153 | + |
| 154 | +<!-- description:end --> |
| 155 | + |
| 156 | +## Solutions |
| 157 | + |
| 158 | +<!-- solution:start --> |
| 159 | + |
| 160 | +### Solution 1: Joining + Grouping + Conditional Filtering |
| 161 | + |
| 162 | +First, we calculate the average GPA of each student and store it in a temporary table `T`. |
| 163 | + |
| 164 | +Next, we join the `students` table with the `courses` table based on `major`, and then join with the `T` table based on `student_id`, followed by a left join with the `enrollments` table based on `student_id` and `course_id`. |
| 165 | + |
| 166 | +After that, we filter out students with an average GPA greater than or equal to 2.5, group by student ID, use the `HAVING` clause to filter students who meet the criteria, and finally sort by student ID. |
| 167 | + |
| 168 | +<!-- tabs:start --> |
| 169 | + |
| 170 | +#### MySQL |
| 171 | + |
| 172 | +```sql |
| 173 | +# Write your MySQL query statement below |
| 174 | +WITH |
| 175 | + T AS ( |
| 176 | + SELECT student_id, AVG(GPA) AS avg_gpa |
| 177 | + FROM enrollments |
| 178 | + GROUP BY 1 |
| 179 | + ) |
| 180 | +SELECT student_id |
| 181 | +FROM |
| 182 | + students |
| 183 | + JOIN courses USING (major) |
| 184 | + JOIN T USING (student_id) |
| 185 | + LEFT JOIN enrollments USING (student_id, course_id) |
| 186 | +WHERE avg_gpa >= 2.5 |
| 187 | +GROUP BY 1 |
| 188 | +HAVING |
| 189 | + SUM(mandatory = 'yes' AND grade = 'A') = SUM(mandatory = 'yes') |
| 190 | + AND SUM(mandatory = 'no' AND grade IS NOT NULL) = SUM(mandatory = 'no' AND grade IN ('A', 'B')) |
| 191 | + AND SUM(mandatory = 'no' AND grade IS NOT NULL) >= 2 |
| 192 | +ORDER BY 1; |
| 193 | +``` |
| 194 | + |
| 195 | +<!-- tabs:end --> |
| 196 | + |
| 197 | +<!-- solution:end --> |
| 198 | + |
| 199 | +<!-- problem:end --> |
0 commit comments