SQL Keywords

SQL RIGHT

What is SQL RIGHT JOIN?

Returns all rows from the right table in a join and the matching rows from the left table, padding non-matches on the left with NULLs.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL RIGHT:

SQL RIGHT Full Explanation

RIGHT JOIN (also called RIGHT OUTER JOIN) is a set-operator that combines two tables based on a join condition. The operation keeps every row from the right-hand table (the second table listed in the query). If a row in the right table has no corresponding match in the left table, the query still returns the right-table row and fills the left-table columns with NULL values. Functionally, a RIGHT JOIN is equivalent to swapping table order in a LEFT JOIN, and most optimizers internally rewrite it that way. RIGHT JOIN is defined in the SQL-92 standard and is widely supported. Caveats: some databases disallow RIGHT JOIN in certain views or derived tables, and older SQLite versions (before 3.39) lack support.

SQL RIGHT Syntax

SELECT column_list
FROM left_table
RIGHT [OUTER] JOIN right_table
  ON join_condition;

SQL RIGHT Parameters

  • left_table (table) - First (left) input table
  • right_table (table) - Second (right) input table whose rows are all preserved
  • join_condition (boolean) - Expression that links the two tables

Example Queries Using SQL RIGHT

-- 1. Show all employees, even if they lack department info
SELECT e.id, e.name, d.dept_name
FROM departments d
RIGHT JOIN employees e ON e.dept_id = d.id;

-- 2. Preserve all order lines and attach product data when available
SELECT ol.order_id, ol.product_id, p.product_name
FROM products p
RIGHT OUTER JOIN order_lines ol
  ON ol.product_id = p.id;

Expected Output Using SQL RIGHT

  • Each query returns every row from the right-hand table (employees, order_lines)
  • Rows without a match in the left-hand table show NULL in the columns coming from that left table

Use Cases with SQL RIGHT

  • Keep the right table intact while enriching it with optional data
  • Audit unmatched rows on the left side (e.g., orphan employees without departments)
  • Write readable queries when the natural primary dataset sits on the right side

Common Mistakes with SQL RIGHT

  • Assuming RIGHT JOIN is faster than LEFT JOIN; optimizers treat them the same
  • Forgetting to qualify columns, leading to ambiguous-column errors
  • Expecting columns from the left table to be non-NULL when there is no match

Related Topics

First Introduced In

SQL-92

Frequently Asked Questions

What is the difference between RIGHT JOIN and RIGHT OUTER JOIN?

They are the same. The keyword OUTER is optional and purely descriptive.

Can a RIGHT JOIN be rewritten as a LEFT JOIN?

Yes. Swap table order and use LEFT JOIN. Most optimizers do this internally.

Does RIGHT JOIN work with multiple join conditions?

Absolutely. Combine conditions with AND or OR inside the ON clause.

How do NULL values behave in a RIGHT JOIN?

If no matching row exists in the left table, every selected column from the left side is returned as NULL.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!