← Guides

SQL JOINs Explained with Real Examples (INNER, LEFT, RIGHT, FULL)

A practical walkthrough of every SQL JOIN type using a small customers/orders dataset, with the mistakes that silently duplicate or drop rows.

SQL · 9 min read

By the FormulAI editorial team · Reviewed November 19, 2026

Sample data used in this guide

Table customers (4 rows). Delta has never ordered.
idnamecountry
1AcmeFR
2BorealisPT
3CobaltFR
4DeltaUK
Table orders (6 rows). Order 106 references customer 9, which no longer exists.
idcustomer_idamount
1011120
102180
1032300
104350
1053150
106975

The sample dataset

We use two tables throughout this guide. customers(id, name, country) holds 4 rows, and orders(id, customer_id, amount, created_at) holds 6 rows. One customer has never ordered, and one order references a customer_id that no longer exists. These two edge cases are exactly what makes each JOIN type behave differently, so keep them in mind as you read.

INNER JOIN: only matching rows

An INNER JOIN returns rows where the join condition is true on both sides. The customer without orders disappears, and so does the orphan order. Use it when you only care about records that exist in both tables, for example revenue per active customer.

SELECT c.name, SUM(o.amount) AS revenue
FROM customers c
INNER JOIN orders o ON o.customer_id = c.id
GROUP BY c.name
ORDER BY revenue DESC;
Result: Delta (no orders) and orphan order 106 disappear.
namerevenue
Borealis300
Acme200
Cobalt200

LEFT JOIN: keep everything on the left

A LEFT JOIN keeps every customer, even those without orders; their order columns are NULL. This is the right choice for questions like "which customers never ordered?". Add WHERE o.id IS NULL to isolate them. A common bug is filtering the right table in the WHERE clause (WHERE o.amount > 100), which silently turns the LEFT JOIN back into an INNER JOIN. Put that condition in the ON clause instead.

SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;
Result: the only customer without an order.
name
Delta

RIGHT and FULL OUTER JOIN

RIGHT JOIN is a mirrored LEFT JOIN; most teams rewrite it as a LEFT JOIN with the tables swapped for readability. FULL OUTER JOIN keeps unmatched rows from both sides, which makes it ideal for reconciliation: comparing an export from your CRM against your billing system and listing differences. MySQL does not support FULL OUTER JOIN natively; emulate it with a LEFT JOIN UNION a RIGHT JOIN.

FULL OUTER JOIN between customers and orders (columns c.name, o.id): both edge cases show up with NULLs.
c.nameo.id
Acme101
Acme102
Borealis103
Cobalt104
Cobalt105
DeltaNULL
NULL106

The duplicate-rows trap

If you join customers to orders and then to payments, each customer row is multiplied by orders × payments. Summing amounts after such a join inflates totals. The fix is to aggregate each child table in a subquery or CTE first, then join the aggregated results on the customer id.

WITH o AS (
  SELECT customer_id, SUM(amount) AS ordered
  FROM orders GROUP BY customer_id
), p AS (
  SELECT customer_id, SUM(amount) AS paid
  FROM payments GROUP BY customer_id
)
SELECT c.name, o.ordered, p.paid
FROM customers c
LEFT JOIN o ON o.customer_id = c.id
LEFT JOIN p ON p.customer_id = c.id;
With a payments table holding Acme 150 and Borealis 300, the aggregated query returns correct totals:
nameorderedpaid
Acme200150
Borealis300300
Cobalt200NULL
DeltaNULLNULL

Checklist before running a JOIN in production

1) Count rows before and after the join to detect duplication. 2) Make sure join columns are indexed. 3) Prefer explicit column lists over SELECT *. 4) Decide consciously how NULLs should be treated (COALESCE(o.amount, 0) for totals). When you ask FormulAI for a query, describe the tables and the expected result in one sentence; the generated SQL comes with an explanation you can check against this list.

Common errors and how to fix them

ERROR: column reference "id" is ambiguous

Cause: Both tables have an id column and the query does not say which one (PostgreSQL message; MySQL shows "Column 'id' in field list is ambiguous").

Fix: Prefix every column with its table alias: c.id, o.id.

ERROR: column "c.name" must appear in the GROUP BY clause or be used in an aggregate function

Cause: A non-aggregated column is selected without being listed in GROUP BY.

Fix: Add it to GROUP BY, or group by c.id and c.name so two customers with the same name are not merged.

ERROR 1064: You have an error in your SQL syntax ... near 'FULL OUTER JOIN'

Cause: MySQL does not support FULL OUTER JOIN.

Fix: Write a LEFT JOIN, then UNION, then the same SELECT as a RIGHT JOIN. UNION removes the duplicated common rows.

No message: totals are too high

Cause: Joining two child tables (orders and payments) multiplies each row, so the sum is inflated.

Fix: Compare COUNT(*) before and after the join, then aggregate each child table in a CTE as in the example above.

FAQ

Are JOIN and INNER JOIN the same?

Yes. The INNER keyword is optional in every common engine; writing it just makes the intent clearer.

Should a filter go in ON or in WHERE?

For an INNER JOIN the result is the same. For a LEFT JOIN, a filter on the right table in WHERE removes unmatched rows: put it in ON to keep them.

Do joins slow queries down?

Not when join columns are indexed. A slow join usually comes from an unindexed foreign key or a function applied to the column in the condition.

Go further

How this guide was made

Every formula, query and script in this guide was run on a real sample file (Excel 365, Google Sheets, PostgreSQL, MySQL or Python 3) before publication. We document the edge cases we hit during testing, such as text-formatted numbers, empty months or NULL values. AI helps us draft and translate; a human checks each example and updates the guide when software versions change. Spotted an error? Write to contact@formulai-app.com.