Monday, 11 February 2013

SQL IN Operator Syntax and Example

SQL Server IN Operator
The IN Operator is used in a Where clause of the SQL Statement to retrieve the records that matches to a list of values.
The IN operator allows you to specify list of values in a WHERE clause for Matching Criteria .

SQL IN Syntax

SELECT Column_name(s) FROM Table_name
WHERE Column_name IN (Value1,Value2,...)

Example :

Suppose we have EMP Table as follows :


E_Id
First_Name
Last_Name
Dept_Id
Address
Salary
1001
Ravi
Kumar
1
Hyderabad
20000
1002
David
Smith
2
Bangalore
35000
1003
Victory
Venkatesh
1
Hyderabad
50000
1004
Tamatam
Reddy
3
Bangalore
25000
1005
William
Smith
2
Hyderabad
40000
1006
King
Fisher
6
Bangalore
30000

Criteria :
Now we want to select the Employees belongs to the Dept 1 and 2.using the below query :


Select *from EMP E WHERE E.Dept_Id IN(1,2)

Result :


E_Id
First_Name
Last_Name
Dept_Id
Address
Salary
1001
Ravi
Kumar
1
Hyderabad
20000
1002
David
Smith
2
Bangalore
35000
1003
Victory
Venkatesh
1
Hyderabad
50000
1005
William
Smith
2
Hyderabad
40000


Thanks,
TAMATAM

No comments:

Post a Comment

Follow Me by Email

ExcelKingdom-Popular Posts

ExcelKingdom-Random Posts