Bird
0
0

Consider these tables:

medium📝 query result Q5 of 15
SQL - LEFT and RIGHT JOIN
Consider these tables:
students(id, name), courses(id, title), enrollments(student_id, course_id)
What does this query return?
SELECT s.name, c.title FROM students s LEFT JOIN enrollments e ON s.id = e.student_id LEFT JOIN courses c ON e.course_id = c.id;
AAll students with their enrolled courses; NULL if not enrolled.
BOnly students enrolled in courses.
CAll courses with enrolled students.
DOnly courses with students enrolled.
Step-by-Step Solution
Solution:
  1. Step 1: Identify main table and joins

    Main table is students. LEFT JOIN enrollments on student_id, then LEFT JOIN courses on course_id.
  2. Step 2: LEFT JOIN preserves all students

    All students appear. If no enrollment, course title is NULL.
  3. Final Answer:

    All students with their enrolled courses; NULL if not enrolled. -> Option A
  4. Quick Check:

    LEFT JOIN keeps all students = All students with their enrolled courses; NULL if not enrolled. [OK]
Quick Trick: LEFT JOIN keeps all left rows, NULLs for missing right rows [OK]
Common Mistakes:
MISTAKES
  • Assuming only enrolled students appear
  • Confusing join order
  • Expecting all courses regardless of enrollment

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes