Knowledge Guide
HomeDatabasesSQL Practice Problems

Find the Quiet Students in All Exams

Problem

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.

Table: Exam

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| exam_id       | int     |
| student_id    | int     |
| score         | int     |
+---------------+---------+
(exam_id, student_id) is the primary key (combination of columns with unique values) for this table.
Each row of this table indicates that the student with student_id had a score points in the exam with id exam_id.

Problem Definition

A quiet student is the one who took at least one exam and did not score the highest or the lowest score.

Write a solution to report the students (student_id, student_name) being quiet in all exams. Do not return the student who has never taken any exam.

Return the result table ordered by student_id.

Example

Image
Image

Output

Image
Image

Try It Yourself

java
-- TODO: Write your user queries here

Solution

To identify quiet students—those who have taken at least one exam and never scored the highest or the lowest in any of their exams—we can follow a structured approach using SQL's window functions and subqueries. This involves ranking students within each exam, filtering out those who achieved top or bottom ranks, and finally selecting the remaining students who meet the quiet student criteria.

Approach Overview

  1. Rank Students' Scores Within Each Exam:
    • Assign ranks to students based on their scores in each exam to identify the highest and lowest scorers.
  2. Identify Students with Highest or Lowest Scores:
    • Extract students who have ever ranked first (highest score) or last (lowest score) in any exam.
  3. Determine Quiet Students:
    • Exclude students identified in the previous step from the list of all students who have taken at least one exam. The remaining students are the quiet students.
  4. Retrieve Quiet Students' Details:
    • Fetch the student_id and student_name of the quiet students from the Student table.
  5. Finalize the Results:
    • Select distinct candidate details and present them in the desired format, ordered by student_id.

SQL Query

WITH cte AS ( SELECT exam_id, student_id, RANK() OVER(PARTITION BY exam_id ORDER BY score DESC) AS high_score, RANK() OVER(PARTITION BY exam_id ORDER BY score) AS low_score FROM Exam ) SELECT DISTINCT e.student_id, s.student_name FROM Exam e LEFT JOIN Student s ON s.student_id = e.student_id WHERE e.student_id NOT IN ( SELECT student_id FROM cte WHERE high_score = 1 OR low_score = 1 ) ORDER BY e.student_id;

Step-by-Step Approach

Step 1: Rank Students' Scores Within Each Exam (cte)

Assign ranks to students within each exam to determine who scored the highest and the lowest.

SQL Query:

WITH cte AS ( SELECT exam_id, student_id, RANK() OVER(PARTITION BY exam_id ORDER BY score DESC) AS high_score, RANK() OVER(PARTITION BY exam_id ORDER BY score) AS low_score FROM Exam ) SELECT * FROM cte;

Explanation:

Intermediate Output After Step 1 (cte):

+---------+------------+-----------+----------+ | exam_id | student_id | high_score| low_score| +---------+------------+-----------+----------+ | 10 | 1 | 3 | 1 | | 10 | 2 | 2 | 2 | | 10 | 3 | 1 | 3 | | 20 | 1 | 1 | 1 | | 30 | 1 | 3 | 1 | | 30 | 3 | 2 | 2 | | 30 | 4 | 1 | 3 | | 40 | 1 | 3 | 1 | | 40 | 2 | 2 | 2 | | 40 | 4 | 1 | 3 | +---------+------------+-----------+----------+

Step 2: Identify Students with Highest or Lowest Scores

Find all students who have ever ranked first (highest score) or last (lowest score) in any exam.

SQL Query:

WITH cte AS ( SELECT exam_id, student_id, RANK() OVER(PARTITION BY exam_id ORDER BY score DESC) AS high_score, RANK() OVER(PARTITION BY exam_id ORDER BY score) AS low_score FROM Exam ) SELECT DISTINCT student_id FROM cte WHERE high_score = 1 OR low_score = 1;

Explanation:

Intermediate Output After Step 2:

+------------+ | student_id | +------------+ | 1 | | 3 | | 4 | +------------+

Step 3: Determine Quiet Students

Exclude students identified in Step 2 from those who have taken at least one exam. The remaining students are the quiet students.

SQL Query:

WITH cte AS ( SELECT exam_id, student_id, RANK() OVER(PARTITION BY exam_id ORDER BY score DESC) AS high_score, RANK() OVER(PARTITION BY exam_id ORDER BY score) AS low_score FROM Exam ), excluded AS ( SELECT DISTINCT student_id FROM cte WHERE high_score = 1 OR low_score = 1 ) SELECT DISTINCT e.student_id, s.student_name FROM Exam e JOIN Student s ON s.student_id = e.student_id WHERE e.student_id NOT IN (SELECT student_id FROM excluded) ORDER BY e.student_id;

Explanation:

Intermediate Output After Step 3:

+------------+--------------+ | student_id | student_name | +------------+--------------+ | 2 | Jade | +------------+--------------+

Explanation of Output:

Note:

🤖 Don't fully get this? Learn it with Claude

Stuck on Find the Quiet Students in All Exams? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Find the Quiet Students in All Exams** (Databases) and want to truly understand it. Explain Find the Quiet Students in All Exams from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Find the Quiet Students in All Exams** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Find the Quiet Students in All Exams** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Find the Quiet Students in All Exams** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes