Skip to main content
Log inGet a demo
Back to SQL Dictionary
Joins

SQL JOIN

What is SQL JOIN?

SQL JOIN is a database operation that combines rows from two or more tables based on a related column between them. It allows you to create a single result set from multiple tables, providing a way to retrieve data that is spread across various tables and establish relationships between them.

When you would use it

You would use SQL JOIN when you need to retrieve data from multiple tables that are related by a common column or key. Common scenarios for using JOIN include:

  1. Retrieving data from related tables: When you have data distributed across multiple tables and you need to combine it into a single result set for analysis or reporting.

  2. Creating relationships: To establish connections between tables using keys or foreign keys, enabling you to retrieve data from one table based on data in another.

  3. Normalizing data: When data is divided into multiple tables to avoid redundancy, JOINs help to reconstruct the data into a complete form when querying.

  4. Combining data: When you need to bring together data from different sources or databases for a unified view.

Syntax

The SQL JOIN syntax typically includes the JOIN clause, which specifies the tables to combine and the related columns. The basic syntax is as follows:

SELECT column_list
FROM table1
JOIN table2 ON table1.column = table2.column;
  • column_list: A list of columns to retrieve from the joined tables.
  • table1 and table2: The names of the tables to join.
  • column: The common column or key that relates the tables.

There are different types of JOIN operations, including INNER JOIN, LEFT JOIN (or LEFT OUTER JOIN), RIGHT JOIN (or RIGHT OUTER JOIN), and FULL JOIN (or `FULL OUTER JOIN). Each type specifies how rows from the tables are combined and whether unmatched rows are included.

Parameter values

  • column_list: A list of column names you want to select from the joined tables.
  • table1 and table2: The names of the tables to be joined.
  • column: The related column or key that connects the tables.

Example query

Suppose you have two tables, "employees" and "departments," and you want to retrieve a list of employees and their corresponding department names. You can use an INNER JOIN like this:

SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id;

Example table response

Assuming the "employees" and "departments" tables contain the following data:

employees:

| employee_id | employee_name | department_id |
| ----------- | ------------- | ------------- |
| 1           | John Smith    | 101           |
| 2           | Mary Johnson  | 102           |
| 3           | Sam Brown     | 101           |

departments:

| department_id | department_name |
| ------------- | --------------- |
| 101           | HR              |
| 102           | Finance         |

The query mentioned earlier would return the following result:

| employee_id | employee_name | department_name |
| ----------- | ------------- | --------------- |
| 1           | John Smith    | HR              |
| 2           | Mary Johnson  | Finance         |
| 3           | Sam Brown     | HR              |

This result combines data from both tables, showing the employees and their corresponding department names.

Use cases

  • Retrieving data from related tables.
  • Establishing relationships between tables.
  • Normalizing data into multiple tables to avoid redundancy.
  • Combining data from different sources.

SQL languages this is available for

SQL JOIN is a fundamental feature of SQL and is available in nearly all relational database management systems (RDBMS), including but not limited to:

  • MySQL
  • PostgreSQL
  • Oracle Database
  • SQL Server
  • SQLite
  • IBM Db2
  • MariaDB

The specific syntax and behavior may vary slightly between different database systems, but the concept of joining tables is universal in SQL.

Related

SQL - Wildcard

SQL LEFT JOIN

SQL RIGHT JOIN

Share

Book a demo

Work email

Start syncing data in minutes

Check out this 5 minute interactive demo to learn how you can sync customer data to any business tool.

Hightouch Audiences user interface.

Activate your data in less than 5 minutes