Left, Right and Inner Joins

What are the four types of JOINs in SQL?

Inner join, left join, right join and full outer join are the four main JOIN types in SQL. Here is what each returns, with sample tables and results.

On this page
  1. The short answer
  2. The sample tables
  3. INNER JOIN: only matches
  4. LEFT OUTER JOIN: every employee
  5. RIGHT OUTER JOIN: every department
  6. FULL OUTER JOIN: everything from both sides
  7. CROSS JOIN, briefly
  8. Which databases support each type
  9. Which to learn first

The short answer

Beginners often ask: what are the four types of JOINs in SQL? Most tutorials give the same list: INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN and FULL OUTER JOIN. They differ only in which rows they keep: matched rows, unmatched rows from one side, or unmatched rows from both sides.

The SQL standard also includes CROSS JOIN, which pairs every row with every row. Some databases support NATURAL JOIN, which matches on every column name the two tables share. Many teams avoid it, because adding a column with a shared name can change results without any warning. A self join is a pattern rather than a keyword: it joins a table to itself, usually with two aliases.

The sample tables

The examples use two small tables, departments and employees:

CREATE TABLE departments (
    dept_id   INT PRIMARY KEY,
    dept_name VARCHAR(50)
);

CREATE TABLE employees (
    emp_id  INT PRIMARY KEY,
    name    VARCHAR(50),
    dept_id INT
);

INSERT INTO departments (dept_id, dept_name) VALUES
    (1, 'Sales'),
    (2, 'Engineering'),
    (3, 'Support');

INSERT INTO employees (emp_id, name, dept_id) VALUES
    (101, 'Ana', 1),
    (102, 'Ben', 1),
    (103, 'Chloe', 2),
    (104, 'Dev', NULL);

Support has no employees. Dev’s dept_id is NULL, so Dev isn’t assigned to any department. That gives each of the four joins a different result to show.

INNER JOIN: only matches

SELECT e.name AS employee, d.dept_name AS department
FROM employees AS e
INNER JOIN departments AS d
    ON d.dept_id = e.dept_id;
employee department
Ana Sales
Ben Sales
Chloe Engineering

Only rows that match on both sides appear. Dev is dropped, because NULL never equals another value in a join condition. Support is dropped, because no employee points to it.

LEFT OUTER JOIN: every employee

SELECT e.name AS employee, d.dept_name AS department
FROM employees AS e
LEFT JOIN departments AS d
    ON d.dept_id = e.dept_id;
employee department
Ana Sales
Ben Sales
Chloe Engineering
Dev NULL

Every employee appears. Dev stays, with NULL for the department. Support still doesn’t appear, because a left join keeps the employees side, and Support has no employees.

RIGHT OUTER JOIN: every department

SELECT e.name AS employee, d.dept_name AS department
FROM employees AS e
RIGHT JOIN departments AS d
    ON d.dept_id = e.dept_id;
employee department
Ana Sales
Ben Sales
Chloe Engineering
NULL Support

Every department appears. Support stays, with NULL for the employee. Dev disappears, because a right join keeps the departments side, and Dev has no department to attach to.

FULL OUTER JOIN: everything from both sides

SELECT e.name AS employee, d.dept_name AS department
FROM employees AS e
FULL OUTER JOIN departments AS d
    ON d.dept_id = e.dept_id;
employee department
Ana Sales
Ben Sales
Chloe Engineering
Dev NULL
NULL Support

Every row from both tables appears at least once. Matched rows are combined, and unmatched rows from either side keep NULL in the missing columns.

CROSS JOIN, briefly

A CROSS JOIN has no ON condition. It pairs every row from one table with every row from the other. With 4 employees and 3 departments, the result has 12 rows (4 × 3). It’s useful for building combinations, but the row count multiplies quickly on large tables, so check it before you run it.

Which databases support each type

  • MySQL supports INNER, LEFT, RIGHT and CROSS JOIN, but not FULL OUTER JOIN. You can emulate a full outer join by combining a LEFT JOIN and a RIGHT JOIN with UNION.
  • SQLite supports RIGHT and FULL OUTER JOIN from version 3.39.0, released in June 2022.
  • PostgreSQL and SQL Server support all four of these types.

Which to learn first

For most everyday work, inner and left joins cover most of what you need. Right and full joins are most useful when you’re comparing two lists and need the unmatched rows from each side.

Choosing between the types for a real query is a separate step. When to use left, right, or inner join walks through that choice with the same kind of examples.

The complete guide to left, right and inner joins

Left Outer Join vs Right Outer Join in SQL
Read the guide

Keep exploring

Guides from other topics

All topics →