SQL Joins Explained: A Complete Guide with Examples

1738339138.jpg

Written by Aayush Saini · 3 minute read · Jan 31, 2025 . SQL, 21

SQL joins are used to combine records from two or more tables based on a related column. They help retrieve meaningful data by linking different tables. Understanding joins is crucial for database querying and data analysis. In this guide, we will explore different types of SQL joins with practical examples.

1. INNER JOIN

An INNER JOIN returns only the matching records from both tables. If there is no match, the record is excluded.

Example

SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;

This query returns employees who have a matching department in the departments table.

 

2. LEFT JOIN (or LEFT OUTER JOIN)

A LEFT JOIN returns all records from the left table and the matching records from the right table. If no match is found, NULL values are returned.

Example

SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.id;

This will return all employees, even if they do not belong to any department.

 

3. RIGHT JOIN (or RIGHT OUTER JOIN)

A RIGHT JOIN returns all records from the right table and the matching records from the left table. If no match is found, NULL values are returned for the left table's columns.

Example

SELECT employees.name, departments.department_name
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.id;

This ensures all departments are displayed, even if they have no employees.

 

4. FULL JOIN (or FULL OUTER JOIN)

A FULL JOIN returns all records from both tables, filling missing matches with NULL values.

Example

SELECT employees.name, departments.department_name
FROM employees
FULL JOIN departments ON employees.department_id = departments.id;

This includes all employees and all departments, whether they have matches or not.

 

5. CROSS JOIN

A CROSS JOIN returns the Cartesian product of the two tables, meaning every row from the first table is combined with every row from the second table.

Example

SELECT employees.name, departments.department_name
FROM employees
CROSS JOIN departments;

If employees has 5 records and departments has 3, the result will have 5 × 3 = 15 rows.

 

6. SELF JOIN

A SELF JOIN is when a table is joined with itself to compare rows within the same table.

Example

SELECT e1.name AS Employee, e2.name AS Manager
FROM employees e1
JOIN employees e2 ON e1.manager_id = e2.id;

This retrieves employees and their respective managers from the same employees table.

 

Conclusion

SQL joins are powerful tools for querying databases efficiently. Depending on the requirement, different joins help extract relevant data by connecting multiple tables effectively. Mastering SQL joins will significantly improve your ability to manage and analyze data in relational databases.

 

Share   Share