IN Clause in SQL Server

IN Clause or IN Command is used along with the SELECT command to limit to restrict the number of rows returned from the select query.

IN Clause is used in the WHERE condition of the SELECT Command.

IN is also used while comparing data using Sub queries.

This will return the records which satisfy the values which are right to the IN Clause or IN Command.

Generally values that should be compared are placed inside the brackets ().

IN Clause will return all the results which satisfy the values in side brackets.

Syntax:

IN ( val1,val2,...valN)

IN ( SELECT Col1 from <Table>)

Examples:

SELECT * FROM MYORDERS WHERE OrderID IN (100002,100003,100004,10000000)

Since 10000000 is not a valid OrderID, that is not fetched in the result set.










SELECT TOP 10 * FROM ORDERS WHERE OrderId IN ( SELECT OrderId from MYORDERS)

Above query will return the data from ORDERS table if the same OrderId exists in MYORDES table.






This entry was posted in . Bookmark the permalink.

Leave a reply