SQL join types come down to five choices. INNER JOIN returns only rows that match on both sides. LEFT JOIN keeps every row from the left table and fills the right side with NULL when nothing matches. RIGHT JOIN does the same for the right table. FULL JOIN keeps unmatched rows from both. CROSS JOIN returns every pairing.[1] The verdict: write INNER when you want matches only, LEFT when the left table is the list you must not lose, and reach for FULL or CROSS only on purpose. MySQL's manual recommends LEFT over RIGHT for portability.[2] Below: the PostgreSQL docs example with exact outputs, a reference table, the ON versus WHERE trap and the mistakes that cost the most time. To tidy a long join before you read it, paste it into the SQL formatter.
What a join does
A joined table is derived from two other tables according to the rules of the join type. The general shape is T1 join_type T2 [ join_condition ], and joins can be chained or nested. Without parentheses, PostgreSQL nests JOIN clauses left to right.[1] SQLite describes the same model from the other end: every join starts from the Cartesian product of the left and right datasets, and the join condition then decides which combined rows survive.[3]
That mental model explains most surprises. Inner and outer joins differ only in what happens to rows the condition rejects.
The join types and what each returns
| Join type | Rows returned | Unmatched rows |
|---|
| INNER JOIN | One row for each pair that satisfies the join condition[1] | Dropped from both sides |
| LEFT [OUTER] JOIN | Inner join first, then each left row with no match is added with NULLs in the right table's columns. At least one row per left row[1] | Left kept, right dropped |
| RIGHT [OUTER] JOIN | The converse: at least one row per right row, NULLs in the left table's columns[1] | Right kept, left dropped |
| FULL [OUTER] JOIN | Inner join, plus unmatched left rows padded with NULLs, plus unmatched right rows padded with NULLs[1] | Both kept |
| CROSS JOIN | Every combination of rows. With N and M rows you get N * M rows[1] | No condition, so nothing is unmatched |
The words INNER and OUTER are optional in PostgreSQL, and INNER is the default. LEFT, RIGHT and FULL imply an outer join.[1] So JOIN alone means an inner join and LEFT JOIN means LEFT OUTER JOIN.
Worked example: the PostgreSQL docs tables
The PostgreSQL documentation uses two small tables, and its outputs are the clearest reference there is. They are reproduced here as printed in the docs.[1] Table t1 holds rows 1/a, 2/b, 3/c. Table t2 holds rows 1/xxx, 3/yyy, 5/zzz. The key num exists in both, and the values 2 and 5 each have no partner.
-- t1: (num, name) 1 a | 2 b | 3 c -- t2: (num, value) 1 xxx | 3 yyy | 5 zzz => SELECT * FROM t1 INNER JOIN t2 ON t1.num = t2.num; num | name | num | value -----+------+-----+------- 1 | a | 1 | xxx 3 | c | 3 | yyy (2 rows) => SELECT * FROM t1 LEFT JOIN t2 ON t1.num = t2.num; num | name | num | value -----+------+-----+------- 1 | a | 1 | xxx 2 | b | | 3 | c | 3 | yyy (3 rows) => SELECT * FROM t1 RIGHT JOIN t2 ON t1.num = t2.num; num | name | num | value -----+------+-----+------- 1 | a | 1 | xxx 3 | c | 3 | yyy | | 5 | zzz (3 rows) => SELECT * FROM t1 FULL JOIN t2 ON t1.num = t2.num; num | name | num | value -----+------+-----+------- 1 | a | 1 | xxx 2 | b | | 3 | c | 3 | yyy | | 5 | zzz (4 rows) Read the row counts, not just the rows. Inner returns 2, left returns 3 because row 2/b survives, right returns 3 because row 5/zzz survives, and full returns 4 because both survive. The docs also show t1 CROSS JOIN t2 returning 9 rows, which is 3 times 3.[1]
The docs state two equivalences worth memorizing. FROM T1 CROSS JOIN T2 is the same as FROM T1 INNER JOIN T2 ON TRUE, and also the same as FROM T1, T2.[1] Note the duplicate num column in the ON outputs above. The next section removes it.
ON, USING and NATURAL
ON is the general form: any Boolean expression of the kind you would put in WHERE. A pair of rows matches when it evaluates to true.[1] USING is shorthand for the case where both tables name the join column the same way. USING (a, b) produces the condition ON T1.a = T2.a AND T1.b = T2.b, and it prints each listed column once.[1] With t1 and t2 above, INNER JOIN t2 USING (num) returns the columns num, name, value, so the duplicate num disappears.[1]
NATURAL goes one step further and builds the USING list for you from every column name that appears in both tables.[1] That convenience is the risk. PostgreSQL's docs call USING reasonably safe from column changes in the joined relations, and NATURAL "considerably more risky", because a schema change that adds a new matching column name makes the join combine that column too.[1] If two tables share no column names, NATURAL JOIN behaves like CROSS JOIN.[1] SQLite's wording differs: it says NATURAL "has no effect" in that case.[3]
MySQL documents the coalesced columns with its own example. For tables t1(a, b) holding 1/x and 2/y, and t2(a, c) holding 2/z and 3/w, NATURAL LEFT JOIN returns columns a, b, c with the rows 1/x/NULL and 2/y/z. The NATURAL RIGHT JOIN returns columns a, c, b with the rows 2/z/y and 3/w/NULL.[2] The coalesced column a takes its value from the inner table of the join, so the left join shows values of t1.a and the right join shows values of t2.a.[2]
The ON versus WHERE trap on outer joins
On an inner join, a filter in ON and the same filter in WHERE give identical results. On an outer join they do not. PostgreSQL shows both versions on the same data.[1]
=> SELECT * FROM t1 LEFT JOIN t2 ON t1.num = t2.num AND t2.value = 'xxx'; num | name | num | value -----+------+-----+------- 1 | a | 1 | xxx 2 | b | | 3 | c | | (3 rows) => SELECT * FROM t1 LEFT JOIN t2 ON t1.num = t2.num WHERE t2.value = 'xxx'; num | name | num | value -----+------+-----+------- 1 | a | 1 | xxx (1 row) The docs give the reason: a restriction in ON is processed before the join, while a restriction in WHERE is processed after it. "That does not matter with inner joins, but it matters a lot with outer joins."[1] The first query keeps all three left rows. The second returns one row, because the unmatched rows carry NULL in t2.value, and WHERE excludes rows that evaluate to false or NULL.[3] A WHERE filter on the right table's columns quietly turns your LEFT JOIN into an inner join. MySQL's manual states the working division of labor: ON specifies how tables are joined, and WHERE restricts which rows appear in the result.[2]
Find rows with no match: the anti-join
MySQL documents a pattern that uses the NULL padding on purpose. When a LEFT JOIN finds no matching right row, it uses a row of NULLs for the right table, so you can filter for those rows.[2]
SELECT left_tbl.* FROM left_tbl LEFT JOIN right_tbl ON left_tbl.id = right_tbl.id WHERE right_tbl.id IS NULL; This returns every row in left_tbl whose id is not present in right_tbl.[2] Test the NULL on a column that cannot be NULL in real data, such as the right table's key, or a genuine NULL will look like a missing row. That is a consequence of the padding rule, not something the sources spell out, so check your schema.
Self-joins need aliases
To join a table to itself, give each use a different alias. PostgreSQL's example pairs a parent row with a child row in one people table.[1]
SELECT * FROM people AS mother JOIN people AS child ON mother.id = child.mother_id; Once you alias a table you cannot refer to it by its original name elsewhere in the query. The docs mark SELECT * FROM my_table AS m WHERE my_table.a > 5 as wrong.[1]
Which databases support which joins
The PostgreSQL syntax lists INNER, LEFT, RIGHT and FULL outer joins, plus CROSS and NATURAL.[1] SQLite's SELECT page describes RIGHT and FULL joins, and defines a FULL join as a combination of a LEFT JOIN and a RIGHT JOIN.[3] The page does not tell you which SQLite release added them, so check the version you ship.
MySQL is the one to watch. The MySQL 8.4 join grammar lists {LEFT|RIGHT} [OUTER] JOIN and [INNER | CROSS] JOIN, and no FULL keyword.[2] If a query written for PostgreSQL uses FULL JOIN, expect to rewrite it when it moves to MySQL. The same page says JOIN, CROSS JOIN and INNER JOIN are syntactic equivalents in MySQL, while in standard SQL they are not: INNER JOIN takes an ON clause and CROSS JOIN does not.[2]
Bookmarkable reference: what breaks
| Situation | What happens | Fix |
|---|
| Filter on the right table in WHERE after a LEFT JOIN | Unmatched rows are removed, so the query behaves like an inner join. PostgreSQL's example drops from 3 rows to 1[1] | Put the condition in ON, or test IS NULL deliberately |
| NATURAL JOIN on tables that change | A new column with a shared name becomes part of the join condition[1] | Use USING (col) or ON |
| Comma mixed with JOIN in MySQL | JOIN binds tighter than the comma, so t1, t2 JOIN t3 ON (t1.i1 = t3.i3) raises Unknown column 't1.i1' in 'on clause'[2] | Use JOIN everywhere, or parenthesize (t1, t2) |
| ON refers to a table not yet joined | t1 JOIN t2 ON (i1 = i3) JOIN t3 fails with Unknown column 'i3' in 'on clause' because ON sees only its operands[2] | Move the condition to the join that has both tables |
| FULL JOIN in MySQL | The 8.4 grammar lists no FULL keyword[2] | Rewrite the query for MySQL, or use a database that lists it |
| One-to-many relationship | An inner join gives a row for each matching row in T2, so a left row with 3 matches appears 3 times[1] | Aggregate the many side first, or expect the repeats |
| No join condition | Cartesian product, N * M rows[1] | Add ON or USING |
| RIGHT JOIN in shared code | Works, but MySQL recommends LEFT JOIN for portability[2] | Swap the table order and use LEFT |
The MySQL comma precedence error, in full
This is the join mistake that fails loudest, because the error message points at the wrong table. The MySQL manual builds the failing case from three single-row tables.[2]
CREATE TABLE t1 (i1 INT, j1 INT); CREATE TABLE t2 (i2 INT, j2 INT); CREATE TABLE t3 (i3 INT, j3 INT); -- Fails: Unknown column 't1.i1' in 'on clause' SELECT * FROM t1, t2 JOIN t3 ON (t1.i1 = t3.i3); -- Works: parentheses group t1 and t2 as one operand SELECT * FROM (t1, t2) JOIN t3 ON (t1.i1 = t3.i3); -- Works: JOIN instead of the comma SELECT * FROM t1 JOIN t2 JOIN t3 ON (t1.i1 = t3.i3); The manual explains that t1, t2 JOIN t3 is read as (t1, (t2 JOIN t3)), so the ON clause only sees t2 and t3.[2] PostgreSQL warns about the same thing: FROM T1, T2 INNER JOIN T3 ON condition is not the same as the CROSS JOIN form, because the condition cannot reference T1 there.[1] The habit that avoids all of it is to write explicit JOIN keywords and no commas.
Common mistakes
Reaching for LEFT JOIN, then filtering the right side in WHERE. You asked to keep every left row and then deleted the unmatched ones. Move the filter into ON if you want to restrict which right rows are matched, as in the PostgreSQL example above.[1]
Forgetting that a join can multiply rows. The inner join definition gives one output row per matching pair.[1] Summing a column from the one side after joining the many side counts it once per match. Check the row count before and after.
Using SELECT * with ON. JOIN ON returns all columns from T1 followed by all columns from T2, so a shared key appears twice, while JOIN USING returns each listed pair once.[1] List the columns you want instead.
Treating CROSS JOIN as a mistake. It is a legitimate join when you want every combination, such as a date list against a product list. The mistake is producing one by omission. SQLite adds a wrinkle: its CROSS JOIN returns the same result as INNER JOIN, but stops the optimizer from reordering the tables, and the SQLite docs advise avoiding it except where you want that manual control.[3]
When a join query grows past a screen, readability is the next bug. Run it through the SQL formatter so each JOIN and ON sits on its own line, then check the conditions one join at a time. For what to do when the query is correct but slow, see the guide to SQL performance, indexing and query tuning.
Choosing a join in practice
Start from the question. If you need only records that exist in both tables, use INNER JOIN. If the left table is your list of record, such as customers, and the right is optional detail, such as orders, use LEFT JOIN. If you want the customers who have no orders, add the IS NULL filter on the right key. If you must see unmatched rows from both sides, such as when reconciling two systems, use FULL JOIN and confirm your database lists it. Use CROSS JOIN for combinations, and write USING or ON in place of NATURAL. Prefer LEFT over RIGHT, since you can always swap the table order.
Sources
Table Expressions, PostgreSQL documentation (accessed December 3, 2026)
JOIN Clause, MySQL 8.4 Reference Manual (accessed December 3, 2026)
3. SELECT, SQLite documentation (accessed December 3, 2026)
Originally published at wowhow.cloud

