Lesson 49 of 60 – RIGHT JOIN
82%

SQL RIGHT JOIN

A RIGHT JOIN returns all records from the right table and the matching records from the left table. If there is no matching record in the left table, the columns from the left table contain NULL.

Note: RIGHT JOIN is useful when you want to keep every record from the second table, even when some records do not have a matching record in the first table.

1. What is RIGHT JOIN?

A RIGHT JOIN combines records from two tables while keeping all records from the right table.

SELECT *
FROM students
RIGHT JOIN courses
ON students.course_id = courses.id;

All courses are returned. If a course does not have a matching student, the student columns contain NULL.

2. Basic RIGHT JOIN Syntax

The basic syntax is:

SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;

The second table is called the right table.

3. RIGHT JOIN with Two Tables

Suppose we have students and courses tables.

SELECT
    students.student_name,
    courses.course_name
FROM students
RIGHT JOIN courses
ON students.course_id = courses.id;

Every course is displayed, even if no student is enrolled in it.

4. Understanding the ON Clause

The ON clause defines how the two tables are related.

SELECT
    s.student_name,
    c.course_name
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id;

Here, students.course_id is matched with courses.id.

5. RIGHT JOIN with Specific Columns

It is better to select only the required columns when working with larger tables.

SELECT
    students.student_name,
    students.mobile,
    courses.course_name
FROM students
RIGHT JOIN courses
ON students.course_id = courses.id;

6. RIGHT JOIN with Table Aliases

Table aliases make RIGHT JOIN queries shorter and easier to read.

SELECT
    s.student_name,
    c.course_name
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id;

Here, s represents the students table and c represents the courses table.

7. RIGHT JOIN and NULL Values

If there is no matching record in the left table, the left table columns contain NULL.

SELECT
    s.student_name,
    c.course_name
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id;

A course without a matching student will have NULL in student_name.

8. Finding Courses Without Students

RIGHT JOIN can be used to find courses that do not have a matching student.

SELECT
    c.course_name
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id
WHERE s.id IS NULL;

This returns courses that currently have no matching student.

9. RIGHT JOIN with WHERE

A WHERE condition can be applied to a RIGHT JOIN result.

SELECT
    s.student_name,
    c.course_name
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id
WHERE c.status = 'Active';

This displays active courses and any matching student information.

10. RIGHT JOIN with ORDER BY

The result can be sorted using ORDER BY.

SELECT
    s.student_name,
    c.course_name
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id
ORDER BY c.course_name;

11. RIGHT JOIN with Multiple Conditions

Multiple conditions can be placed in the ON clause.

SELECT
    s.student_name,
    c.course_name
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id
AND s.status = 'Active';

Every course is kept, while only active students are matched.

12. RIGHT JOIN with Three Tables

RIGHT JOIN can be combined with other joins to retrieve related information.

SELECT
    s.student_name,
    c.course_name,
    p.amount
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id
LEFT JOIN payments AS p
ON s.id = p.student_id;

All courses remain in the result.

13. RIGHT JOIN with GROUP BY

GROUP BY can be used with RIGHT JOIN to create course reports.

SELECT
    c.course_name,
    COUNT(s.id) AS total_students
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id
GROUP BY c.id, c.course_name;

Courses without students will have a count of zero.

14. RIGHT JOIN with HAVING

HAVING can filter grouped RIGHT JOIN results.

SELECT
    c.course_name,
    COUNT(s.id) AS total_students
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id
GROUP BY c.id, c.course_name
HAVING total_students = 0;

This finds courses without students.

15. RIGHT JOIN with Payments

RIGHT JOIN can keep every course while displaying payment information where available.

SELECT
    c.course_name,
    s.student_name,
    p.amount
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id
LEFT JOIN payments AS p
ON s.id = p.student_id;

16. RIGHT JOIN to Find Unused Courses

RIGHT JOIN can help identify courses that currently have no students.

SELECT
    c.id,
    c.course_name
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id
WHERE s.id IS NULL;

These courses exist in the courses table but have no matching student.

17. RIGHT JOIN with CASE

CASE can be used to display a meaningful status.

SELECT
    c.course_name,
    CASE
        WHEN s.id IS NULL THEN 'No Student'
        ELSE 'Student Assigned'
    END AS course_status
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id;

18. RIGHT JOIN with COALESCE()

COALESCE() can replace NULL values with a readable value.

SELECT
    COALESCE(s.student_name, 'No Student') AS student,
    c.course_name
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id;

If a course has no student, No Student is displayed.

19. RIGHT JOIN with Date Conditions

Date conditions can be applied to joined data.

SELECT
    s.student_name,
    c.course_name,
    s.admission_date
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id
WHERE s.admission_date >= '2026-01-01'
   OR s.admission_date IS NULL;

