Aral Code

Joins Lesson 15 of 21

SQL LEFT JOIN, RIGHT JOIN and FULL JOIN

Keep all rows from one table with LEFT JOIN, find missing data such as customers with no orders and products never sold, and see how RIGHT and FULL joins compare.

An INNER JOIN drops rows that have no match. But sometimes the missing rows are exactly what you want to see: customers who never ordered, products nobody bought, students without a grade. For this, use a LEFT JOIN.

LEFT JOIN keeps everything from the left table

The "left" table is the one written first, after FROM. A LEFT JOIN returns all its rows. Where there's no match in the right table, the right-side columns are filled with NULL.

SQL All customers, with their orders if any
SELECT c.name, o.id AS order_id, o.order_date
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
ORDER BY c.name;

Carlo Mendoza appears with NULL in the order columns: he's a customer but has never ordered. With an INNER JOIN, he would have disappeared.

Finding rows with no match

Add WHERE right_table.id IS NULL to keep only the unmatched rows:

SQL Customers who have never ordered
SELECT c.name, c.barangay
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;
SQL Products that have never been sold
SELECT p.name, p.stock
FROM products p
LEFT JOIN order_items oi ON oi.product_id = p.id
WHERE oi.product_id IS NULL;

This is a powerful pattern. Aling Nena might stop ordering items that never sell.

Counting with LEFT JOIN

SQL Number of orders per customer, including zero
SELECT c.name, COUNT(o.id) AS orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id
ORDER BY orders DESC, c.name;

Missing grades

Which students are missing an English grade? Join students to only their English grades:

SQL Students without an English grade
SELECT s.first_name, s.last_name
FROM students s
LEFT JOIN grades g ON g.student_id = s.id AND g.subject = 'English'
WHERE g.student_id IS NULL;

Notice the extra condition AND g.subject = 'English' is inside the ON, not in WHERE. In a LEFT JOIN, conditions on the right table usually belong in ON; putting them in WHERE would remove the NULL rows you're looking for.

RIGHT JOIN

A RIGHT JOIN is the mirror image: it keeps all rows of the right table. A RIGHT JOIN B gives the same rows as B LEFT JOIN A. Most people just use LEFT JOIN and put the important table first.

SQL RIGHT JOIN (same result as the LEFT JOIN above)
SELECT c.name, o.id AS order_id
FROM orders o
RIGHT JOIN customers c ON o.customer_id = c.id
ORDER BY c.name;

FULL OUTER JOIN

A FULL JOIN keeps all rows from both tables, with NULLs wherever there's no match on either side. SQLite (since 3.39) and PostgreSQL support it; MySQL doesn't, but you can simulate it with a LEFT JOIN, a RIGHT JOIN and UNION.

Join types at a glance

JoinReturns
INNER JOINOnly matching rows
LEFT JOINAll rows of the left table + matches (or NULL)
RIGHT JOINAll rows of the right table + matches (or NULL)
FULL JOINAll rows of both tables

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does a LEFT JOIN return?
2. How do you find customers with no orders?
3. With LEFT JOIN, which counts 0 correctly for customers with no orders?
4. A RIGHT JOIN B gives the same rows as…

By Sir Reyel · Last updated 2026-12-04