Equi Join in SQL

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

Overview

Equi join in SQL is a type of Inner Join in which two or more tables are joined by equating the common column values of the tables. The table participating in Equi join should have at least one common column on which equality operation can be performed.

What is Equi Join in SQL?

diagram of Equi Join in SQL

Equi join uses the equality operator(=) only to compare the values of the common column. Each common column value of one table is compared to each common column value of the other table and if the values of both the common columns of the table match, that row value of both the tables is retrieved in the result of Equi join in SQL.

The common column names in the tables performing Equi join need not be the same.

The names of the column which are equated in the Equi join can be of different names. Also, the resultant table of Equi join contains both columns of tables performing Equi join.

Syntax

The Equi join in SQL can be written with the WHERE clause and also the ON clause for equating the columns of the participating tables.

Example

  • with WHERE clause
  • with ON clause

Why Use Equi Join in SQL?

Equi join in SQL is used mostly to join two or more tables on the basis of equality of column values of the tables.

While working on web applications the data are stored in different tables in the database. So, if one needs to combine the data of two tables to get better data insights then the need for joining the table arises.

Equi join is one of the SQL Joins which can be used to join the tables for better data insights.

For example - Let's understand Equi join with the help of two tables.

  1. Products table
idproduct_name
1Iwatch
2IPhone
3IPod
4macbook
  1. Discount table
iddicount_percentage
220%
417%

Suppose we have a need to know the discounts available on the products then can we only know that by the Product table or the discount table?

No, we have to combine both the table to know what are the discounts available on the products.

With the help of inner join, we can join both the products table and the discount table to know on which products the discount exists.

The column on which we can perform Equi join is the id column in both tables.

The resultant table after performing Equi join on products and the discount table will be -

idproduct_nameiddiscount_percentage
2Iphone220%
4macbook417%

With the help of the resultant table of Equi join we are able to know that only products iPhone and MacBook have a discount of 20% and 17% respectively.

More Examples of Equi join in SQL

Let's see examples of Equi join on the following tables.

  1. Student table
idnamecity
1RakeshDelhi
2AmitVaranasi
3VipulGorakhpur
4TejanDelhi
5ShashankLucknow
  1. Course table
course_idcourse_namesid
100AI3
101Machine learing4
102Java1
103Python5
100AI2
  1. College_Detail table
college_nameLocationsid
UIETKanpur4
IIT-DDelhi1
IIT-BMumbai5
BBDLucknow3
IIT-GGuwahati2

Example 1

Find all the details of the student and the course they are enrolled in.

Result -

idnamecitycourse_idcourse_namesid
1RakeshDelhi102java1
2AmitVaranasi100AI2
3VipulGorakhpur100AI3
4TejanDelhi101Machine learning4
5ShashankLucknow103Python5

Example 2

Find the all student name, city and the college name, and location of the college in which they studied.

Result -

namecitycollege_nameLocation
RakeshDelhiIIT-DDelhi
AmitVaranasiIIT-GGuwahati
VipulGorakhpurBBDLucknow
TejanDelhiUIETKanpur
ShashankLucknowIIT-BShashank

Example 3

For all the students find the student name, course name and the coellge name in which they pursued the course.

Result -

namecourse_namecolege_name
RakeshJavaIIT-D
AmitAIIIT-G
VipulAIBBD
TejanMachine learningUIET
ShashankPythonIIT-B

How is Equi Join Different from Natural Join?

The difference between Equi join and natural join lies in column names which are equated for performing equi join or natural join.

In Natural join, the tables should have the same column names to perform equality operations on them. In Equi join, the common column name can be the same or different. Also in the resultant table of Equi join the common column of both the tables are present. But in the natural join, the common column is present only once in the resultant table.

If we would have performed natural join on the above products and discount table then the resultant table would be -

idproduct_namediscount_percentage
2Iphone20%
4macbook17%

Conclusion

  • Equi join in SQL combines two or more than two tables on the basis of equality or common column values.
  • The common column name of the participating tables in Equi join need not be the same.
  • Equi join in SQL can be written with WHERE as well as ON clause.
  • Equi join has the column name of both the tables in the resulting table whereas natural join only has one of the column names in the resulting table.
  • The common column name of participating tables in the natural join should be the same but the common colmn name me to be different in case if Equi joins.

Learn more about Equi-join and Non-Equi Join in SQL