SQL Tutorial 0/85 lessons ~6 min read Lesson 38

    RIGHT JOIN

    RIGHT JOIN is the mirror of LEFT JOIN — it keeps all rows from the right table.

    Course progress0%
    Focus
    6 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    RIGHT JOIN is the mirror of LEFT JOIN — it keeps all rows from the right table. In practice, almost everyone writes LEFT JOIN with the tables flipped, but you'll see RIGHT JOIN occasionally.

    Understanding the topic

    Core concepts to understand:

    • FROM a RIGHT JOIN b ON ....
    • All B rows kept; A columns NULL when no match.
    • Stylistic preference: most teams use only LEFT JOIN.
    • Useful when the natural reading order keeps the 'main' table on the right.
    • Same performance as LEFT JOIN.

    Syntax reference

    Visual workflow / architecture:

    bash
    RIGHT JOIN orders ──▶ users
    every order kept; user side may be NULL if data is bad
    Syntax Pattern
    SELECT <cols>
    FROM   <a> [AS a]
    RIGHT JOIN <b> [AS b]
           ON  <a.key> = <b.key>;
    • RIGHT JOINKeeps every row from B; A columns are NULL when no match.
    • tipEquivalent to LEFT JOIN with tables flipped — usually preferred.
    Live Example
    sql
    SELECT u.name, o.amount
    FROM users u
    RIGHT JOIN orders o ON o.user_id = u.id;
    users + orders
    u.idu.nameo.ido.amount
    1Ana10150
    2Leo10230
    10370
    joined
    nameamount
    Ana50
    Leo30
    NULL70
    ABB (with matching A)

    Order 103 has no matching user — its name column is NULL.

    Real-world use

    Audit queries like 'every order, even if its user record is missing/deleted' sometimes read more naturally as RIGHT JOIN.

    Best practices

    • Stick to LEFT JOIN for consistency unless RIGHT reads more naturally.
    • Same indexing rules as LEFT JOIN.
    • Code reviews often suggest flipping RIGHT to LEFT.

    Hands-on exercise

    Interview preparation — practice these questions:

    • Q1. RIGHT vs LEFT JOIN.
    • Q2. Why is RIGHT JOIN uncommon?
    • Q3. Convert a RIGHT JOIN to a LEFT JOIN.
    • Q4. Performance differences.
    • Q5. Use case where RIGHT is clearer.
    Ready to mark this lesson complete?Track your journey across the entire course.