SQL SELECT IN

What is the SQL IN Operator?

The SQL IN operator allows you to specify multiple values in a WHERE clause. It is used to filter the result set based on a list of values. Instead of using multiple OR conditions, you can use the IN operator for simplicity and readability.

The basic syntax of the IN operator is:

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

Example: Products Table

Consider the following Products table:

ProductID ProductName Category Price
1 Apple Fruit 1.50
2 Banana Fruit 0.75
3 Carrot Vegetable 0.90
4 Tomato Vegetable 0.60

Using the SQL IN Operator

If you want to select products that belong to the category 'Fruit' or 'Vegetable', you can use the following query with the IN operator:

SELECT ProductName, Category
FROM Products
WHERE Category IN ('Fruit', 'Vegetable');

This query will return all products that are categorized as either 'Fruit' or 'Vegetable'.

Example Output

The result of the above query would be:

ProductName Category
Apple Fruit
Banana Fruit
Carrot Vegetable
Tomato Vegetable

This result shows all the products that belong to the categories 'Fruit' or 'Vegetable'.