Day 14 – SQL IN Operator
In SQL, the IN
operator is used to specify multiple values in a WHERE
clause. The IN
operator allows you to specify a list of values, and the WHERE
clause will return any rows where the specified column matches any of the values in the list.
The basic syntax for using the IN
operator is as follows:
SELECT column_name FROM table_name WHERE column_name IN (value1, value2, value3, ...);
Here, column_name
is the name of the column you want to search, table_name
is the name of the table containing the column, and value1
, value2
, value3
, etc. are the values you want to search for.
For example, suppose you have a table called employees
with columns employee_id
, first_name
, last_name
, and department
. If you want to find all employees who work in the “Sales” or “Marketing” departments, you would use the following SQL statement:
SELECT * FROM employees WHERE department IN ('Sales', 'Marketing');
This statement will return all rows from the employees
table where the department
column matches either “Sales” or “Marketing”.
The IN
operator can also be used with a subquery to search for values in another table. For example, suppose you have a table called sales
with columns product_id
, product_name
, and price
, and you want to find all products with a price higher than the average price. You could use the following SQL statement:
SELECT product_name, price FROM sales WHERE price > (SELECT AVG(price) FROM sales);
This statement will return all rows from the sales
table where the price
column is higher than the average price of all products in the sales
table.