×

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

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

4 minutes read.

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

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 Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause...

5 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 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 Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 minutes read.

SQL INSERT Table

In this tutorial, we will help you to understand and learn how to add records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

Introduction to SQL

SQL Introduction SQL is Standard Query Language. This language is used to communicate or interact with database. In other words, SQL is used to access and manage data or information...

2 minutes read.

SQL FULL JOIN

In this section, we will help you understand the concept of the SQL FULL Join clause with a few examples. The SQL FULL Join query is executed to display the integrated...

3 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 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.