SQL - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

SQL IN Condition

SQL IN Condition

shape Description

SQL IN Condition is used when trying to match a value from a discrete value set. It will prevent to use multiple or condition in Where Clause statement.

shape Conceptual figure

shape Syntax

Select <column_name> from <table_name> where column_name IN (value1,value2,...);
Table name => The accurate table in the database. column name => The operations that can be performed in the column.

shape Examples

When tried to find the customer details whose branch is in Downtown or in Brighton, then write like below: [c]sql>SELECT cust_id, cust_name FROM cust_det WHERE branch in ('Downtown','Brighton');[/c]

Subquery

shape Description

Here IN condition prevents from multiple OR executions. It is very useful in sub queries when it is not known if the result of the sub query will be a single value or multiple like below.

shape Examples

The SQL IN condition is used in subquery. [c]sql>SELECT account_number FROM accounts WHERE cust_id IN (SELECT cust_id FROM cust_det WHERE branch ='Downtown' OR branch ='Brighton'); [/c] In the above example the sub query will return multiple values and the outer query will search the customer account numbers who have a account in branch 'Downtown' or 'Brighton'.

Summary

shape Key Points

SQL IN Condition - Is used when we are trying to match a value from a discrete value set.