0% found this document useful (0 votes)
2 views

SQL Q&a

Uploaded by

Shakti Rathore
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL Q&a

Uploaded by

Shakti Rathore
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Let's explain the different types of SQL joins using two example tables:

### Example Tables:


- **Employees**: Contains information about employees.
- **Departments**: Contains information about departments.

```plaintext
Employees Table:
+----+----------+--------------+
| id | name | department_id|
+----+----------+--------------+
| 1 | Alice | 101 |
| 2 | Bob | 102 |
| 3 | Charlie | NULL |
| 4 | David | 101 |
| 5 | Eve | 103 |
+----+----------+--------------+

Departments Table:
+-------------+---------------+
| department_id | department_name |
+-------------+---------------+
| 101 | HR |
| 102 | IT |
| 103 | Finance |
| 104 | Marketing |
+-------------+---------------+
```

### 1. **INNER JOIN**


- **Explanation**: Returns only the rows where there is a match in both tables.
- **Query**:

```sql
SELECT Employees.name, Departments.department_name
FROM Employees
INNER JOIN Departments ON Employees.department_id = Departments.department_id;
```

- **Result**:

```plaintext
+----------+------------------+
| name | department_name |
+----------+------------------+
| Alice | HR |
| Bob | IT |
| David | HR |
| Eve | Finance |
+----------+------------------+
```

### 2. **LEFT JOIN (LEFT OUTER JOIN)**


- **Explanation**: Returns all rows from the left table (Employees), and the
matched rows from the right table (Departments). If there is no match, the result
is `NULL` on the right side.
- **Query**:

```sql
SELECT Employees.name, Departments.department_name
FROM Employees
LEFT JOIN Departments ON Employees.department_id = Departments.department_id;
```

- **Result**:

```plaintext
+----------+------------------+
| name | department_name |
+----------+------------------+
| Alice | HR |
| Bob | IT |
| Charlie | NULL |
| David | HR |
| Eve | Finance |
+----------+------------------+
```

### 3. **RIGHT JOIN (RIGHT OUTER JOIN)**


- **Explanation**: Returns all rows from the right table (Departments), and the
matched rows from the left table (Employees). If there is no match, the result is
`NULL` on the left side.
- **Query**:

```sql
SELECT Employees.name, Departments.department_name
FROM Employees
RIGHT JOIN Departments ON Employees.department_id = Departments.department_id;
```

- **Result**:

```plaintext
+----------+------------------+
| name | department_name |
+----------+------------------+
| Alice | HR |
| Bob | IT |
| David | HR |
| Eve | Finance |
| NULL | Marketing |
+----------+------------------+
```

### 4. **FULL JOIN (FULL OUTER JOIN)**


- **Explanation**: Returns all rows when there is a match in either left or
right table. Records that do not match in either table will have `NULL` values in
place.
- **Query**:

```sql
SELECT Employees.name, Departments.department_name
FROM Employees
FULL OUTER JOIN Departments ON Employees.department_id =
Departments.department_id;
```

- **Result**:
```plaintext
+----------+------------------+
| name | department_name |
+----------+------------------+
| Alice | HR |
| Bob | IT |
| Charlie | NULL |
| David | HR |
| Eve | Finance |
| NULL | Marketing |
+----------+------------------+
```

### 5. **CROSS JOIN**


- **Explanation**: Returns the Cartesian product of the two tables, meaning
every row from the first table is combined with every row from the second table.
- **Query**:

```sql
SELECT Employees.name, Departments.department_name
FROM Employees
CROSS JOIN Departments;
```

- **Result**:

```plaintext
+----------+------------------+
| name | department_name |
+----------+------------------+
| Alice | HR |
| Alice | IT |
| Alice | Finance |
| Alice | Marketing |
| Bob | HR |
| Bob | IT |
| Bob | Finance |
| Bob | Marketing |
| Charlie | HR |
| Charlie | IT |
| Charlie | Finance |
| Charlie | Marketing |
| David | HR |
| David | IT |
| David | Finance |
| David | Marketing |
| Eve | HR |
| Eve | IT |
| Eve | Finance |
| Eve | Marketing |
+----------+------------------+
```

### 6. **SELF JOIN**


- **Explanation**: A self join is a regular join, but the table is joined with
itself.
- **Scenario**: Let's say you want to find pairs of employees who share the same
department.
- **Query**:

```sql
SELECT e1.name AS Employee1, e2.name AS Employee2
FROM Employees e1
INNER JOIN Employees e2 ON e1.department_id = e2.department_id
WHERE e1.id <> e2.id;
```

- **Result**:

```plaintext
+----------+-----------+
| Employee1| Employee2 |
+----------+-----------+
| Alice | David |
| David | Alice |
+----------+-----------+
```

These are the main types of SQL joins, each serving different use cases depending
on the data you need to retrieve from your tables.

You might also like