DEV Community

Anh Trần Tuấn
Anh Trần Tuấn

Posted on • Originally published at tuanh.net on

Facts About Different Types of SQL Joins: How They Work and When to Use Them

1. Introduction to SQL Joins

1.1 What Are SQL Joins?

Image

SQL joins allow you to retrieve data from two or more tables based on a related column between them. By using joins, you can merge rows from these tables, making it easier to analyze and report data.

1.2 Why Are SQL Joins Important?

Joins are fundamental for performing complex queries that require data from multiple sources. They are commonly used in data analysis, reporting, and even in building applications that require integrated data.

1.3 How to Use SQL Joins?

To use a join, you specify the type of join, the tables you want to combine, and the conditions that define how the tables relate to each other.

2. Types of SQL Joins and How They Work

Image

2.1 Inner Join

Inner Join returns records that have matching values in both tables. It is the most common type of join.

Image

SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.department_id;
Enter fullscreen mode Exit fullscreen mode

If an employee is in a department, the query returns both the employee's name and the department name. If there's no match, the row is excluded.

2.2 Left (Outer) Join

Left Join returns all records from the left table, and the matched records from the right table. If no match is found, NULL values are returned for columns from the right table.

Image

SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
Enter fullscreen mode Exit fullscreen mode

This query returns all employees, including those without a department. For employees with no department, the department name is NULL.

2.3 Right (Outer) Join

Right Join is the opposite of a Left Join. It returns all records from the right table and the matched records from the left table.

Image

SELECT employees.name, departments.department_name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id;
Enter fullscreen mode Exit fullscreen mode

The result includes all departments, even those with no employees assigned. For such departments, the employee's name is NULL.

2.4 Full (Outer) Join

Full Join returns all records when there is a match in either left or right table records. It includes all rows from both tables, with NULLs in places where the join condition is not met.

Image

SELECT employees.name, departments.department_name
FROM employees
FULL OUTER JOIN departments
ON employees.department_id = departments.department_id;
Enter fullscreen mode Exit fullscreen mode

The query returns all employees and departments, showing NULL for departments with no employees and for employees with no departments.

2.5 Cross Join

Cross Join returns the Cartesian product of the two tables, meaning it combines all rows from the first table with all rows from the second table.

Image

SELECT employees.name, departments.department_name
FROM employees
CROSS JOIN departments;
Enter fullscreen mode Exit fullscreen mode

If there are 5 employees and 3 departments, the result will have 15 rows, each employee paired with every department.

2.6 Self Join

Self Join is a regular join but the table is joined with itself.

Image

SELECT e1.name AS Employee, e2.name AS Manager
FROM employees e1
JOIN employees e2
ON e1.manager_id = e2.employee_id;
Enter fullscreen mode Exit fullscreen mode

The query lists employees alongside their managers, useful for hierarchical data like organizational structures.

3. Conclusion

Understanding these types of SQL joins—Inner, Left, Right, Full, Cross, and Self—empowers you to write more efficient and meaningful queries. Each join type serves a specific purpose, so knowing when and how to use them is key to working effectively with relational databases.

Mastering SQL joins is essential for any data professional. By using the right type of join, you can unlock powerful insights from your data. If you have any questions or need further clarification, feel free to leave a comment below!

Read posts more at : Facts About Different Types of SQL Joins: How They Work and When to Use Them

Top comments (0)