IN vs EXISTS in SQL - GeeksforGeeks

15 May, 2021
SQL stands for Structured Query Language. It is used for storing data in databases, modifying or manipulating data in databases and retrieving data from databases. We can say that SQL manages data in a relational database management system (RDBMS).
The instructions that are used to communicate with a database in order to perform tasks, functions, and queries with data are called SQL Statements. SQL is not case-sensitive. Generally, SQL keywords are written in uppercase.
To match an expression against a list of values, SQL provides IN Operator. So we do not need to use multiple OR conditions in SELECT, UPDATE, etc.
We can list values directly or we want to provide a query result to the IN operator.
Syntax : SELECT columnName(s) FROM tableName WHERE columnNamex IN (value1, value2, ...);
Here ColumnNameX is matched with every value (value1, value2,...) in the list. If the match occurs, then IN evaluates to be TRUE and if the mismatch occurs, then IN evaluates to be FALSE.
Suppose we have a table named “Customers” as :
Customer Table
Now we want to see the details of the customers whose id can be 1 or 2 or 5 or  6 or 7, we can use IN operator & give a set of values to check upon.
If the argument sub-query is non-empty, exists construct returns the value true, otherwise false. To check whether a row is returned through this sub-query or not, it is used. True is returned if one or more rows are returned by executing the sub-query, otherwise False when no rows are returned.
Syntax :
SELECT columnName(s) FROM tableName1 WHERE EXISTS
(subquery);

subquery is of the form : SELECT columnName FROM tableName2 WHERE condition
Here the sub-query is executed first. 
The sub-query is a SELECT statement.  The EXISTS condition will be met & it will return TRUE if the subquery returns at least one record in its result set, else,  the EXISTS condition will not be met and it will return FALSE.
Note: The sub-query is returning for EVERY row in the outer query’s table.
Now, let up suppose we have another table: “Accounts” as :
Accounts Table
Now to see all the customers who’s any kind of account exists, we can make use of exists keyword as :
We here check: match for every CUSTOMERID in the customer table with the CUSTOMERID in the accounts table. 
If CUSTOMER.CUSTOMERID = ACCOUNTS.CUSTOMERID evaluates to be true in the sub-query, one row is returned & thus the sub-query evaluates to be true & our outer query (Select * from Customer) get executed for that particular customer.
Note: To do the same thing, i.e., to see all the customers who’s any kind of account exists, we can make use of IN keyword also :
The key differences between IN & EXISTS Operator are :
Picked
SQL-Query
SQL
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Update Multiple Columns in Single Update Statement in SQL?
SQL | Subquery
How to Create a Table With Multiple Foreign Keys in SQL?
What is Temporary Table in SQL?
SQL Query to Find the Name of a Person Whose Name Starts with Specific Letter
SQL using Python
SQL Query to Convert VARCHAR to INT
How to Write a SQL Query For a Specific Date Range and Date Time?
How to Select Data Between Two Dates and Times in SQL Server?
SQL Query to Compare Two Dates