Duplicate Enrollments
Mediumsql
A student should not be able to enroll in the same course in the same semester twice — (student_id, course_id, semester) is meant to be a composite candidate key on enrollments, but it isn't enforced yet. Write a query returning student_id, course_id, semester, and a cnt column, for every combination that appears more than once.
sqlCREATE TABLE enrollments ( student_id INTEGER NOT NULL, course_id INTEGER NOT NULL, semester TEXT NOT NULL );
Sample data (this is what your query runs against when you press Run):
sqlINSERT INTO enrollments (student_id, course_id, semester) VALUES (1, 101, 'Fall2025'), (1, 102, 'Fall2025'), (2, 101, 'Fall2025'), (1, 101, 'Fall2025'), (2, 102, 'Spring2026'), (3, 101, 'Fall2025');
Example 1
Input
(none)
Output
student_id course_id semester cnt 1 101 Fall2025 2
Group by all three columns that together form the intended composite key, then keep groups with more than one row. Reference: SELECT student_id, course_id, semester, COUNT(*) AS cnt FROM enrollments GROUP BY student_id, course_id, semester HAVING COUNT(*) > 1;