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
-- 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.
- Determine Total Number of Courses: Calculate the total count of distinct courses available in the
Coursetable. - Count Student Enrollments: For each student, count the number of distinct courses they have enrolled in from the
Enrollmenttable. - Compare and Select Eligible Students: Select students whose count of enrolled courses matches the total number of courses, indicating full enrollment.
- Order the Results: Present the final list of student IDs in any order as specified.
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:
SELECT COUNT(*) AS total_courses:- Counts all the rows in the
Coursetable, effectively determining the total number of courses available.
- Counts all the rows in the
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:
SELECT student_id, COUNT(DISTINCT course_id) AS enrolled_courses:- Retrieves each
student_idalong with the count of uniquecourse_ids they have enrolled in.
- Retrieves each
FROM Enrollment:- Specifies the
Enrollmenttable as the data source.
- Specifies the
GROUP BY student_id:- Aggregates the data by
student_idto perform the count for each individual student.
- Aggregates the data by
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:
SELECT student_id:- Chooses the
student_idof students who meet the enrollment criteria.
- Chooses the
FROM Enrollment:- Utilizes the
Enrollmenttable as the data source.
- Utilizes the
GROUP BY student_id:- Groups the records by
student_idto perform aggregate calculations for each student.
- Groups the records by
HAVING COUNT(DISTINCT course_id) = (SELECT COUNT(*) FROM Course):-
Filters the grouped students to include only those whose count of distinct enrolled courses equals the total number of courses available.
-
COUNT(DISTINCT course_id): Counts the unique courses each student has enrolled in. -
(SELECT COUNT(*) FROM Course): Subquery that retrieves the total number of courses from theCoursetable.
-
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.
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.
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.
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.
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.