Skip to content
Laviyx Learn

Try

SQL JOIN Practice

Combine tables with INNER JOIN and LEFT JOIN, find missing matches, and summarise joined data.

Intermediate3 problemsUpdated

Review first: INNER JOIN, LEFT JOIN

students

student_idname
1Asha
2Ravi
3Meera
4Kiran

enrollments

student_idcourse
1SQL
1Python
2SQL
3Python
Try your answers on these tables in the SQL Playground
  1. 1.Students and their courses

    Intermediate

    Return each student's name and course for students who are enrolled in at least one course.

    Show hint

    Only matching rows are needed, so an INNER JOIN on student_id is enough.

    Show solution
    SQLRun
    SELECT s.name, e.course
    FROM students s
    INNER JOIN enrollments e ON s.student_id = e.student_id
    ORDER BY s.name, e.course;
    Output
    name  | course
    ------+-------
    Asha  | Python
    Asha  | SQL
    Meera | Python
    Ravi  | SQL
  2. 2.Not enrolled yet

    Intermediate

    Return the names of students who are not enrolled in any course.

    Show hint

    LEFT JOIN from students, then keep rows where the enrollments side is NULL.

    Show solution
    SQLRun
    SELECT s.name
    FROM students s
    LEFT JOIN enrollments e ON s.student_id = e.student_id
    WHERE e.student_id IS NULL;
    Output
    name
    -----
    Kiran
  3. 3.Course count for everyone

    Intermediate

    Return every student's name with the number of courses they take as courses. Students with no courses must show 0.

    Show hint

    Use a LEFT JOIN and count a column from enrollments, not COUNT(*).

    Show solution
    SQLRun
    SELECT s.name, COUNT(e.course) AS courses
    FROM students s
    LEFT JOIN enrollments e ON s.student_id = e.student_id
    GROUP BY s.student_id, s.name
    ORDER BY courses DESC, s.name;
    Output
    name  | courses
    ------+--------
    Asha  | 2
    Meera | 1
    Ravi  | 1
    Kiran | 0

    COUNT(*) would return 1 for Kiran because the LEFT JOIN still produces one row for Kiran, with NULLs on the right side. COUNT(e.course) skips NULLs and correctly returns 0.