T-SQL 101: #25 Checking lists of values in SQL Server T-SQL by using the IN operator

The T-SQL IN operator allows you to choose from a list of matching values.

In the top case shown below:

I've said where StateName is one of QLD, NSW, or VIC.

Now see that's exactly the same as if I had written the option shown at the bottom where I said:

where StateName equals QLD or StateName equals NSW or StateName equals VIC.

The two options work exactly the same and mean the same thing, so having an IN operator is logically equivalent to having a whole lot of predicates joined by OR operators.

The IN operator just leads to simpler code that's also easier to read.

Learning T-SQL

It's worth your while becoming proficient in SQL. If you'd like to learn a lot about T-SQL in a hurry, our Writing T-SQL Queries for SQL Server course is online, on-demand, and low cost.

Leave a Reply

Your email address will not be published.