SQL Joins
SQL Joins
retrieve data from multiple tables based on a related column between them. Here's a detailed look
at different types of SQL joins:
**Definition:**
An INNER JOIN returns only the rows where there is a match in both tables.
**Syntax:**
```sql
SELECT columns
FROM table1
ON table1.common_column = table2.common_column;
```
**Example:**
```sql
-- Employees Table
----------------------------------
1 | John | 1
2 | Jane | 2
3 | Jack | 3
-- Departments Table
DepartmentID | DepartmentName
----------------------------
1 | HR
2 | IT
3 | Finance
```
```sql
FROM Employees
ON Employees.DepartmentID = Departments.DepartmentID;
```
**Result:**
```sql
Name | DepartmentName
---------------------
John | HR
Jane | IT
Jack | Finance
```
**Definition:**
A LEFT JOIN returns all rows from the left table and the matched rows from the right table. If no
match is found, NULL values are returned for columns from the right table.
**Syntax:**
```sql
SELECT columns
FROM table1
ON table1.common_column = table2.common_column;
```
**Example:**
```sql
FROM Employees
ON Employees.DepartmentID = Departments.DepartmentID;
```
**Result:**
```sql
Name | DepartmentName
---------------------
John | HR
Jane | IT
Jack | Finance
```
```sql
----------------------------------
4 | Jill | NULL
```
```sql
Name | DepartmentName
---------------------
John | HR
Jane | IT
Jack | Finance
Jill | NULL
```
### 3. RIGHT (OUTER) JOIN
**Definition:**
A RIGHT JOIN returns all rows from the right table and the matched rows from the left table. If no
match is found, NULL values are returned for columns from the left table.
**Syntax:**
```sql
SELECT columns
FROM table1
ON table1.common_column = table2.common_column;
```
**Example:**
```sql
FROM Employees
ON Employees.DepartmentID = Departments.DepartmentID;
```
```sql
DepartmentID | DepartmentName
----------------------------
4 | Marketing
```
```sql
Name | DepartmentName
---------------------
John | HR
Jane | IT
Jack | Finance
NULL | Marketing
```
**Definition:**
A FULL JOIN returns all rows when there is a match in either left or right table. Rows without a match
in one of the tables will have NULL values for columns from that table.
**Syntax:**
```sql
SELECT columns
FROM table1
ON table1.common_column = table2.common_column;
```
**Example:**
```sql
FROM Employees
ON Employees.DepartmentID = Departments.DepartmentID;
```
This includes all employees and all departments, with NULLs where there is no match:
```sql
Name | DepartmentName
---------------------
John | HR
Jane | IT
Jack | Finance
Jill | NULL
NULL | Marketing
```
**Definition:**
A CROSS JOIN returns the Cartesian product of the two tables, i.e., all possible combinations of rows.
**Syntax:**
```sql
SELECT columns
FROM table1
```
**Example:**
```sql
FROM Employees
```
**Result:**
```sql
Name | DepartmentName
---------------------
John | HR
John | IT
John | Finance
Jane | HR
Jane | IT
Jane | Finance
Jack | HR
Jack | IT
Jack | Finance
```
**Definition:**
A SELF JOIN is a regular join but the table is joined with itself.
**Syntax:**
```sql
```
**Example:**
```sql
FROM Employees e1
ON e1.ManagerID = e2.EmployeeID;
```
**Result:**
```sql
Employee | Manager
------------------
John | Jane
Jack | John
```
**Definition:**
A NATURAL JOIN is based on all columns in the two tables that have the same name and selects rows
with equal values in the relevant columns.
**Syntax:**
```sql
SELECT columns
FROM table1
```
**Example:**
```sql
SELECT *
FROM Employees
```
This would automatically join based on the `DepartmentID` without specifying it explicitly in the `ON`
clause.
### Summary
- **INNER JOIN:** Returns matched rows.
- **LEFT JOIN:** Returns all rows from the left table, matched rows from the right table.
- **RIGHT JOIN:** Returns all rows from the right table, matched rows from the left table.
- **FULL JOIN:** Returns all rows from both tables, with NULLs for non-matches.
- **NATURAL JOIN:** Joins based on columns with the same name in both tables.
Understanding and using these joins will allow you to efficiently retrieve and analyze data across
multiple related tables.