Lesson 21 — Right-Side Priority

RIGHT JOIN

RIGHT JOIN is the mirror of LEFT JOIN. It returns all rows from the right table. In practice, most developers rewrite RIGHT JOINs as LEFT JOINs by swapping table order — it is clearer.

SQL — RIGHT JOIN and equivalent LEFT JOIN
-- RIGHT JOIN — all placements, even if the learner was deleted:
SELECT
    l.full_name,
    p.employer_name,
    p.monthly_salary
FROM   learners    AS l
RIGHT JOIN placements AS p
    ON l.learner_id = p.learner_id;
-- Returns all placements
-- Learners who exist: their name appears
-- Placements with deleted/missing learner: NULL in full_name column

-- Equivalent LEFT JOIN (swap table order — same result, usually clearer):
SELECT
    l.full_name,
    p.employer_name
FROM   placements  AS p
LEFT  JOIN learners AS l
    ON p.learner_id = l.learner_id;
SQL — Finding orphaned records
-- Practical use: find orphaned records in the right table:
SELECT p.*
FROM   learners   AS l
RIGHT JOIN placements AS p
    ON l.learner_id = p.learner_id
WHERE  l.learner_id IS NULL;
-- Placements that reference a learner_id not in the learners table
-- This indicates a data integrity problem (FK was not enforced)

-- RIGHT JOIN is NOT SUPPORTED in SQLite:
-- Rewrite as LEFT JOIN with tables swapped (always works)

Practice Task

Your Turn

Write a RIGHT JOIN query that returns all assessments and their learner's name (NULL if the learner record is missing). Rewrite the same query as a LEFT JOIN. Verify both produce the same result. Use the IS NULL pattern to identify any orphaned assessments.

Common Mistakes

  • RIGHT JOIN is not supported in SQLite — always rewrite as LEFT JOIN.
  • RIGHT JOIN confuses readers — unless there is a specific reason, LEFT JOIN with tables swapped is clearer.
  • Mixing LEFT and RIGHT JOINs in one query — very confusing. Pick one style.
  • NULL in the left table columns after RIGHT JOIN means no match was found in the left table.

Professional Tip

Many SQL style guides recommend never using RIGHT JOIN — just swap your table order and use LEFT JOIN. Consistent direction makes queries easier to read.

Mini Quiz

How is a RIGHT JOIN related to a LEFT JOIN?