Discuss Equijoin & Natural Join with examples.
Answer:-
1. Equi Join
Definition:
- An Equi Join is a type of inner join that combines rows from two or more tables based on a condition that compares columns using an equality operator (
=
). - It requires you to explicitly specify the join condition.
Example:
Assume we have the following tables:
Sailors(sid, sname, rating, age) Reserves(sid, bid, day)
Query using Equi Join:
SELECT Sailors.sid, sname, rating, age, bid, day FROM Sailors JOIN Reserves ON Sailors.sid = Reserves.sid;
Key Point:
- You must specify the column to join on.
- If columns have the same name, both are retained in the result unless selected specifically.
2. Natural Join
Definition:
- A Natural Join automatically joins tables based on all columns with the same names in both tables.
- It removes duplicate columns from the result.
Example:
Using the same tables:
SELECT * FROM Sailors NATURAL JOIN Reserves;
Key Point:
- Joins on the common column(s)
sid
automatically. - No need to write an explicit condition.
- Removes duplicate
sid
from output.
Comparison Table
Feature | Equi Join | Natural Join |
---|---|---|
Join Condition | Must be written explicitly (ON A.col = B.col ) | Implicit (based on same-named columns) |
Duplicate Columns | Keeps duplicates unless handled manually | Automatically removes duplicates |
Flexibility | More control (custom join conditions) | Less control, but cleaner for simple joins |