Unique Course EnrollmentsEasy

Unique Course Enrollments

EasySQLPractice library · +10 XPdistinct valuessqlenrollment

An educational platform wants to identify all the unique courses offered that have at least one student enrolled. This will help them understand course popularity.

Tables:

  • students: student_id (integer, PRIMARY KEY), name (varchar(50))
  • enrollments: enrollment_id (integer, PRIMARY KEY), student_id (integer), course_id (varchar(10))

Return the course_id of all distinct courses that appear in the enrollments table. The results should be ordered by course_id in ascending order.

Handle ties by ordering alphabetically. NULL course_id values should not be included in the result. An empty result set is possible if no enrollments exist.

Tables

CREATE TABLE students (
  student_id integer PRIMARY KEY,
  name varchar(50)
);
CREATE TABLE enrollments (
  enrollment_id integer PRIMARY KEY,
  student_id integer REFERENCES students(student_id),
  course_id varchar(10) 
);

Example data

INSERT INTO students (student_id, name) VALUES
(1, 'Alice'),
(2, 'Bob'),
(3, 'Charlie'),
(4, 'David');
INSERT INTO enrollments (enrollment_id, student_id, course_id) VALUES
(101, 1, 'CS101'),
(102, 2, 'CS101'),
(103, 1, 'MA202'),
(104, 3, 'PH101'),
(105, 4, 'CS101'),
(106, 3, 'MA202'),
(107, 2, 'CS303');

Your query is also checked on hidden datasets with different data.

Hints