Right Join

SQL · Joining Multiple Tables

Theory

RIGHT JOIN (RIGHT OUTER JOIN) is the mirror image of LEFT JOIN: it keeps every row from the right table, the one written second, and fills the left table's columns with NULL where there is no match. A RIGHT JOIN B gives the same rows as B LEFT JOIN A; only the column order in SELECT * differs.

In the example below every department appears, including Sales with no employees, while employees without a valid department are dropped. Most teams rewrite right joins as left joins by swapping the table order, because reading queries is easier when the main table always comes first. RIGHT JOIN is supported by PostgreSQL, MySQL, SQL Server, Oracle and recent SQLite versions.

Why we use it

Sometimes the table that must be kept completely appears second in an existing query. RIGHT JOIN keeps all rows of that table without reordering the whole FROM clause.

When to use it

Use RIGHT JOIN when you need every row of the second table, for example to list departments with no staff. In most code, prefer swapping the tables and using LEFT JOIN for consistency.

How it works

  1. Take a row from the right table (departments)
  2. Look for left rows that satisfy the ON condition
  3. Any match? Yes: One combined row per match. No: Right row once, NULLs on the left.
  4. Repeat for every right row
  5. Return all right rows

Syntax

SELECT columns
FROM t1
RIGHT JOIN t2 ON t1.id = t2.t1_id;

Example

SELECT e.name, d.department_name
FROM employees e
RIGHT JOIN departments d ON e.dept_id = d.id;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic