Table of contents
1.
Introduction
2.
EQUI JOIN in SQL
3.
Examples
3.1.
SQL
3.2.
SQL
4.
Equi Join Using Three Tables
4.1.
SQL
4.2.
SQL
5.
Difference between Natural Join, Equi Join and Inner Join:
6.
Frequently Asked Questions
6.1.
Can Equi Join be used with more than two tables?
6.2.
Is Equi Join case-sensitive?
6.3.
What happens if there are no matching rows in an Equi Join?
7.
Conclusion
Last Updated: Aug 2, 2024
Easy

Equi Join in SQL

Introduction

SQL JOIN is a crucial concept that allows you to combine data from multiple tables based on a related column. EQUI JOIN is a specific type of JOIN that uses the equality operator (=) to match the values in the related columns. 

Equi Join in SQL

In this article, we'll talk about EQUI JOIN  and its various components in detail, like its syntax, examples, and how it differs from other types of JOINs.

EQUI JOIN in SQL

EQUI JOIN is used when you want to combine rows from two or more tables based on a related column. It uses the equality operator (=) to compare the values in the specified columns. The basic syntax for an EQUI JOIN is as follows:

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


In this syntax, "table1" and "table2" are the names of the tables you want to join, and "column" is the name of the related column in each table. The ON clause specifies the condition for joining the tables.

Examples

Let's consider two tables, "employees" and "departments", with the following structures:

  • SQL

SQL

CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
first_name VARCHAR2(50),
last_name VARCHAR2(50),
department_id NUMBER
);

CREATE TABLE departments (
department_id NUMBER PRIMARY KEY,
department_name VARCHAR2(50)
);


Output

Output

To retrieve the employee names along with their corresponding department names, you can use an EQUI JOIN like this:

  • SQL

SQL


SELECT
employees.first_name || ' ' || employees.last_name AS name,
departments.department_name
FROM
employees
JOIN
departments
ON
employees.department_id = departments.department_id;


Output

Output

This query will return a result set with two columns: "name" from the "employees" table and "department_name" from the "departments" table. The JOIN condition `employees.department_id = departments.department_id` ensures that only the rows with matching department IDs are combined.

Equi Join Using Three Tables

EQUI JOIN can also be used to join more than two tables. Let's add a third table, "salaries", with the following structure:

  • SQL

SQL

CREATE TABLE salaries (
employee_id NUMBER PRIMARY KEY,
salary NUMBER
);


Output

Output


To retrieve the employee names, their department names, and their salaries, you can use an EQUI JOIN across all three tables:

  • SQL

SQL

SELECT 
employees.employee_id,
employees.first_name,
employees.last_name,
departments.department_name,
salaries.salary
FROM
employees
JOIN
departments ON employees.department_id = departments.department_id
JOIN
salaries ON employees.employee_id = salaries.employee_id;

 

Output

Output


In this example, the "employees" table is joined with the "departments" table based on the "department_id" column, and then the result is further joined with the "salaries" table based on the "employee_id" column. The final result set will contain the employee names, department names, and their corresponding salaries.

Difference between Natural Join, Equi Join and Inner Join:

Natural Join

Equi Join

Inner Join

Automatically matches columns with the same nameRequires explicit specification of the columns to joinCan use any condition in the ON clause
No need to specify the join conditionJoin condition is specified using the equality operator (=)Combines rows that satisfy the join condition
Removes duplicate columnsKeeps all columns from both tablesReturns only matching rows
May not always produce the desired resultProvides more control over the join conditionEquivalent to Equi Join with additional flexibility
Suitable when tables have columns with the same name and data typeSuitable when columns have different names or when a specific condition is requiredSuitable for most common join scenarios

 

Frequently Asked Questions

Can Equi Join be used with more than two tables?

Yes, Equi Join can be used to join multiple tables by specifying join conditions between each pair of tables.

Is Equi Join case-sensitive?

Equi Join is case-sensitive when comparing string values. If the column collation is case-insensitive, the join will be case-insensitive.

What happens if there are no matching rows in an Equi Join?

If there are no matching rows based on the join condition, those rows will be excluded from the result set.

Conclusion

In this article, we discussed Equi Join, a type of SQL JOIN that combines rows from multiple tables based on matching values in specified columns. We talked about the syntax of Equi Join & saw examples of joining two or more tables. We also discussed the differences between Natural Join, Equi Join & Inner Join. Equi Join provides a powerful way to retrieve related data from multiple tables, allowing for efficient querying & data analysis.

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass