December 5, 2026

SQL Joins Explained: INNER, LEFT, RIGHT, FULL, and the Filter That Quietly Turns One Into Another

Two overlapping colored glass discs on a light table

A query asks which customers have never placed an order. It uses NOT IN, it runs without an error, and it returns nobody, even though two customers in the table have never ordered anything. One missing value in the orders table did that. Joins are the part of SQL where a query can look right, run cleanly and still answer a different question from the one asked. Every query below was run against the same small dataset in SQLite 3.45 and PostgreSQL 16, and the output shown is what the databases returned.

Video: Anton Putra on YouTube

The test data

Two small tables

Four customers and five orders, built to include the awkward cases real data always has:

CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT, city TEXT);
CREATE TABLE orders (id INTEGER PRIMARY KEY, customer_id INTEGER, total REAL, status TEXT);

INSERT INTO customers VALUES (1,'Ana','Boston'),(2,'Ben','Denver'),(3,'Cleo','Austin'),(4,'Dev',NULL);
INSERT INTO orders VALUES (101,1,40,'paid'),(102,1,15,'refunded'),(103,2,60,'paid'),
                          (104,NULL,25,'paid'),(105,9,30,'paid');

Ana has two orders, Ben has one, Cleo and Dev have none. Order 104 has no customer at all, and order 105 points at customer 9, who doesn’t exist. Production databases accumulate rows like these through guest checkouts, deleted accounts and imports, and they are exactly where joins disagree with each other.

What a join does

A join lines up rows from two tables wherever a condition holds, here orders.customer_id = customers.id. The join type decides what happens to rows that find no partner. That is the entire difference between the four common kinds.

Wooden card catalog drawers with labels

INNER JOIN: only matches

The definition

The PostgreSQL documentation on joined tables defines an inner join simply: for each row of the first table, the result has a row for each row in the second table that satisfies the join condition.

The result

SELECT c.name, o.id AS order_id, o.total
FROM customers c INNER JOIN orders o ON o.customer_id = c.id;
name  order_id  total
----  --------  -----
Ana   101       40.0
Ana   102       15.0
Ben   103       60.0

Three rows. Cleo and Dev vanished because they have no orders. Orders 104 and 105 vanished because they have no matching customer. Ana appears twice because she has two orders, and that repetition is normal: a join produces one row per matching pair, not one per customer.

When it’s the right choice

An inner join answers “show the things that have partners.” Order lines with their products, invoices with their customers, any case where an unmatched row would be an error to hide. Writing just JOIN means the same thing.

LEFT, RIGHT and FULL: keeping the unmatched

LEFT JOIN keeps every row on the left

PostgreSQL’s documentation describes a left outer join as an inner join first, then one extra row for every left-table row with no match, filled with nulls in the right table’s columns. The left table always appears in full.

name  order_id  total
----  --------  -----
Ana   102       15.0
Ana   101       40.0
Ben   103       60.0
Cleo
Dev

Cleo and Dev are back, with empty order columns. The orphan orders are still missing, because they are on the right side.

Ana’s two orders came back in a different order from the inner join, 102 before 101. Nothing was wrong. The SQLite SELECT documentation states that without an ORDER BY, the order of returned rows is undefined. Any query whose output feeds a report or a test needs an explicit sort.

RIGHT JOIN keeps every row on the right

name  order_id  total
----  --------  -----
Ana   101       40.0
Ana   102       15.0
Ben   103       60.0
      104       25.0
      105       30.0

Now every order is listed, including the two with no customer, and the customers without orders are gone. A right join is a left join with the tables swapped, and many teams simply write every outer join as a LEFT JOIN with the “keep everything” table first, so queries read the same way.

SQLite supported only left joins for most of its history. Its release notes for version 3.39.0, from June 2022, list “long overdue” support for RIGHT and FULL OUTER JOIN as the first change. Older copies of SQLite reject these queries outright.

FULL OUTER JOIN keeps everything

name  order_id  total
----  --------  -----
Ana   101       40.0
Ana   102       15.0
Ben   103       60.0
Cleo
Dev
      104       25.0
      105       30.0

All seven rows: matched pairs, customers without orders, and orders without customers. PostgreSQL returned the same seven rows. A full join is the tool for reconciling two lists, such as finding which records exist in one system but not the other.

CROSS JOIN: every combination

