Find the Quiet Students in All Exams — LeetCode 1412 Python Solution
HardLeetCode PremiumDatabase
- Problem
- #1412
- Reading time
- 5 min
- Source
- leetcode.com
Table schema
SQL
Table: Student +---------------------+---------+ | Column Name | Type | +---------------------+---------+ | student_id | int | | student_name | varchar | +---------------------+---------+ student_id is the primary key (column with unique values) for this table. student_name is the name of the student.Example
SQL
+---------------------+---------+
| Column Name | Type |
+---------------------+---------+
| student_id | int |
| student_name | varchar |
+---------------------+---------+
student_id is the primary key (column with unique values) for this table.
student_name is the name of the student.Python solution
Python
import duckdb
import pandas as pd
def solution(student: pd.DataFrame, exam: pd.DataFrame) -> pd.DataFrame:
con = duckdb.connect()
con.register("Student", student)
con.register("Exam", exam)
return con.execute("""WITH
T AS (
SELECT
student_id,
RANK() OVER (
PARTITION BY exam_id
ORDER BY score
) AS rk1,
RANK() OVER (
PARTITION BY exam_id
ORDER BY score DESC
) AS rk2
FROM Exam
)
SELECT student_id, student_name
FROM
T
JOIN Student USING (student_id)
GROUP BY 1
HAVING SUM(rk1 = 1) = 0 AND SUM(rk2 = 1) = 0
ORDER BY 1;""").df()Complexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) (typical) |
| Space | O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 1412. Find the Quiet Students in All Exams?
- LeetCode 1412. Find the Quiet Students in All Exams is rated Hard on LeetCode.
- What topics does LeetCode 1412. Find the Quiet Students in All Exams cover?
- LeetCode 1412. Find the Quiet Students in All Exams is tagged Database on LeetCode.
- Is LeetCode 1412. Find the Quiet Students in All Exams a premium problem?
- Yes. LeetCode 1412. Find the Quiet Students in All Exams is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.