Academic Block

SQL RIGHT JOIN Get Certified

Learn the concept of RIGHT JOIN and how to achieve the same result in SQLite by reversing the table order and using LEFT JOIN.

What is RIGHT JOIN?

The RIGHT JOIN statement returns all rows from the right table. Matching rows from the left table are included. When no match exists on the left, the left-side columns contain NULL.

Important for this tutorial: SQLite does not support RIGHT JOIN in most versions used by online editors. Every working example below therefore uses the equivalent pattern: put the table you want to keep on the left and write a LEFT JOIN. The result is identical to a true RIGHT JOIN.

Basic RIGHT JOIN Concept (SQLite Version)

In a database that supports RIGHT JOIN you would write FROM left_table RIGHT JOIN right_table. In SQLite we reverse the tables and use LEFT JOIN instead.

What this code does: Keeps every course. Physics and Chemistry have matching students, so the student names appear. Biology has student_id 5, which does not exist in the students table, so the student name is NULL. The query returns 3 rows.

WITH students(student_id, student_name) AS (
  VALUES
    (1, 'Aarav'),
    (2, 'Meera'),
    (3, 'Kabir')
),
courses(course_id, course_name, student_id) AS (
  VALUES
    (101, 'Physics', 1),
    (102, 'Chemistry', 2),
    (103, 'Biology', 5)
)
SELECT
  c.course_name,
  s.student_name
FROM courses AS c
LEFT JOIN students AS s
  ON c.student_id = s.student_id
ORDER BY c.course_id;

Keeping All Instructors

When the table you care about most is the one that must never lose rows, place it on the left of a LEFT JOIN. This is the practical SQLite equivalent of a RIGHT JOIN.

What this code does: Lists every instructor. Anika has two courses, Vivek has one, and both Sana and Rohan have no courses (NULL). The query returns 5 rows.

WITH instructors(instructor_id, instructor_name) AS (
  VALUES
    (1, 'Anika'),
    (2, 'Vivek'),
    (3, 'Sana'),
    (4, 'Rohan')
),
course_assignments(assignment_id, instructor_id, course_name) AS (
  VALUES
    (101, 1, 'Data Science'),
    (102, 2, 'Web Development'),
    (103, 1, 'Database Systems')
)
SELECT
  i.instructor_name,
  c.course_name
FROM instructors AS i
LEFT JOIN course_assignments AS c
  ON i.instructor_id = c.instructor_id
ORDER BY i.instructor_id, c.assignment_id;

Keeping All Products (Even Without Orders)

A common business need is to show every product, including those that have never been ordered.

What this code does: Preserves every product. Wireless Mouse and Desk Lamp have orders; USB Hub has none and appears with a NULL customer name. The query returns 4 rows (Wireless Mouse appears twice because it has two orders).

WITH products(product_id, product_name) AS (
  VALUES
    (10, 'Wireless Mouse'),
    (20, 'Desk Lamp'),
    (30, 'USB Hub')
),
orders(order_id, product_id, customer_name) AS (
  VALUES
    (501, 10, 'Anaya'),
    (502, 20, 'Rahul'),
    (503, 10, 'Kavya')
)
SELECT
  p.product_name,
  o.customer_name
FROM products AS p
LEFT JOIN orders AS o
  ON p.product_id = o.product_id
ORDER BY p.product_id;

Finding Records Without a Match

After a LEFT JOIN that preserves the table of interest, filter with WHERE left_side_column IS NULL to locate the unmatched rows.

What this code does: Shows only the customers who have never made a payment. Maya and Rohan have no matching payment rows. The query returns 2 rows.

WITH customers(customer_id, customer_name) AS (
  VALUES
    (1, 'Isha'),
    (2, 'Arjun'),
    (3, 'Maya'),
    (4, 'Rohan')
),
payments(payment_id, customer_id, amount) AS (
  VALUES
    (101, 1, 500),
    (102, 2, 750)
)
SELECT
  c.customer_name
FROM customers AS c
LEFT JOIN payments AS p
  ON c.customer_id = p.customer_id
WHERE p.payment_id IS NULL
ORDER BY c.customer_id;

Multiple Matching Rows

When several left-table rows match one preserved row, that preserved row appears once for each match.

What this code does: Mira has two songs and therefore appears twice. Karan has one song. Tara has no songs and appears with NULL. The query returns 4 rows.

WITH artists(artist_id, artist_name) AS (
  VALUES
    (1, 'Mira'),
    (2, 'Karan'),
    (3, 'Tara')
),
songs(song_id, artist_id, song_title) AS (
  VALUES
    (101, 1, 'Morning Light'),
    (102, 1, 'Open Sky'),
    (103, 2, 'New Road')
)
SELECT
  a.artist_name,
  s.song_title