A cross join has no condition. PostgreSQL’s documentation describes it as every possible combination of rows, so tables of N and M rows give N times M results. Four customers crossed with five orders produced 20 rows. Forgetting the ON clause in some databases produces the same explosion by accident.

A green colander full of white eggs

WHERE vs ON: the filter that changes the join

The same condition, two places

The question: list every customer, with their paid orders where they have any. The condition o.status = 'paid' can go in two places.

In the WHERE clause:

SELECT c.name, o.id AS order_id, o.status
FROM customers c LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'paid';
name  order_id  status
----  --------  ------
Ana   101       paid
Ben   103       paid

In the ON clause:

SELECT c.name, o.id AS order_id, o.status
FROM customers c LEFT JOIN orders o ON o.customer_id = c.id AND o.status = 'paid';
name  order_id  status
----  --------  ------
Ana   101       paid
Ben   103       paid
Cleo
Dev

Why

PostgreSQL’s documentation explains it in one line: a restriction in the ON clause is processed before the join, while one in the WHERE clause is processed after it. That makes no difference to inner joins and a large difference to outer joins. In the WHERE version, Cleo’s and Dev’s rows had null status, null is not equal to ‘paid’, and the filter removed them. The left join silently became an inner join.

The rule of thumb: conditions about which right-side rows count as a match go in ON. Conditions about which final rows to keep go in WHERE.

Finding what’s missing: NOT IN and its trap

The anti-join

“Customers with no orders” is a left join looking for the empty rows:

SELECT c.name FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;

That returned Cleo and Dev. NOT EXISTS with a correlated subquery returned the same two names.

The version that returns nothing

SELECT name FROM customers WHERE id NOT IN (SELECT customer_id FROM orders);

SQLite returned zero rows. PostgreSQL returned (0 rows). The cause is order 104, whose customer_id is null.

The PostgreSQL documentation for subquery expressions spells it out: if there are no equal values and at least one value in the subquery is null, the result of NOT IN is null, not true. Asking whether Cleo’s id 3 is “not in (1, 1, 2, null, 9)” can’t be answered yes, because the null might have been 3. A WHERE clause keeps only rows where the condition is true, so every customer is filtered out.

Fixes

Adding WHERE customer_id IS NOT NULL inside the subquery made PostgreSQL return Cleo and Dev. Switching to NOT EXISTS or the left join anti-join avoids the problem without needing to remember it. A column that allows nulls today but has none yet is a query that works until the first guest checkout.

A calculator and a pen sitting on top of a piece of paper

Counting and summing across joins

**COUNT(*) counts the empty rows**

SELECT c.name, COUNT(*) AS orders
FROM customers c LEFT JOIN orders o ON o.customer_id = c.id GROUP BY c.name;

That gave Cleo and Dev one order each. The left join produced one row for each of them, with nulls, and COUNT(*) counts rows. Changing it to COUNT(o.id) counts only non-null order ids, and gave them the correct 0.

Joining two children doubles the sum

A third table, support tickets, gave Ana two tickets and Ben one. Summing Ana’s orders on their own returned $55. Joining customers to both orders and tickets in the same query, then summing, returned:

name  spent  tickets
----  -----  -------
Ana   110.0  4
Ben   60.0   1

Ana’s spending doubled to $110 and her ticket count to 4. Each of her two orders paired with each of her two tickets, making four rows, and the sum and count read every one. Nothing errored. Rewriting the query with a separate subquery for each total returned the true $55 and 2 tickets.

The pattern to watch: any time one parent row joins to two independent lists, the rows multiply. Aggregate each list separately first, then join the results, or use subqueries.

Nulls don’t join to nulls

Dev’s city is null. A self-join of customers on matching cities returned no pairs at all for him; a null never equals anything, including another null. That is the same rule that broke NOT IN, showing up from a different direction. Between undefined row order, nulls and multiplied rows, the join is rarely the problem. The surprises come from the data feeding it.

Sources

  • Table Expressions (Joined Tables), PostgreSQL 16 documentation: https://www.postgresql.org/docs/16/queries-table-expressions.html
  • Subquery Expressions, PostgreSQL 16 documentation: https://www.postgresql.org/docs/16/functions-subquery.html
  • SELECT, SQLite documentation: https://www.sqlite.org/lang_select.html
  • SQLite Release 3.39.0, release notes: https://www.sqlite.org/releaselog/3_39_0.html

920 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *