LinkedIn: Learning Course Completions per Category in Q1 2025
The LinkedIn Learning content team reviews which course categories learners actually finished in the first quarter.
- Difficulty: Easy
- Topics: Joins, Aggregation, Dates
- Dialect: MySQL
- Problem: #729
Problem statement
The LinkedIn Learning content team reviews which course categories learners actually finished in the first quarter.
For every category that appears in Course, return category and completions, the number of enrollments in that category's courses with completed_on between 2025-01-01 and 2025-03-31 inclusive. A category with no completion in that window shows 0. Order the rows by completions descending, then category ascending.
A practice scenario set at LinkedIn: the tables and data are invented, and this is not a question from, or endorsed by, LinkedIn.
Tables
Table: Course
| Column | Type |
|---|---|
| course_id | int |
| title | varchar |
| category | varchar |
Primary key: course_id.
One row per course; several courses share a category.
Table: CourseEnrollment
| Column | Type |
|---|---|
| enrollment_id | int |
| member_id | int |
| course_id | int |
| enrolled_on | date |
| completed_on | date |
Primary key: enrollment_id.
completed_on is NULL until the learner finishes the course.
Examples
Example 1
Course
| course_id | title | category |
|---|---|---|
| 1 | SQL Essential Training | Data Science |
| 2 | Python for Data Analysis | Data Science |
| 3 | Leading Without Authority | Leadership |
| 4 | Agile Foundations | Project Management |
| 5 | Content Marketing Basics | Marketing |
CourseEnrollment
| enrollment_id | member_id | course_id | enrolled_on | completed_on |
|---|---|---|---|---|
| 1 | 21 | 1 | 2024-12-20 | 2025-01-01 |
| 2 | 22 | 2 | 2025-02-01 | 2025-03-31 |
| 3 | 23 | 1 | 2025-03-10 | 2025-04-01 |
| 4 | 24 | 3 | 2025-01-05 | 2025-02-14 |
| 5 | 25 | 3 | 2025-01-20 | NULL |
| 6 | 26 | 4 | 2024-11-02 | 2024-12-31 |
| 7 | 27 | 5 | 2025-02-11 | 2025-03-02 |
Output
| category | completions |
|---|---|
| Data Science | 2 |
| Leadership | 1 |
| Marketing | 1 |
| Project Management | 0 |
How to solve LinkedIn: Learning Course Completions per Category in Q1 2025
Every category must be listed, even the ones with no completions, so the query starts from Course and LEFT JOINs the enrollments. The quarter condition goes into the ON clause: it decides which enrollments attach to a course, while a course with none still keeps one row with NULLs. COUNT(e.enrollment_id) then counts only the attached enrollments, giving 0 for such a category. Putting the date test in WHERE instead would discard the NULL rows and lose exactly the categories the question insists on.
NULL completed_on values fail BETWEEN, so unfinished enrollments never count, and both 2025-01-01 and 2025-03-31 are inside the window. The first alternative keeps every enrollment in the join and counts conditionally with SUM(CASE …); the second computes each category's count in a correlated subquery over the distinct categories, testing the quarter with QUARTER() and YEAR(). Grouping by category merges all of its courses.
Reference solution (MySQL)
SELECT c.category, COUNT(e.enrollment_id) AS completions
FROM Course c
LEFT JOIN CourseEnrollment e
ON e.course_id = c.course_id
AND e.completed_on BETWEEN '2025-01-01' AND '2025-03-31'
GROUP BY c.category
ORDER BY completions DESC, c.categoryAnother way
SELECT c.category,
SUM(CASE WHEN e.completed_on >= '2025-01-01' AND e.completed_on < '2025-04-01' THEN 1 ELSE 0 END) AS completions
FROM Course c LEFT JOIN CourseEnrollment e ON e.course_id = c.course_id
GROUP BY c.category
ORDER BY completions DESC, c.categoryAnother way
SELECT cat.category,
(SELECT COUNT(*) FROM CourseEnrollment e JOIN Course c ON c.course_id = e.course_id
WHERE c.category = cat.category AND QUARTER(e.completed_on) = 1 AND YEAR(e.completed_on) = 2025) AS completions
FROM (SELECT DISTINCT category FROM Course) cat
ORDER BY completions DESC, cat.category← LinkedIn: InMail Response Rate per Recruiter · LinkedIn: Easy Apply Share of Applications per Job →