Knowledge Guide
HomeDatabasesSQL Practice Problems

medium Students Who Attended All Courses

Problem Statement

Table: Enrollment

This table records the enrollments of students in various courses. Each record includes a student ID and a course ID.

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| student_id   | int     |
| course_id    | int     |
+--------------+---------+
This table may contain duplicate rows.
student_id is not NULL.
course_id is a foreign key to the Course table.

Table: Course

This table lists all the courses available.

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| course_id    | int     |
+--------------+---------+
course_id is the primary key for this table.

Develop a solution to identify the student IDs from the Enrollment table who have enrolled in all the courses listed in the Course table.

Return the result table in any order.

Example

Input:

Enrollment table: +------------+-----------+ | student_id | course_id | +------------+-----------+ | 100 | 200 | | 200 | 300 | | 300 | 200 | | 300 | 300 | | 100 | 300 | +------------+-----------+
Course table: +-----------+ | course_id | +-----------+ | 200 | | 300 | +-----------+

Output:

+------------+ | student_id | +------------+ | 100 | | 300 | +------------+

Try It Yourself

java
-- TODO: Write your user queries here

Solution

To identify students who have enrolled in all available courses, we need to compare the number of distinct courses each student has enrolled in with the total number of courses offered. This approach ensures that only those students who have enrolled in every course are selected.

SQL Query

SELECT student_id FROM Enrollment GROUP BY student_id HAVING COUNT(DISTINCT course_id) = (SELECT COUNT(*) FROM Course) ORDER BY student_id

Step-by-Step Approach

Step 1: Determine Total Number of Courses

Calculate the total number of distinct courses available in the Course table to establish the benchmark for full enrollment.

SQL Query:

SELECT COUNT(*) AS total_courses FROM Course;

Explanation:

Output After Step 1:

+--------------+ | total_courses| +--------------+ | 2 | +--------------+

Step 2: Count Student Enrollments

For each student, count the number of distinct courses they have enrolled in to assess whether they meet the full enrollment criteria.

SQL Query:

SELECT student_id, COUNT(DISTINCT course_id) AS enrolled_courses FROM Enrollment GROUP BY student_id;

Explanation:

Output After Step 2:

+------------+-----------------+ | student_id | enrolled_courses| +------------+-----------------+ | 100 | 2 | | 200 | 1 | | 300 | 2 | +------------+-----------------+

Step 3: Compare and Select Eligible Students

Identify students whose number of enrolled courses matches the total number of available courses, indicating that they have enrolled in all courses.

SQL Query:

SELECT student_id FROM Enrollment GROUP BY student_id HAVING COUNT(DISTINCT course_id) = (SELECT COUNT(*) FROM Course);

Explanation:

Final Output:

+------------+ | student_id | +------------+ | 100 | | 300 | +------------+
🤖 Don't fully get this? Learn it with Claude

Stuck on Students Who Attended All Courses? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🪜 Hint ladder (no spoilers)

Progressively stronger hints — you still solve it.

I'm working on the problem **Students Who Attended All Courses** (Databases). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🎨 Explain the approach visually

See the technique, not just code.

Explain the optimal approach to **Students Who Attended All Courses** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔍 Review my solution

Catch bugs, edge cases, sub-optimality.

I'll paste my solution to **Students Who Attended All Courses**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔁 Drill the pattern

Lock in recognition with look-alikes.

Give me 2 problems that use the SAME underlying pattern as **Students Who Attended All Courses**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes