×

SQL IN vs SQL EXISTS

SQL IN vs SQL EXISTS

This article discusses in detail about the IN and the EXISTS operators in SQL. It is a common question between developers that what is the difference between these two operators. To know the difference, let’s first discuss about each of these operators in detail.

IN Operator

The IN Operator is used to get results when a specified value matches a value in a set of values or is returned by an inner query. This operator can also be used with the WHERE clause to specify more than one value. The IN operator is often referred to as the acronym for the OR operator because it reduces the use of multiple OR conditions in SELECT, INSERT, UPDATE or DELETE queries.

In this operator, the subquery is solved first and that result is then used to solve the outer query.

The following is the general syntax of the IN operator:

SELECT column_name
 FROM table_name
 WHERE column_name IN (value1, value2, …); 

Example:

Let’s consider the following Customer table.

Cust_idCust_nameCityOccupation
1HarryKolkataBusiness
2RonMumbaiDoctor
3AlbusDelhiEngineer
4DobbyPuneScientist
5SnapeBangaloreStudent

Query:

SELECT *
 FROM Customer    
 WHERE Occupation IN ('Doctor', 'Scientist', 'Engineer'); 

The above query will return all the customers who are either Doctor or Scientist or Engineer.

Output:

Cust_idCust_nameCityOccupation
2RonMumbaiDoctor
3AlbusDelhiEngineer
4DobbyPuneScientist

EXISTS Operator

The EXISTS operator, unlike the IN operator, returns a Boolean value. This operator checks the result of the inner query and returns a Boolean value i.e., either true or false. If the inner query returns a single or multiple record, then the operator returns true else it returns false when no records are found.

This operator is also efficient as it stops further processing as soon as the first true event is detected.

The following is the syntax of the EXISTS operator:

SELECT column_names    
 FROM table_name    
 WHERE NOT EXISTS (    
 SELECT column_names     
 FROM table_name     
 WHERE condition    
 ); 

Example:

Let’s consider the following Customer table.

Cust_idNameOccupationAge
101HarryEngineer32
102RonDeveloper30
103DobbyLeader28
104AlbusScientist45
105SnapeCarpenter26
106GingerActor25
107NULLNULLNULL

Let’s consider another table Orders.

Order_idCust_idProduct_nameOrder_date
1101Laptop2021-01-10
2103Desktop2021-02-12
3106IPhone2021-02-15
4104Mobile2021-03-05
5102TV2021-03-20

Query:

SELECT Name, Occupation
 FROM Customer    
 WHERE EXISTS (
 SELECT *
 FROM Orders     
 WHERE Customer.Cust_id = Orders.Cust_id
 ); 

The above query will return the name and occupation of all the customers who have placed at least one order.

Output:

NameOccupation
HarryEngineer
RonDeveloper
DobbyLeader
AlbusScientist
GingerActor

Difference table between IN and EXISTS operator

The key differences between the IN operator and the EXISTS operator are listed below in a tabular manner:

IN OperatorEXISTS Operator
It minimizes the usage of the OR conditions.It checks the existence of a record in the inner query.
It compares the values of the inner query with the value of the outer query.It does not compare the values between the inner query and the sub query.
It checks all the values inside the block of the IN clause.It stops any further execution as soon as the first true condition is met.
It can return TRUE, FALSE or NULL.It returns either TRUE or FALSE.
It can be used to check NULL values.It cannot be used to check NULL values.
It is used with both subqueries and values.It is used only with subqueries.
Execution is faster when the result of the inner query is less.Execution is faster even when the result of the inner query is large. It is more efficient than the IN operator.
Syntax: SELECT column_names FROM table_name WHERE column_name IN (subquery);Syntax: SELECT column_namesFROM table_nameWHERE [NOT] EXISTS (subquery);

Conclusion:

In this topic, a comparison has been made between the IN operator and the EXISTS operators of SQL. Both the operators do the same operation but their internal working is different. They have different logical working. Any one of them can be selected according to the requirement. But, if the set of data is large, it is always recommended to go for the EXISTS operator.


Related Topics

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 minutes read.

SQL SELECT WHERE Clause

In this SQL section, we will help you understand the concept of the SQL SELECT WHERE clause with a few examples. The WHERE clause in SELECT query displays those records as...

5 minutes read.

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

SQL SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

SQL Truncate

This command deletes all records from table. Truncate is a DDL command. Syntax: TRUNCATE table table_name; Example: Truncate table teacher; ORDER BY The ORDER BY clause arranges the table or column in ascending...

5 minutes read.

How to use LIKE in SQL

In this SQL article, we will learn and understand how to use LIKE to the columns in the SQL tables. What is Like? Like is an operator in the SQL. It is...

7 minutes read.

How to Add Foreign Key in SQL?

How to Add Foreign Key in SQL Foreign key is an attribute or a set of attributes that references to primary key of same table or another table (relation). Foreign key creation along...

4 minutes read.

SQL Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

How to remove duplicates in SQL

Introduction There are some specific rules that needs to be followed while creating the database objects. To improve the performance of a database, a primary key, clustered and non-clustered indexes, and...

9 minutes read.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.