Open In App

MySQL IN vs. EXISTS

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In MySQL, We have two commonly used clauses in SQL queries that are EXISTS and IN used for querying data efficiently for high-performance applications. EXISTS and IN are the two most important clauses that are used to filter and extract data from the database. Although they both use subqueries in the same way, they serve different purposes and are used in different ways.

In this article, we’ll briefly explain EXISTS vs. IN in MySQL. We’ll look at their syntax, functions, and usage through practical examples.

EXISTS

In MySQL, EXISTS is used to test if any record that we are searching in a subquery exists or not. It is a boolean operator which returns true if the subquery returns one or more records.

Syntax of EXISTS in MySQL:

SELECT columnName(s) FROM tableName

WHERE EXISTS (SELECT columnName FROM tableName WHERE condition);

Example of EXISTS in MySQL

Suppose we have a database named EMPDATA and it has two tables called Employees and Departments. The Employees table contains columns EmployeeID, EmployeeName, and DepartmentID and Departments table contains DepartmentID and DepartmentName. We can create the database using the below queries:

CREATE DATABASE EMPDATA;
USE EMPDATA;

CREATE TABLE Departments ( DepartmentID int, DepartmentName varchar(255), PRIMARY KEY (DepartmentID) );

INSERT INTO Departments(DepartmentID,DepartmentName) VALUES
(001, 'IT'),
(002, 'HR'),
(003, 'JAVA');

CREATE TABLE Employees ( EmployeeID int,
EmployeeName varchar(255), DepartmentID int, PRIMARY KEY (EmployeeID),
FOREIGN KEY (DepartmentID),
REFERENCES Departments(DepartmentID) );

INSERT INTO Employees(EmployeeID ,EmployeeName,DepartmentID) VALUES
(1, 'RAM', 001),
(2, 'SHYAM', 002),
(5, 'ROHIT', 003);

If you want to find departments that have employees, you might use EXISTS the below example demonstrates the same.

SELECT DepartmentName FROM Departments 
WHERE EXISTS ( SELECT 1 FROM Employees WHERE Employees.DepartmentID = Departments.DepartmentID );

Output:

OutputExists

Explanation: The above query gives all department names that have at least one employee. EXISTS checks for the presence of any row in the Employees table that matches the condition.

IN

IN operator in MySQL is used to allow multiple values to be tested against a column. It’s often used with a subquery that returns a list of values.

Syntax of IN in MySQL:

SELECT column_name(s) FROM table_name

WHERE column_name IN (SELECT column_name FROM table_name WHERE condition);

Example of IN Clauses

SELECT EmployeeName FROM Employees 
WHERE DepartmentID IN ( SELECT DepartmentID FROM Departments WHERE DepartmentName = 'HR' OR DepartmentName = 'IT' );

Output:

OutputIN

Explanation: The above query gives a list of all the employees which belong to either the HR or IT departments. IN matches the DepartmentID against a list of department IDs returned by the subquery.

Difference Between EXISTS and IN in MySQL

The below table illustrates the differences among EXISTS and IN in MYSQL.

Feature

EXISTS

IN

Purpose

Used to check if a subquery returns any rows.

Used to check if a value matches any value in a list or subquery.

Usage

Ideal for cases where you want to verify the existence of records based on a condition.

Suitable for comparing a value against multiple values.

Syntax

WHERE EXISTS (SELECT column FROM table WHERE condition);

WHERE column IN (SELECT column FROM table WHERE condition);

Performance

Generally faster with subqueries that return a large number of rows, as it stops at the first found row.

Can be slower if the list or subquery returns a large number of rows, as it checks for all matches.

Return Type

Boolean (true/false) based on the existence of rows.

Boolean (true/false) based on value matching.

Typical Scenario

Checking for the presence of related data in another table.

Selecting data based on a list of values, often used with static lists or subqueries.

Conclusion

While both EXISTS and IN are used to filter data in SQL, they serve different purposes. EXISTS is ideal for situations where you need to check for the existence of rows that satisfy certain conditions. IN is more suited when you need to compare a column against multiple values. Knowing when to use each can significantly optimize your SQL queries.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads