Summary -
In this topic, we described about the Select In with detailed example.
SELECT IN statement used to fetch specific rows or values from existing table with multiple conditions. The conditions are specified with IN clause. The operation of SELECT IN is same as OR operation. SELECT IN is used to reduce the multiple OR operators in SELECT statement.
Syntax -
SELECT column1, column2, …, columnN
FROM table_name
[WHERE column_name [NOT] [IN (value1, value2, …, valueN)]];
- column1, column2, …, columnN - Specifies the columns of the table.
- column_name – Specifies the name of the column used in the condition.
- table_name - Specifies the name of the table.
Example -
Consider employee_details, department_details tables as follows -
employee_details -
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
001 | Employee1 | Director | 2019-07-11 | 45000.00 | 1000 | |
002 | Employee2 | Director | 2019-07-11 | 40000.00 | 2000 | |
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
004 | Employee4 | Manager | Employee2 | 2019-10-08 | 25000.00 | 2000 |
005 | Employee5 | Analyst | Employee3 | 2019-07-11 | 20000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 18000.00 | 1000 |
007 | Employee7 | Clerk | Employee3 | 2019-07-11 | 15000.00 | 1000 |
008 | Employee8 | Salesman | Employee4 | 2019-09-09 | 14000.00 | 2000 |
009 | Employee9 | Salesman | Employee4 | 2019-10-08 | 13000.00 | 2000 |
department_details -
dept_id | dept_name | dept_location |
---|---|---|
1000 | Office | Location1 |
2000 | Sales | Location2 |
Scenario – Fetch single column using IN clause.
Requirement – Fetch designation of employees who are having emp_id is 003 or 004 from employee_details table. The query was as follows -
SELECT designation FROM employee_details WHERE emp_id IN (003, 004);
By executing above query, we can get result like as follows –
designation |
---|
Manager |
Manager |
Scenario – Fetch Specific columns using IN clause.
Requirement – Fetch emp_id, emp_name, designation column values of employees having id 003 or 004 from employee_details table. The query was as follows –
SELECT emp_id, emp_name, designation
FROM employee_details
WHERE emp_id IN (003, 004);
By executing above query, we can get output like as follows –
emp_id | emp_name | designation |
---|---|---|
003 | Employee3 | Manager |
004 | Employee4 | Manager |
Scenario – All rows information using IN clause.
Requirement – Fetch all columns of employees having id 003 or 004 from employee_details table. The query was as follows –
SELECT * FROM employee_details
WHERE emp_id IN (003, 004);
By executing above query, we can get output like as follows –
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
004 | Employee4 | Manager | Employee2 | 2019-10-08 | 25000.00 | 2000 |
Scenario – Fetch all columns data using NOT IN clause.
Requirement - Fetch employee data who are not "director" or "clerk" from employee_details table. The query was as follows –
SELECT * FROM employee_details
WHERE designation NOT IN ('Director', 'Clerk');
By executing above query, we can get output like as follows –
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
004 | Employee4 | Manager | Employee2 | 2019-10-08 | 25000.00 | 2000 |
005 | Employee5 | Analyst | Employee3 | 2019-07-11 | 20000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 18000.00 | 1000 |
008 | Employee8 | Salesman | Employee4 | 2019-09-09 | 14000.00 | 2000 |
009 | Employee9 | Salesman | Employee4 | 2019-10-08 | 13000.00 | 2000 |
Scenario – Fetch all columns data using subquery in IN clause.
Requirement – Fetch all employees whose department is "Office" from employee_details table. The query was as follows –
SELECT * FROM employee_details
WHERE dept_id IN (
SELECT dept_id
FROM department_details
WHERE dept_name = "Office" );
By executing above query, we can get output like as follows -
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
001 | Employee1 | Director | 2019-07-11 | 45000.00 | 1000 | |
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
005 | Employee5 | Analyst | Employee3 | 2019-07-11 | 20000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 18000.00 | 1000 |
007 | Employee7 | Clerk | Employee3 | 2019-07-11 | 15000.00 | 1000 |