âĄī¸ The RIGHT JOIN (also called RIGHT OUTER JOIN) returns all rows from the right table and the matching rows from the left table. If there is no matching row in the left table, SQL returns NULL for the left-table columns.
đ What is a RIGHT JOIN?
A RIGHT JOIN keeps every record from the right table, regardless of whether a matching record exists in the left table. It is useful when the right table contains the primary data you want to preserve while retrieving related information from the left table whenever available.
Information
đ¯ Why Use RIGHT JOIN?
Use a RIGHT JOIN when every record from the right table is important, even if related data from the left table is missing.
- đ Include all records from the right table.
- đ Identify records that have no matching data in the left table.
- đ Generate complete reports from the right table.
- đ Work with optional relationships between tables.
đ Sample Tables
Consider the following Students table:
| StudentID | Name | Department |
|---|---|---|
| 101 | Alice | Computer Science |
| 102 | Bob | Mathematics |
| 103 | Charlie | Physics |
| 104 | David | Computer Science |
Consider the following Courses table:
| CourseID | StudentID | CourseName |
|---|---|---|
| 1 | 101 | Database Systems |
| 2 | 101 | Operating Systems |
| 3 | 103 | Physics Lab |
| 4 | 104 | Computer Networks |
| 5 | 105 | Artificial Intelligence |
đ Basic Syntax
RIGHT JOIN Syntax
SELECT table1.column_name,
table2.column_name
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;đĄ Basic RIGHT JOIN Example
Retrieve every course along with the student enrolled in it, if one exists.
Basic RIGHT JOIN
SELECT Students.Name,
Courses.CourseName
FROM Students
RIGHT JOIN Courses
ON Students.StudentID = Courses.StudentID;Result:
| Name | CourseName |
|---|---|
| Alice | Database Systems |
| Alice | Operating Systems |
| Charlie | Physics Lab |
| David | Computer Networks |
| NULL | Artificial Intelligence |
Important
đˇī¸ Using Table Aliases
Aliases make join queries shorter and easier to read.
RIGHT JOIN with Aliases
SELECT s.Name,
c.CourseName
FROM Students AS s
RIGHT JOIN Courses AS c
ON s.StudentID = c.StudentID;đ RIGHT JOIN with WHERE
Filter the joined results using the WHERE clause.
Filter RIGHT JOIN Results
SELECT s.Name,
c.CourseName
FROM Students s
RIGHT JOIN Courses c
ON s.StudentID = c.StudentID
WHERE c.CourseName LIKE '%Systems%';đĢ Finding Unmatched Rows
A common use of RIGHT JOIN is finding records in the right table that have no matching rows in the left table.
Courses Without Students
SELECT c.CourseName
FROM Students s
RIGHT JOIN Courses c
ON s.StudentID = c.StudentID
WHERE s.StudentID IS NULL;This query returns courses that are not assigned to any student.
đ RIGHT JOIN with ORDER BY
Sort the joined results using the ORDER BY clause.
Sort RIGHT JOIN Results
SELECT s.Name,
c.CourseName
FROM Students s
RIGHT JOIN Courses c
ON s.StudentID = c.StudentID
ORDER BY c.CourseName ASC;đ RIGHT JOIN Multiple Tables
You can join multiple related tables in a single query.
RIGHT JOIN Three Tables
SELECT s.Name,
c.CourseName,
d.DepartmentName
FROM Students s
RIGHT JOIN Courses c
ON s.StudentID = c.StudentID
RIGHT JOIN Departments d
ON c.DepartmentID = d.DepartmentID;đ How RIGHT JOIN Works
| Students Table | Courses Table | Included? |
|---|---|---|
| Matching StudentID | Matching StudentID | â Yes |
| No Matching Student | Course Exists | â Yes (Student columns are NULL) |
| Student Exists | No Matching Course | â No |
âī¸ LEFT JOIN vs RIGHT JOIN
| Feature | LEFT JOIN | RIGHT JOIN |
|---|---|---|
| Returns All Rows From | Left table | Right table |
| Unmatched Left Rows | â Included | â Excluded |
| Unmatched Right Rows | â Excluded | â Included |
â ī¸ Database Compatibility
Important
â ī¸ Common Mistakes
- â Confusing the left and right tables.
- â Using the wrong join condition.
- â Forgetting the ON clause.
- â Using = NULL instead of IS NULL.
Warning
đŧ Real-World Example
A university administrator wants a report listing every course, including courses that have not yet been assigned to a student.
Course Assignment Report
SELECT c.CourseID,
c.CourseName,
s.Name AS StudentName
FROM Students s
RIGHT JOIN Courses c
ON s.StudentID = c.StudentID
ORDER BY c.CourseName;This query ensures that every course appears in the report, even if no student is currently enrolled.
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ RIGHT JOIN returns all rows from the right table.
- đ Matching rows from the left table are included.
- đ Unmatched left-table columns contain NULL.
- đ It is useful for finding records in the right table without matches.
- đ Many RIGHT JOIN queries can be rewritten as LEFT JOIN queries.
- đ Use IS NULL to identify unmatched records.