SQL Server – SELECT Statement with WHERE IN and WHERE NOT IN clause

SELECT Statement with WHERE IN and WHERE NOT IN clause

We often come across situations when have multiple value that need to be compared to a single column. In such situation WHERE IN clause is very useful

The IN search condition searches for an exact match from the given list of values.

Example:

USE AdventureWorks

SELECT * FROM Sales.CreditCard

WHERE CardType

IN (‘Vista’,’SuperiorCard’,’ColonialVoice’)

The above query will return all the records having CardType from the list given in the parenthesis.

NOT IN clause is just the opposite. It will return all the rows except that matches the given list.

Example:

USE AdventureWorks

SELECT * FROM Sales.CreditCard

WHERE CardType

NOT IN (‘Vista’,’SuperiorCard’,’ColonialVoice’)

The above query will return all the records not matching CardType from the list given in the parenthesis.

See also:

DML SELECT Statement

SELECT Statement with WHERE clause

SELECT Statement with WHERE BETWEEN clause

Good Luck!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: