SQL -Structured Query Language - Sql Clause - In Clause Tutorial
The IN Statement is used mainly in a subquery or taking value from the array.
Suppose if we want data from the other table using a subquery.
Syntax-
The basic syntax of the IN statement is.–
SELECT * FROM table_name1
WHERE
column_name
IN
( SELECT column_name from table_name2);
Statement After IN is a subquery
For example-
Suppose there are two tables, 1st table employee contains employee information.
And 2nd table departments contains department information like location,department_name,etc.
If we want to retrieve data from employees tables whose department_id are in a subquery. The subquery will retrieve department_id whose location_id not equal to 1700.
Then syntax is
SELECT * FROM employees
WHERE
department_id
IN
(SELECT department_id FROM departments
WHERE
location_id!=1700);
IN statement can also be used to retrieve data from the array
SELECT * FROM employees
WHERE
department_id
IN
(40,50,60);