In SQL, the IN operator is useful to search for the specified value that matches with any value in the set of multiple values or not.
Generally, we will use IN
operator with WHERE
clause to compare a column or variable value with multiple values.
Following is the syntax of IN operator in the SQL server.
If you observe the above SQL IN operator syntax, we are checking whether the column1 value matches any value in the set of multiple values or not, and it will return records that match with column1 values.
Now, we will check this with the example. For that first, create the “EmployeeDetails” table in the database by using the following SQL statements.
Once we execute the above script, our table “EmployeeDetails” will create, and the following is the result.
Now run the following examples to check IN
operator in the SQL server.
The following SQL query will return all the employees whose location in 'chennai', 'guntur', 'bangalore'
.
We will get the following result when we execute the above SQL IN operator query.
In the following SQL query, we will see how to use subquery with IN
operator in the SQL Server.
The above SQL query will return all the employees whose empname values match with the results returned by the SQL subquery. Following is the result of the SQL IN operator with a subquery example.
Now we will see how to use the NOT
keyword with IN
operator and check how it will return records. Generally, using the NOT
keyword with IN
operator will return all the records that do not exist in the list of values.
Generally, using IN
operator in SQL statements will return records whose column values match with a defined set of values. Suppose if we use NOT
keyword with IN
operator, it will return data whose column values do not match with the set of values.
The following SQL query will return all the records whose location is not in mentioned values.
Once we execute the above SQL IN operator with not keyword result, we will get the below result.