🔍
👶 Kids📝 Blog About Contact 🚀 Get Started Free

SQL – RIGHT JOIN

Preserve all rows from the right table using RIGHT JOIN, with NULLs for non-matching left rows.

RIGHT JOIN is the mirror of LEFT JOIN — it preserves every row from the right table whether or not a match exists in the left. Non-matching left columns are filled with NULL. In practice, most developers rewrite RIGHT JOINs as LEFT JOINs by swapping the table order, since LEFT JOIN is more intuitive and universally understood.

RIGHT JOIN is useful when you want to keep the “lookup” or “dimension” table complete — e.g., keeping all courses visible even if no student has enrolled in them yet.

Equivalent queries

-- RIGHT JOIN
SELECT * FROM enrollments e
RIGHT JOIN courses c ON c.id = e.course_id;

-- Equivalent LEFT JOIN (just swap table order)
SELECT * FROM courses c
LEFT JOIN enrollments e ON c.id = e.course_id;

Try It Yourself — Interactive SQL Editor

Edit the query below and click Run Query ▶ to see live results powered by SQLite running directly in your browser.

SQLite – edit & run
Results
← Click Run Query ▶ to see results

Key Points

  • RIGHT JOIN keeps all rows from the right table
  • Equivalent to LEFT JOIN with swapped table order
  • SQLite supports RIGHT JOIN from version 3.39+
  • Most style guides prefer LEFT JOIN for consistency

Pro Tip from CodesCompiler: The best way to learn SQL is to break things intentionally — modify the query above, change the WHERE conditions, try different columns. Every error teaches you something the docs cannot.

In the next lesson, we continue exploring SQL’s powerful feature set to build your database mastery.