What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Equi Join and Non-Equi Join in SQL (with Examples)

  • Feb 26, 2024
  • 7 Minutes Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Tanish Mallik
Equi Join and Non-Equi Join in SQL (with Examples)

Equi Joins and Non-Equi Joins are used to retrieve data from different tables of a schema based on given conditions, but there are differences between them. In this article, we will explore more about Equi and Non-Equi Joins which are a superset of these joins in SQL.

What is Equi Join in SQL?

We have already learned about different types of joins which helps us to easily find commonality between different tables.

Equi Joins in SQL are used to perform operations between different tables where there are common columns.

They are more or less like INNER Joins, they compare different column data based on similarities given. It usually uses the equality “=” sign to compare the data between given columns. It matches the data until and unless the requirements are met and retrieves it.

Equi Joins only retrieves the data from the tables once the compared columns are matched against given conditions. It does not fetch unmatchable data. Equi Join uses the WHERE condition with the equality  “= “ symbol.

Syntax:

SELECT
column1,
column2,
column3
FROM
table1
JOIN
table2 ON table1.column = table2.column;

 

Let us understand the use of Equi-Join through an example

Table: Employees

Equi Join SQL Employees Table

Table: Departments

Equi Join SQL Departments Table

SQL Query:

SELECT
E.EmployeeID,
E.FirstName,
E.LastName,
D.DepartmentName,
E.Salary
FROM
Employees E
JOIN
Departments D ON E.DepartmentID = D.DepartmentID;

 

Output:

Equi Join SQL Output

In this example, we used Equi-Join between the Employees and Departments tables based on the common column to retrieve insightful information.

What is Non-Equi Join in SQL?

Non-Equi Joins are used in SQL to combine the data of two or more tables based on a condition that is not specifically equal or similar; rather they are unequal operations. It uses conditions such as less than (<), greater than (>), less than or equal to (<=), greater than or equal to (>=), or not equal to (<>) to join the data of the tables.

Syntax:

SELECT
column1,
column2,
column3
FROM
table1
JOIN
table2 ON table1.column = table2.column
WHERE
non-equi condition;

 

Table: Customers

Non Equi Join SQL Customers Table

Table: Orders

Non Equi Join SQL Orders Table

SQL Query:

SELECT
O.OrderID,
C.CustomerName,
C.CreditLimit,
O.OrderTotal
FROM
Customers C
JOIN
Orders O ON C.CustomerID = O.CustomerID
WHERE
O.OrderTotal > C.CreditLimit;

 

Output:

Non Equi Join SQL Output

In the above example, we have used non-equi join between the Customers and Orders tables. We have used the condition for the join where the total amount of order is greater than the credit limit (OrderTotal > CreditLimit). The output includes columns from both tables where the order total is greater than the customer's credit limit.

Difference between Equi and Non-Equi Joins

Basis

Equi Join

Non-Equi Join

Symbols

They use the equality symbol (=) between them.

They use non-equality symbols >, <, >=, <=

Purpose

They are used for matching data where values in the table columns are equal.

They are used where the relationship between columns has inequalities or other conditions.

Output

Outputs include similar or common data points from the tables.

Outputs include the data points based on the given condition by the user

Application

They are used in situations where the user needs the exact matches from the tables.

Often used in scenarios where the user needs a range of output in the form of data from the table

Conclusion

In a nutshell, through this article, we were able to distinguish between Equi Joins and Non-Equi Joins. Through appropriate examples, we were able to point out key differences and demonstrate them in our table. Furthermore, the article dealt with the practical application of joins, providing an example of how these joins can be effectively utilized in different tables to find common grounds.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Tanish Mallik
I am Tanish, a final-year computer science student with extensive expertise in SQL. My background in the field of data science has honed my strong analytical skills, and I also have hands-on experience in developing data integration and optimization pipelines using Python and MSBI. I am eager to contribute innovative ideas and make an impact in the ever-evolving world of technology.