Monday, 21 May 2012

SQL IN Operator


The IN operator helps to reduce the need of  multiple OR conditions.


The syntax for the IN operators is:


SELECT columns
FROM tables
WHERE column1 in (value1, value2, .... value_n);


This SQL statement will return the records where column1 is value1, value2..., or value_n. 
The IN operators can be used in any valid SQL statement - select, insert, update, or delete.


The number of values in the parenthesis can be one or more, with each values separated by comma. 
Values can be numerical or characters. If there is only one value inside the parenthesis, this commend is equivalent to,


IN Operator Example


The "Customer" table:


CUS_Id LastName        FirstName     Address        City
1         Kumar             vinoth        Trivensn10 surandai
2         lakshmi           Vijaya        daospd23         surandai
3         latha               Hema                Reodj20        kadayam


Now we want to select the persons with a last name equal to "Kumar" or "latha" from the table above.


We use the following SELECT statement:


SELECT * FROM Customer
WHERE LastName IN ('Kumar','latha')


The result-set will look like this:


CUS_Id LastName        FirstName     Address        City
1       Kumar               vinoth        Trivensn10  surandai
3         latha                 hema              Reodj20          kadayam

No comments:

Post a Comment