Find The Quiet Students In All Exams Problem


Description

LeetCode Problem 1412.

Table: Student

1
2
3
4
5
6
7
8
+---------------------+---------+
| Column Name         | Type    |
+---------------------+---------+
| student_id          | int     |
| student_name        | varchar |
+---------------------+---------+
student_id is the primary key for this table.
student_name is the name of the student.

Table: Exam

1
2
3
4
5
6
7
8
9
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| exam_id       | int     |
| student_id    | int     |
| score         | int     |
+---------------+---------+
(exam_id, student_id) is the primary key for this table.
Student with student_id got score points in exam with id exam_id.

A “quite” student is the one who took at least one exam and didn’t score neither the high score nor the low score.

Write an SQL query to report the students (student_id, student_name) being “quiet” in ALL exams.

Don’t return the student who has never taken any exam. Return the result table ordered by student_id.

The query result format is in the following example.

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
Student table:
+-------------+---------------+
| student_id  | student_name  |
+-------------+---------------+
| 1           | Daniel        |
| 2           | Jade          |
| 3           | Stella        |
| 4           | Jonathan      |
| 5           | Will          |
+-------------+---------------+

Exam table:
+------------+--------------+-----------+
| exam_id    | student_id   | score     |
+------------+--------------+-----------+
| 10         |     1        |    70     |
| 10         |     2        |    80     |
| 10         |     3        |    90     |
| 20         |     1        |    80     |
| 30         |     1        |    70     |
| 30         |     3        |    80     |
| 30         |     4        |    90     |
| 40         |     1        |    60     |
| 40         |     2        |    70     |
| 40         |     4        |    80     |
+------------+--------------+-----------+

Result table:
+-------------+---------------+
| student_id  | student_name  |
+-------------+---------------+
| 2           | Jade          |
+-------------+---------------+

For exam 1: Student 1 and 3 hold the lowest and high score respectively.
For exam 2: Student 1 hold both highest and lowest score.
For exam 3 and 4: Studnet 1 and 4 hold the lowest and high score respectively.
Student 2 and 5 have never got the highest or lowest in any of the exam.
Since student 5 is not taking any exam, he is excluded from the result.
So, we only return the information of Student 2.


MySQL Solution

1
2
3
4
5
6
7
8
9
10
11
select student_id,student_name 
from student
where student_id in (select student_id from exam) and 
      student_id not in 
        (select student_id 
         from 
            (select student_id, 
                rank() over(partition by exam_id order by score asc) as asc_rank,
                rank() over(partition by exam_id order by score desc) as desc_rank 
             from exam) tab1
        where asc_rank=1 or desc_rank=1)




Related Posts

Total Sales Amount By Year Problem

LeetCode 1384. Write an SQL query to report the Total...

Find The Quiet Students In All Exams Problem

LeetCode 1412. Write an SQL query to report the students...

NPV Queries Problem

LeetCode 1421. Write an SQL query to find the npv...

Customers Who Bought Products A And B But Not C Problem

LeetCode 1398. Write an SQL query to report the customer_id...

Capital Gain/Loss Problem

LeetCode 1393. Write an SQL query to report the Capital...

Top Travellers Problem

LeetCode 1407. Write an SQL query to report the distance...