SQL IN Operator

In SQL, the IN operator is used to specify a list of values to be matched against a column. The syntax for the IN operator is as follows:

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
Sour‮w:ec‬ww.theitroad.com

For example, suppose we have a table named "employees" with a column named "department". We can use the IN operator to find all employees who work in the "Sales" or "Marketing" department, like this:

SELECT *
FROM employees
WHERE department IN ('Sales', 'Marketing');

This query will return all rows from the "employees" table where the "department" column contains the value "Sales" or "Marketing".

We can also use a subquery with the IN operator to match values from another table. For example, suppose we have a table named "departments" with a column named "department_name", and we want to find all employees who work in any department with a budget greater than $100,000. We can use the following query:

SELECT *
FROM employees
WHERE department IN (SELECT department_name FROM departments WHERE budget > 100000);

This query will first execute the subquery to retrieve all department names with a budget greater than $100,000, and then match those department names against the "department" column of the "employees" table to return all employees who work in those departments.