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:

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:

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.
Similar Reads
IN vs EXISTS in SQL
SQL stands for Structured Query Language. SQL is used for retrieving useful information from a large set of data and it is used for storing the data in the Database, modifying, or manipulating the data from the database.In this article, we are going to discuss IN Operator and EXISTS Operator in SQL
5 min read
SQL | EXISTS
The SQL EXISTS condition is used to test whether a correlated subquery returns any results. If the subquery returns at least one row, the EXISTS condition evaluates to TRUE; otherwise, it evaluates to FALSE. The EXISTS operator can be used in various SQL statements like SELECT, UPDATE, INSERT, and D
3 min read
NOT IN vs NOT EXISTS in SQL
Structured Query Language (SQL) is a domain-specific language used in managing and manipulating data in a relational database. In SQL, we use these two operators i.e. NOT IN and NOT EXISTS to filter out and efficiently retrieve our data from a table. Both of these operators are negations of IN and E
5 min read
NOT IN vs NOT EXISTS in PL/SQL
PL/SQL is a Procedural Language/Structured Query Language. It allows developers to create robust, modular, and reusable code for implementing data manipulation, and transaction control in databases. Two crucial operators in PL/SQL are NOT IN and NOT EXISTS, which are often used to filter data based
5 min read
MySQL EXISTS Operator
The EXISTS operator in MySQL is a powerful boolean operator used to test the existence of any record in a subquery. It returns true if the subquery yields one or more records, enabling efficient data retrieval and manipulation, particularly in large datasets. The operator is often paired with subque
6 min read
Explicit vs Implicit MySQL Joins
MySQL joins combined rows from two or more tables based on a related column. MySQL, a popular relational database management system, offers two main approaches to perform joins: explicit and implicit. In this article, we will explore these two methodologies, understanding their syntax, use cases, an
7 min read
SELECT INTO in MySQL
MySQL is an open-source Relational Database Management System that stores data in a structured format using rows and columns. MySQL is a platform-independent language and can be compiled on any platform. MySQL is generally used for storing, manipulating, and retrieving data in RDBMS by using the SQL
5 min read
NOT IN vs NOT EXISTS in PostgreSQL
PostgreSQL is one of the most advanced general-purpose object-relational database management systems and is open-source. Being an open-source software, its source code is available under the PostgreSQL license, a liberal open-source license. Anyone with the right skills can use, modify, and distribu
4 min read
MySQL DISTINCT Clause
When working with databases, you often need to filter out duplicate records to get a clear and accurate result set. MySQL offers a straightforward solution for this with the DISTINCT clause. This clause helps you retrieve only unique rows from your query results, making it an essential tool for data
4 min read
MySQL SELF JOIN
Joins are very important for effective data retrieval and analysis. The 'JOIN' clause is used to combine data from two or more tables using the common column between them. In MySql, there are many types of joins like INNER JOIN, OUTER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, and SELF JOIN. In this ar
5 min read