IN Operator in SQL
To match an expression against a list of values, SQL provides the 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,vaule2,….);
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:
Query
CREATE table Customer(
CUSTOMERID INT PRIMARY KEY,
FIRST_NAME VARCHAR(50),
LAST_NAME VARCHAR(50),
AGE INT);
INSERT INTO Customer(CUSTOMERID,FIRST_NAME,LAST_NAME,AGE) VALUES
(1,'Mohit','Kumar',21),
(2,'Praful','Singh',22),
(3,'Ritik','Kumar',25),
(4,'Vishnu','Yadav',26);
Output
Now we want to see the details of the customers whose FIRST_NAME can be ‘Mohit’ or ‘Praful’ or ‘Ritik’, we can use IN operator & give a set of values to check upon.
Query
SELECT * FROM Customer where FIRST_NAME IN('Mohit', 'Praful', 'Ritik');
Output
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 as well as the difference between these two operators in detail.