FROM artists AS a
LEFT JOIN songs AS s
  ON a.artist_id = s.artist_id
ORDER BY a.artist_id, s.song_id;

RIGHT JOIN Concept with COUNT()

You can count related rows while still showing every record from the table you want to preserve.

What this code does: Counts classes for every instructor. Neel has 2, Pia has 1, and Arman has 0. The query returns 3 rows.

WITH instructors(instructor_id, instructor_name) AS (
  VALUES
    (1, 'Neel'),
    (2, 'Pia'),
    (3, 'Arman')
),
classes(class_id, instructor_id, class_name) AS (
  VALUES
    (101, 1, 'Photography'),
    (102, 1, 'Editing'),
    (103, 2, 'Drawing')
)
SELECT
  i.instructor_name,
  COUNT(c.class_id) AS class_count
FROM instructors AS i
LEFT JOIN classes AS c
  ON i.instructor_id = c.instructor_id
GROUP BY i.instructor_id, i.instructor_name
ORDER BY i.instructor_id;

Multiple Conditions in the ON Clause

Extra conditions can be added to the ON clause. Only right-side rows that satisfy every condition are matched; the preserved table still keeps all of its rows.

What this code does: Shows every warehouse together with deliveries that have quantity ≥ 10. Lower-quantity deliveries are ignored for matching purposes. West Warehouse has no qualifying deliveries and appears with NULL.

WITH warehouses(warehouse_id, warehouse_name) AS (
  VALUES
    (1, 'North Warehouse'),
    (2, 'South Warehouse'),
    (3, 'East Warehouse'),
    (4, 'West Warehouse')
),
deliveries(delivery_id, warehouse_id, item_name, quantity) AS (
  VALUES
    (101, 1, 'Laptop', 12),
    (102, 1, 'Monitor', 4),
    (103, 2, 'Keyboard', 15),
    (104, 3, 'Mouse', 3)
)
SELECT
  w.warehouse_name,
  d.item_name,
  d.quantity
FROM warehouses AS w
LEFT JOIN deliveries AS d
  ON w.warehouse_id = d.warehouse_id
 AND d.quantity >= 10
ORDER BY w.warehouse_id;

COALESCE for Default Values

When a preserved row has no match, the joined columns are NULL. COALESCE replaces those NULLs with a useful default.

What this code does: Lists every movie. Movies that have ratings keep their scores; Hidden World has no rating and receives 0 instead of NULL. The query returns 4 rows (Skyline appears twice).

WITH movies(movie_id, movie_title) AS (
  VALUES
    (10, 'Skyline'),
    (20, 'The Journey'),
    (30, 'Hidden World')
),
ratings(rating_id, movie_id, score) AS (
  VALUES
    (1, 10, 8),
    (2, 20, 9),
    (3, 10, 7)
)
SELECT
  m.movie_title,
  COALESCE(r.score, 0) AS score
FROM movies AS m
LEFT JOIN ratings AS r
  ON m.movie_id = r.movie_id
ORDER BY m.movie_id;

LEFT JOIN vs RIGHT JOIN

Feature
LEFT JOIN
RIGHT JOIN
Preserved table
Left table
Right table
Matching rows
Included
Included
Unmatched preserved rows
Included (other side NULL)
Included (other side NULL)
SQLite support
Fully supported
Not supported (use reverse + LEFT JOIN)

Advantages of the RIGHT JOIN Concept

  • Keeps every record from the table you designate as primary.
  • Includes matching records from the secondary table when they exist.
  • Useful when the “main” table is naturally written on the right in other SQL dialects.
  • Helps identify unmatched records with a simple IS NULL filter.
  • Works with COUNT, SUM, COALESCE, GROUP BY and ORDER BY once rewritten as LEFT JOIN.

Best Practices

  • Remember that true RIGHT JOIN is not available in most SQLite environments.
  • For maximum compatibility, reverse the table order and write a LEFT JOIN.
  • Place the table whose rows must never disappear on the left of the LEFT JOIN.
  • Use clear table aliases so the rewritten query stays readable.
  • Use WHERE column IS NULL to locate rows that have no match.
  • Use COALESCE when NULL values should become a meaningful default.
  • Keep join conditions in the ON clause; use WHERE only for final filtering.

Although many databases support a native RIGHT JOIN, SQLite does not. All interactive examples on this page therefore use the equivalent and fully supported pattern: put the table you want to preserve on the left and write a LEFT JOIN. Click Test My Code on any example to run it live, or Code Explanation to walk through the query block by block.

Ctrl + Enter to run code in the editor Esc to close the editor

🧪 Test Your SQL Code

Edit the SQL code on the left and click “Run Code” to see the result on the right.

📝 SQL Code
👁️ Preview (query result)

Click “Run Code” to see the result here.