Skip to content

Commit

Permalink
BCA: DBMS: Add Natural Join
Browse files Browse the repository at this point in the history
  • Loading branch information
sounddrill31 authored Oct 6, 2024
1 parent 17ecc9d commit 3a91306
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions NEP2020/2024/BCA/3rdsem/dbms/unit3/imp.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,22 +258,24 @@ Consider two tables: `employees` and `departments`.
| 2 | IT |
| 3 | Marketing |

1. **Inner Join**: This join returns rows that have matching values in both tables.
1. **Natural Join (⨝)**: This join automatically matches columns with the same name and data type in both tables. You don't need to specify any condition. In this example, it would automatically join on the `dept_id` column, as it's present in both tables.

Example:
Notation:
R ⨝ S


Example:
```MySQL
SELECT employees.name, departments.dept_name FROM employees INNER JOIN departments ON employees.dept_id = departments.id;
SELECT employees.name, departments.dept_name FROM employees NATURAL JOIN departments;
```

Result:

| name | dept_name |
| ------- | --------- |
| Alice | HR |
| Bob | IT |
| David | HR |


| id | name | dept_name |
| --- | ------- | --------- |
| 1 | Alice | HR |
| 2 | Bob | IT |
| 4 | David | HR |

2. **Left Join (Left Outer Join)** (⟕): This join returns all rows from the left table (employees), and the matching rows from the right table (departments). If there’s no match, `NULL` is returned for columns from the right table.

Expand Down Expand Up @@ -336,6 +338,21 @@ Result:
| David | HR |
| NULL | Marketing |

5. **Inner Join**: This join returns rows that have matching values in both tables.

Example:
```MySQL
SELECT employees.name, departments.dept_name FROM employees INNER JOIN departments ON employees.dept_id = departments.id;
```

Result:

| name | dept_name |
| ------- | --------- |
| Alice | HR |
| Bob | IT |
| David | HR |


## Source:
- Questions: Dictated in class

0 comments on commit 3a91306

Please sign in to comment.