LEFT & RIGHT JOINS

SQL joins allow us to combine data from multiple tables based on related columns.

LEFT JOIN and RIGHT JOIN are useful when we want to keep all records from one table, even if there’s no match in the other.

How LEFT JOIN Works

  • Returns all rows from the left table (first table in the query).
  • If there’s no match in the right table, the result contains NULL values.

Students without exams will show NULL in the Score column.

Example Listing all students, even if they haven't taken an exam.

SELECT Students.Name, Exams.Score 
FROM Students 
LEFT JOIN Exams ON Students.StudentID = Exams.StudentID;

How RIGHT JOIN Works

  • Returns all rows from the right table (second table in the query).
  • If there’s no match in the left table, the result contains NULL values.

Example Listing all courses, even if no students are enrolled.

SELECT Courses.CourseName, Students.Name 
FROM Students 
RIGHT JOIN Courses ON Students.StudentID = Courses.CourseID;

Courses with no students will show NULL in the Name column.

Question number:

Up Next: Practice OUTER JOIN

Donate

About

Privacy Policy

Changelog