An INNER JOIN returns only the rows that have a match in both tables. A LEFT JOIN keeps every row from the first (left) table and fills the other table's columns with NULL where there is no match. Use INNER JOIN when you only want matched rows, and LEFT JOIN when you must not lose any rows from the first table.


INNER JOIN

An INNER JOIN matches rows from two tables wherever a condition is true and returns one combined row per match. Suppose order_items records a product_id and you want product names:

SELECT i.order_id, p.product_name, i.quantity
FROM order_items i
INNER JOIN products p ON p.product_id = i.product_id;

The ON clause says: for each order line, find the product with the same product_id. The word INNER is optional, so JOIN on its own means the same thing. Order lines with no matching product disappear from the result, and so do products that have never been ordered.


LEFT JOIN

A LEFT JOIN keeps every row from the table named first, in the FROM clause, and shows NULL for the right table's columns where nothing matches:

SELECT p.product_name, su.supplier_name
FROM products p
LEFT JOIN suppliers su ON su.supplier_id = p.supplier_id;

Every product appears. A product with no supplier shows NULL for supplier_name, rather than vanishing. Which table you name first matters: products LEFT JOIN suppliers keeps every product, while suppliers LEFT JOIN products keeps every supplier.


Finding what is missing

The NULLs a LEFT JOIN creates are useful. To find products that never sold, start from products, LEFT JOIN the order lines, and keep rows where the order side is empty:

SELECT p.product_id, p.product_name
FROM products p
LEFT JOIN order_items i ON i.product_id = p.product_id
WHERE i.order_id IS NULL;

Use IS NULL on a column that is never NULL in a real match, such as order_id on a sales line, so you only pick up genuine gaps.


Worked example

You have three products (Spade, Fork, Trowel) and sales lines for Spade and Fork only. An INNER JOIN of products to sales lines returns rows for Spade and Fork. A LEFT JOIN from products returns Spade, Fork and Trowel, with NULL in the sales columns for Trowel. The row count tells you which you used: always check it against what you expect.


Common mistakes


Common questions


What about RIGHT JOIN?

A RIGHT JOIN keeps every row of the second table. A LEFT JOIN with the tables swapped does the same job, so many people use only LEFT JOIN.


Do these work in every database?

INNER JOIN and LEFT JOIN are standard SQL and work in common systems, including SQLite, MySQL, PostgreSQL and SQL Server.


Which is faster?

Do not choose by speed. Choose by the rows you need: matched rows only, or every row from one side.


Learn the full skill: SQL for Beginners: Ask Questions of Your Data with Databases

More plain-English answers: see all quick answers.

Learn something new every week

One short, practical tip each Thursday, plus news when new courses open. Free, and you can unsubscribe in one click.

We'll never share your email. See our privacy policy.