The NULL condition keeps courses without matching students.

20. RIGHT JOIN with IN

The IN operator can be used to filter courses.

SELECT
    s.student_name,
    c.course_name
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id
WHERE c.course_name IN ('Python', 'Java', 'PHP');

The query returns the selected courses and any matching students.

21. RIGHT JOIN with Calculations

Calculations can be performed using columns from the joined tables.

SELECT
    c.course_name,
    c.fee,
    COALESCE(s.paid_fee, 0) AS paid_fee,
    c.fee - COALESCE(s.paid_fee, 0) AS pending_fee
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id;

22. RIGHT JOIN and Foreign Keys

RIGHT JOIN is commonly used with tables connected through primary and foreign keys.

SELECT
    s.student_name,
    c.course_name
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id;

The student's foreign key is matched with the course primary key.

23. RIGHT JOIN with Different Column Names

The related columns do not need to have the same name.

SELECT
    s.student_name,
    c.course_name
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_code = c.code;

The important point is that the values represent the same relationship.

24. RIGHT JOIN vs LEFT JOIN

RIGHT JOIN: Keeps all records from the right table.

LEFT JOIN: Keeps all records from the left table.

-- RIGHT JOIN
SELECT *
FROM students s
RIGHT JOIN courses c
ON s.course_id = c.id;

In this example, every course is kept.

25. RIGHT JOIN vs INNER JOIN

RIGHT JOIN: Keeps every record from the right table.

INNER JOIN: Keeps only records with matches in both tables.

-- RIGHT JOIN
SELECT *
FROM students s
RIGHT JOIN courses c
ON s.course_id = c.id;

26. RIGHT JOIN and Missing Matches

If a course has no matching student, the course is still returned.

SELECT
    s.student_name,
    c.course_name
FROM students s
RIGHT JOIN courses c
ON s.course_id = c.id;

The student_name value will be NULL for a course without a matching student.

27. Common RIGHT JOIN Mistake

A common mistake is forgetting which table is the right table.

SELECT
    s.student_name,
    c.course_name
FROM students s
RIGHT JOIN courses c
ON s.course_id = c.id;

Here, courses is the right table, so all courses are preserved.

28. RIGHT JOIN Can Be Rewritten as LEFT JOIN

A RIGHT JOIN can usually be rewritten as a LEFT JOIN by reversing the table order.

-- RIGHT JOIN
SELECT
    s.student_name,
    c.course_name
FROM students s
RIGHT JOIN courses c
ON s.course_id = c.id;

The equivalent LEFT JOIN is:

SELECT
    s.student_name,
    c.course_name
FROM courses c
LEFT JOIN students s
ON s.course_id = c.id;

29. Practical Course Report

A course report can display every course and the number of students enrolled.

SELECT
    c.id AS course_id,
    c.course_name,
    COUNT(s.id) AS total_students
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id
GROUP BY c.id, c.course_name
ORDER BY c.course_name;

Courses with no students are also included.

30. Complete RIGHT JOIN Example

The following example creates a complete course report using students, courses, and payments.

SELECT
    c.id AS course_id,
    c.course_name AS course,
    COALESCE(s.student_name, 'No Student') AS student,
    COALESCE(s.mobile, 'Not Available') AS mobile,
    COALESCE(p.amount, 0) AS paid_amount,
    CASE
        WHEN s.id IS NULL THEN 'No Student'
        WHEN p.id IS NULL THEN 'Payment Pending'
        ELSE 'Payment Available'
    END AS status
FROM students AS s
RIGHT JOIN courses AS c
ON s.course_id = c.id
LEFT JOIN payments AS p
ON s.id = p.student_id
ORDER BY c.course_name;

This query keeps every course and displays matching student and payment information when available.

📌 Key Points

  • RIGHT JOIN returns all records from the right table.
  • Matching records from the left table are included.
  • If there is no match, left table columns contain NULL.
  • The ON clause defines the relationship between tables.
  • RIGHT JOIN can combine multiple tables.
  • Table aliases make RIGHT JOIN queries easier to read.
  • RIGHT JOIN can be used to find courses without students.
  • IS NULL can be used to find missing matches.
  • RIGHT JOIN can be combined with GROUP BY and HAVING.
  • RIGHT JOIN can be used with aggregate functions such as COUNT().
  • COALESCE() can replace NULL values.
  • RIGHT JOIN is useful when every record from the right table must be preserved.
  • A RIGHT JOIN can usually be rewritten as a LEFT JOIN by reversing table order.
  • RIGHT JOIN is different from INNER JOIN because unmatched right-table records are retained.

🧠 Quick Quiz

Question: What does a RIGHT JOIN return?