SQL SELECT IN
The SQL SELECT IN statement is used to filter the result set based on multiple values in a specific column. It allows you to specify a list of values to match against a column, providing a more concise and readable way to check for multiple conditions.
This statement is helpful when you want to retrieve records that match any of a set of values. It's equivalent to using multiple OR
conditions, but it's much more efficient and easier to read.
Syntax
The basic syntax for SELECT IN is:
SELECT column_name
FROM table_name
WHERE column_name IN (value1, value2, ...);
Example
In this example, we will retrieve products ordered by a specific set of customers from the Orders table.
Code Example
-- Select orders from customers 101 and 105
SELECT Product FROM Orders
WHERE CustomerID IN (101, 105);
Output
Explanation
- The query retrieves products ordered by customers with CustomerID 101 and 105.
- The IN
operator allows us to check multiple values in a cleaner way than using multiple OR conditions.