Skip to content

SQL Joins: Definition, Types and Examples

Quick answer

Define each SQL join by what it keeps, see the same two tables through INNER and LEFT JOIN with exact row counts, and learn the anti-join pattern.

SQL Joins: Definition, Types and Examples

SQL joins combine rows from two or more tables based on a related column, usually a key. The join type decides which rows survive: INNER JOIN keeps only matches, LEFT JOIN keeps every left-table row, RIGHT JOIN keeps every right-table row, and FULL OUTER JOIN keeps all rows from both sides.

Key takeaways

  • Each join type is defined by which unmatched rows it keeps.
  • LEFT JOIN plus a NULL check finds rows with no match, called an anti-join.
  • A WHERE filter on the right table can silently turn a LEFT JOIN into an inner join.
  • Engine support differs: check your database's manual before using RIGHT or FULL OUTER JOIN.

On this page:

JoinKeepsUnmatched rows get
INNER JOINOnly rows that match in both tablesDropped
LEFT JOINAll left rows plus matchesNULL in right columns
RIGHT JOINAll right rows plus matchesNULL in left columns
FULL OUTER JOINAll rows from both tablesNULL on the missing side

What do SQL joins mean?

A join pairs rows from one table with rows from another where a condition holds, normally ON a.id = b.a_id. The PostgreSQL documentation defines an inner join this way: for each row R1 of T1, the joined table has a row for each row in T2 that satisfies the join condition with R1.

The outer joins build on that. For a left outer join, PostgreSQL says an inner join is performed first, then each T1 row that matches nothing in T2 is added with null values in the T2 columns. A right join is the converse. A full join adds unmatched rows from both sides.

How do joins work, with real row counts?

Take two made-up tables you can run in SQLite or PostgreSQL. Three customers, three orders, and Meera has not ordered.

CREATE TABLE customers (id INTEGER, name TEXT);
INSERT INTO customers VALUES (1, 'Asha'), (2, 'Ravi'), (3, 'Meera');
CREATE TABLE orders (id INTEGER, customer_id INTEGER, amount INTEGER);
INSERT INTO orders VALUES (101, 1, 500), (102, 1, 300), (103, 2, 1200);

SELECT c.name, o.amount
FROM customers AS c
INNER JOIN orders AS o ON c.id = o.customer_id;  -- 3 rows

SELECT c.name, o.amount
FROM customers AS c
LEFT JOIN orders AS o ON c.id = o.customer_id;   -- 4 rows

INNER JOIN returns three rows: Asha 500, Asha 300, Ravi 1200. LEFT JOIN returns four, because Meera appears once with amount NULL. Asha shows up twice because she has two orders; a join returns one row per matching pair.

Where are joins used?

Almost every analyst query uses them, because business data is split across tables. Illustrative scenario: an Indian edtech team wants students who enrolled but never submitted an assignment. That is an anti-join, and the MySQL 8.4 manual describes the idea: when a LEFT JOIN finds no matching right-table row, a row of NULLs is used, and you can use this to find rows with no counterpart in another table.

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

For deeper practice, see SQL joins explained with examples and SQL interview questions and answers.

What are the common mistakes?

  • Filtering the right table in WHERE. LEFT JOIN orders o ... WHERE o.amount > 400 removes Meera, because NULL fails the test. The result behaves like an inner join. Put the condition in ON to keep her.
  • Assuming every engine supports every join. The SQLite 3.39.0 release notes, dated 2022-06-25, list support for RIGHT and FULL OUTER JOIN as added then. The MySQL 8.4 manual page cited here does not describe FULL OUTER JOIN, so check your version before relying on it.
  • Preferring RIGHT JOIN. The MySQL manual recommends LEFT JOIN over RIGHT JOIN to keep code portable across databases.
  • Forgetting duplicate rows. A one-to-many match multiplies rows, as Asha showed.

A primary key and a foreign key are the columns joins usually match on. In MySQL, per its 8.4 manual, JOIN, CROSS JOIN and INNER JOIN are syntactic equivalents. Window functions are the next step after joins; see SQL window functions. If you need tables to practise on, read creating SQL tables.

Next step

Paste the tables above into SQLite or PostgreSQL, run all four queries, and predict each row count before you press enter. Then pick a dataset from SQL projects for data analysts. Also read SQL vs MySQL to know which engine you are learning on.

Frequently asked questions

What is the difference between INNER JOIN and LEFT JOIN?

INNER JOIN returns only rows that match in both tables. LEFT JOIN returns every row from the left table and fills the right-table columns with NULL where no match exists.

What does a FULL OUTER JOIN return?

It returns all matched rows, plus unmatched rows from both tables. Per the PostgreSQL docs, unmatched rows from either side appear with null values in the other table's columns.

Does MySQL support FULL OUTER JOIN?

The MySQL 8.4 manual's join page does not describe FULL OUTER JOIN. Check the manual for your version and test on your server before relying on it. SQLite added RIGHT and FULL OUTER JOIN in 3.39.0.

How do I find rows with no match in another table?

Use a LEFT JOIN and filter with WHERE right_table.key IS NULL. This returns left-table rows that have no counterpart, such as customers with no orders.

Which join should I use in a data analyst role?

Use INNER JOIN when you need only matched rows and LEFT JOIN when you must keep every row from the main table. Most day-to-day queries use one of these two.

Share LinkedIn X WhatsApp
×

Our Courses

Practice-Based Learning Tracks, Supercharged By A.I.