SQL RIGHT JOIN
intermediatejoins
2 min read
What is RIGHT JOIN?
RIGHT JOIN (or RIGHT OUTER JOIN) returns all rows from the right table and matching rows from the left table. If there is no match, the left-side columns are NULL.
Syntax
SELECT columns
FROM left_table
RIGHT JOIN right_table ON left_table.col = right_table.col;When to Use
- When the primary table is listed second in your query
- Most developers prefer rewriting as a LEFT JOIN for readability
Key Points
- Mirror of LEFT JOIN — A RIGHT JOIN is equivalent to a LEFT JOIN with the tables swapped.
- All Right Rows — Every row from the right table appears in the result.
- Rarely Used — In practice, LEFT JOIN is far more common.
- NULLs for No Match — Left-side columns are NULL when there is no match.
- Readability — Using LEFT JOIN consistently makes queries easier to read and maintain.
Guided Practice
Solve the challenge below. Use hints when stuck and check your answer for instant feedback.
Practice challengeGuided learning mode
SQL RIGHT JOIN Challenge
Write a query that solve this task: list every course even if no one is enrolled.
Expected result
All courses appear. Courses with no enrollments show NULL for student_id.
Hidden checks
- Returned rows and values
- Output columns and result shape
- Final database state after the query runs
Lesson guidance
What is RIGHT JOIN?
Initializing database...Each run starts from fresh sample data.
More Examples
Equivalent LEFT JOIN
The same query rewritten as a LEFT JOIN.
Initializing database...Each run starts from fresh sample data.
Frequently Asked Questions
Should I use RIGHT JOIN or LEFT JOIN?
LEFT JOIN is more common and generally preferred. A RIGHT JOIN can always be rewritten as a LEFT JOIN by swapping the table order.
Is RIGHT JOIN supported in all databases?
Most major databases including PostgreSQL, MySQL, and SQL Server support RIGHT JOIN. SQLite does not.