Friday, 3 April 2015

SQL Server IN Operator

IN : Determines whether a specified value matches any value in a subquery or a list.

Example: - With string
SELECT * FROM employees WHERE last_name IN ('Smith', 'Anderson', 'Johnson');
This SQL Server IN condition example would return all rows from the employees table where the last_name is either 'Smith', 'Anderson', or 'Johnson'.

Example: - With Numeric
SELECT * FROM employees WHERE employee_id IN (1, 2, 3, 4, 10);
This SQL Server IN condition example would return all employees where the employee_id is either 1, 2, 3, 4, or 10.

Example: - Using NOT operator with IN
SELECT * FROM employees WHERE first_name NOT IN ('Sarah', 'John', 'Dale');
This SQL Server IN condition example would return all rows from the employees table where the first_name is not 'Sarah', 'John', or 'Dale'.

The above IN example is equivalent to the following SELECT statement:
SELECT * FROM employees WHERE first_name <> 'Sarah' AND first_name <> 'John'
AND first_name <> 'Dale';


Example: - With Subquery
SELECT * FROM employee WHERE dept_no IN(SELECT dept_no FROM department WHERE location = 'Dallas')

This SQL Server IN condition example would return all rows from the employee table where the departments belongs to Dallas location.

Queries :

Write a Query to get employee details with firstname "Sunil", "Santosh", "Himaja" from employee table.

Ans: Select * from employee where firstname in ('Sunil','Santosh','Himaja').

No comments:

Post a Comment