Discuss Equijoin & Natural Join with examples.

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

FeatureEqui JoinNatural Join
Join ConditionMust be written explicitly (ON A.col = B.col)Implicit (based on same-named columns)
Duplicate ColumnsKeeps duplicates unless handled manuallyAutomatically removes duplicates
FlexibilityMore control (custom join conditions)Less control, but cleaner for simple joins

Leave a Reply

Your email address will not be published. Required fields are marked *