Course of Each Learner's First Enrollment
Problem
An online learning platform logs each course a learner enrolled in, and when.
Table: `enrollments`
| Column | Type |
|---------------|------|
| learner_id | int |
| course_id | int |
| enroll_date | date |
| hours_studied | int |
`(learner_id, enroll_date)` is the primary key.
Write a query that reports, for each learner, the `course_id` they enrolled in on their **earliest** `enroll_date`. Return columns `learner_id` and `course_id`, in any order.
Tables
Example rows — the live problem includes the full dataset.
| learner_id | course_id | enroll_date | hours_studied |
|---|---|---|---|
| 1 | 20 | 2023-01-05 | 5 |
| 1 | 21 | 2023-03-02 | 6 |
| 2 | 30 | 2023-02-18 | 1 |
Expected output
Your answer should return 3 rows with the columns learner_id, course_id.
Starter code (SQL)
SELECT *
FROM enrollments;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock