0% found this document useful (0 votes)
31 views9 pages

SQL Joins

Uploaded by

yashnikam844
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views9 pages

SQL Joins

Uploaded by

yashnikam844
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Understanding SQL joins is crucial for working with relational databases, as they allow you to

retrieve data from multiple tables based on a related column between them. Here's a detailed look
at different types of SQL joins:

### 1. INNER JOIN

**Definition:**

An INNER JOIN returns only the rows where there is a match in both tables.

**Syntax:**

```sql

SELECT columns

FROM table1

INNER JOIN table2

ON table1.common_column = table2.common_column;

```

**Example:**

Consider two tables, `Employees` and `Departments`:

```sql

-- Employees Table

EmployeeID | Name | DepartmentID

----------------------------------

1 | John | 1

2 | Jane | 2

3 | Jack | 3

-- Departments Table

DepartmentID | DepartmentName

----------------------------

1 | HR

2 | IT
3 | Finance

```

An INNER JOIN query:

```sql

SELECT Employees.Name, Departments.DepartmentName

FROM Employees

INNER JOIN Departments

ON Employees.DepartmentID = Departments.DepartmentID;

```

**Result:**

```sql

Name | DepartmentName

---------------------

John | HR

Jane | IT

Jack | Finance

```

### 2. LEFT (OUTER) JOIN

**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

LEFT JOIN table2

ON table1.common_column = table2.common_column;

```
**Example:**

```sql

SELECT Employees.Name, Departments.DepartmentName

FROM Employees

LEFT JOIN Departments

ON Employees.DepartmentID = Departments.DepartmentID;

```

**Result:**

```sql

Name | DepartmentName

---------------------

John | HR

Jane | IT

Jack | Finance

```

If there's an employee without a department, say:

```sql

EmployeeID | Name | DepartmentID

----------------------------------

4 | Jill | NULL

```

The result would include Jill with a NULL department:

```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

RIGHT JOIN table2

ON table1.common_column = table2.common_column;

```

**Example:**

```sql

SELECT Employees.Name, Departments.DepartmentName

FROM Employees

RIGHT JOIN Departments

ON Employees.DepartmentID = Departments.DepartmentID;

```

If there's a department without any employees, say:

```sql

DepartmentID | DepartmentName

----------------------------

4 | Marketing

```

The result would include Marketing with a NULL employee:

```sql

Name | DepartmentName
---------------------

John | HR

Jane | IT

Jack | Finance

NULL | Marketing

```

### 4. FULL (OUTER) JOIN

**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

FULL OUTER JOIN table2

ON table1.common_column = table2.common_column;

```

**Example:**

```sql

SELECT Employees.Name, Departments.DepartmentName

FROM Employees

FULL OUTER JOIN Departments

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

```

### 5. CROSS JOIN

**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

CROSS JOIN table2;

```

**Example:**

```sql

SELECT Employees.Name, Departments.DepartmentName

FROM Employees

CROSS JOIN Departments;

```

**Result:**

```sql

Name | DepartmentName

---------------------

John | HR
John | IT

John | Finance

Jane | HR

Jane | IT

Jane | Finance

Jack | HR

Jack | IT

Jack | Finance

```

### 6. SELF JOIN

**Definition:**

A SELF JOIN is a regular join but the table is joined with itself.

**Syntax:**

```sql

SELECT a.columns, b.columns

FROM table a, table b

WHERE a.common_column = b.common_column;

```

**Example:**

Consider a table `Employees` with a `ManagerID` that references another employee.

```sql

SELECT e1.Name AS Employee, e2.Name AS Manager

FROM Employees e1

INNER JOIN Employees e2

ON e1.ManagerID = e2.EmployeeID;

```

**Result:**
```sql

Employee | Manager

------------------

John | Jane

Jack | John

```

### 7. NATURAL JOIN

**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

NATURAL JOIN table2;

```

**Example:**

Assuming `Employees` and `Departments` have a common column `DepartmentID`:

```sql

SELECT *

FROM Employees

NATURAL JOIN Departments;

```

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.

- **CROSS JOIN:** Returns the Cartesian product.

- **SELF JOIN:** Joins a table with itself.

- **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.

You might also like