SQL Joins

Anonymous contributor's avatar
Anonymous contributor
Published Aug 4, 2021Updated Dec 3, 2025

The JOIN clause combines rows from two or more tables by joining them together with other results based on common column values specified using an ON condition.

In order to efficiently store data, we often spread related information across multiple tables. Connecting or joining these tables to find interesting data is a common task that a data analyst, data scientist, or data engineer will often encounter.

  • Learn to analyze data with SQL and prepare for technical interviews.
    • Includes 9 Courses
    • With Certificate
    • Beginner Friendly.
      18 hours
  • In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
    • Beginner Friendly.
      5 hours

INNER JOIN

INNER JOIN is the default JOIN and it will only return results matching the condition specified by ON.

The following only returns rows from the books and authors tables on the condition that books.author_id and authors.id columns match:

SELECT *
FROM books
INNER JOIN authors
ON books.author_id = authors.id;

OUTER JOIN

An OUTER JOIN will combine rows from different tables even if the join condition is not met. These commands include the following:

  • LEFT JOIN, which combines matches with all rows from the left-side table.
  • RIGHT JOIN, which combines matches with all rows from the right-side table.
  • FULL OUTER JOIN, which combines matches with all rows from the left- and right-side tables.

CROSS JOIN

A CROSS JOIN clause combines each row from one table with each row from another in the result set. This result is also known as a Cartesian product. The following query returns every combination of shirt_color and pants_color:

SELECT shirts.shirt_color,
pants.pants_color
FROM shirts
CROSS JOIN pants;

For example, here’s the shirts table:

shirt_color
white
grey

And the pants table:

pants_color
light
dark

The result of the query would contain every combination of the two tables:

shirt_color pants_color
white light
white dark
grey light
grey dark

UNION

The UNION clause is used to combine results that appear from multiple SELECT statements and filter duplicates.

SELECT name
FROM first_names
UNION
SELECT name
FROM last_names;

For example, given a first_names table with a column name containing rows of data “James” and “Hermione”, and a last_names table with a column name containing rows of data “James”, “Hermione” and “Cassidy”, the result of this query would contain three names:

name
Cassidy
James
Hermione

All contributors

Learn SQL on Codecademy

  • Learn to analyze data with SQL and prepare for technical interviews.
    • Includes 9 Courses
    • With Certificate
    • Beginner Friendly.
      18 hours
  • In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
    • Beginner Friendly.
      5 hours