SQL IN Vs EXIST Operator
We'll talk about the differences between IN
and EXIST
in this session.
The key distinction is that IN returns the Boolean value TRUE or FALSE, whereas EXISTS returns a list of matched values.
SQL IN Operator
When the provided value matches any value in a collection of values or is returned by a subquery, the IN operator is used to obtain results.
Along with the WHERE
clause, this operator allows us to define numerous values.
It is also known as the shorthand for numerous OR conditions since it reduces the need of several OR
conditions in SELECT
, INSERT
, UPDATE
, and DELETE
queries.
The inner query is executed first in this operator, and the result is used by the outer query to display the output. It's important to remember that the inner query is only run once.
Example: If we want to retrieve all Country information who came from as a Asia and Europe , we may utilise the following statement:
ID | Code | Name | Continent |
---|---|---|---|
1 | MNG | Mongolia | Asia |
2 | BGR | Bulgaria | Europe |
3 | PNG | Papua New Guinea | Oceania |
4 | MYS | Malaysia | Asia |
Get who came from asia and europe to run below query
SELECT * FROM Country
WHERE Continent IN ('Asia', 'Europe');
Output: The result will be
id | code | name | continent |
---|---|---|---|
1 | MNG | Mongolia | Asia |
2 | BGR | Bulgaria | Europe |
4 | MYS | Malaysia | Asia |
SQL EXIST Operator
EXISTS is a Boolean operator that checks the result of a subquery and returns TRUE or FALSE.
It's used in conjunction with subquery to determine whether or not a row is returned by this subquery. If the subquery produces a single or several records, this operator returns TRUE.
When no records are returned, it returns a FALSE result.
The EXISTS operator automatically quits for further processing when it identifies the first true event.
This feature boosts the query's performance.
With SELECT
,UPDATE
, DELETE
, and INSERT
statements, we can use the EXISTS operator.
Every row in the outer query's table is returned by the sub-query.
Example: To further comprehend this operator, consider the following example. Let's say we have a table called Customer and Order that has the following information:
CID | Name | Gender | City |
---|---|---|---|
C-1 | Dharan | Female | Pune |
C-2 | Geetha | Female | Mysore |
The Following table is Order table
OrderNo | CID | Amount | OrderDate |
---|---|---|---|
1 | C-1 | 7000 | 2008/05/21 18:50:49 |
2 | C-1 | 1000 | 2001/02/25 20:28:51 |
3 | C-2 | 8000 | 2007/01/22 12:28:47 |
4 | C-1 | 7000 | 1996/10/19 17:28:42 |
If we wish to collect the CID of all customers who have placed at least one order, we may use the following statement:
Mysql,Sql-server,Sqlite,Ms access
SELECT CID
FROM Customer
WHERE EXISTS (SELECT CID FROM Order WHERE Order.CID = Customer.CID);
Output: The following output will be which customer placed order
cid |
---|
C-1 |
C-2 |
SQL Difference Between IN And EXIST Operator
Here , we will discussed and tabulated the contrasts between in & exist operator :-
IN | EXIST |
---|---|
The IN clause examines all records returned by the subquery field specified, and the IN-condition SQL Engine compares all IN Clause values. | The EXISTS clause returns true or false, and once a match is found, the SQL engine stops scanning. |
When the sub-query results are modest, however, the IN operator is faster than EXISTS. | When the subquery results are large, the EXISTS operator performs better. |
When using the IN operator, the list of matched values is always selected. | EXISTS returns a Boolean value of TRUE or FALSE. |
You can use the IN operator on both subqueries and values. | The EXISTS operator only works with subqueries. |
A straight set of values can be provided for comparison. Nothing in the IN clause compares to NULL. | The EXISTS clause can compare anything to NULLs, but it can't directly compare values, hence a sub-query is needed. |
The IN operator compares the columns provided before the IN keyword to the subquery result. | The EXISTS operator does not check for a match because it only confirms the existence of data in a subquery. |
It's used to cut down on the amount of OR conditions in a statement. | It's used to check if a subquery has any data. It determines whether or not the value will be returned, to put it another way. |
# It compares the parent query's values to the subquery's values (child query). | It does not compare the values of the subquery to those of the parent query. |
To verify against a single column, use the IN operator. | You can use the EXISTS Operator to check against several columns. |