SQL SERVER | IN Condition

IN condition is an alternative to multiple OR conditions in SELECT, INSERT, UPDATE, or DELETE statement. The IN operator allows multiple values to be tested against the expression and thus reduces the use of multiple OR conditions with each test value.

Syntax:

expression IN (value1, value2, .... value_n);

Where
1. expression : value/attribute to be tested.
2. value1, value2, .. value_n : The values to be tested against expression.



Example:

Creating Table GEEKS_6

Image

Inserting values into Table GEEKS_6:

Image

GEEKS_6 content:

Image



Selecting multiple values using ‘OR’. Multiple OR’s have to be used for checking the expression.
Image

The same query can be answered using ‘IN’ which reduces the number of times conditions have to be written and all test values are present at a single place.
IN Query:
Image

Example 2 : Using ‘IN’ with two tables

Creating Table GEEKS_7:

Image

Inserting values into Table GEEKS_7:

Image

GEEKS_7 content:

Image

Query: Find the common elements from two tables using ‘IN’

Image

Explanation:
The inner query will be executed first and all the values in the ‘name’ column of Table Geeks_7 will be selected. Then the outer query will start executing and it will use the inner query values to filter out matching values.



My Personal Notes arrow_drop_up

Image
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.