In this post explains how to use the IN condition in SQL Server (Transact-SQL) with syntax and examples.
The SQL Server (Transact-SQL) IN condition is used to help reduce the need to use multiple OR conditions in a SELECT, INSERT, UPDATE, or DELETE statement.
The syntax for the IN condition in SQL Server (Transact-SQL) is:
OR
A value to test.
The values to test against expression.
This is a SELECT statement whose result set will be tested against expression. If any of these values matches expression, then the IN condition will evaluate to true.
Let's look at a SQL Server IN condition example using string values.
The following is a SQL Server SELECT statement that uses the IN condition to compare string values:
This SQL Server IN condition example would return all rows from the employees table where the last_name is either 'Smith', 'Mark', or 'Johnson'. Because the * is used in the SELECT, all fields from the employees table would appear in the result set.
The above IN example is equivalent to the following SELECT statement:
As you can see, using the SQL Server IN condition makes the statement easier to read and more efficient.
Next, let's look at a SQL Server IN condition example using numeric values.
For example:
This SQL Server IN condition example would return all employees where the employee_id is either 1, 2, 3, 4, or 10.
The above IN example is equivalent to the following SELECT statement:
Finally, let's look at an IN condition example using the SQL Server NOT operator.
For example:
This SQL Server IN condition example would return all rows from the employees table where the first_name is not 'Julie', 'John', or 'Dale' Sometimes, it is more efficient to list the values that you do not want, as opposed to the values that you do want.
The above IN example is equivalent to the following SELECT statement: