• Home
  • SQL IN Operator

    The SQL IN operator is used to specify multiple values in a WHERE clause. It allows you to match a column's value against a list of predefined values or the result of a subquery.


    The basic syntax of the IN operator is as follows:

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

    In the above syntax:

    - `column_name` is the name of the column you want to compare.
    - `(value1, value2, ...)` is a comma-separated list of values or a subquery that returns a list of values.


    Here are a couple of examples to illustrate the usage of the IN operator:

    Example 1: Using explicit values
    Suppose you have a table named "Employees" with a column named "Department," and you want to retrieve the employees who belong to the departments 'Sales', 'Marketing', and 'Finance'. You can use the IN operator like this:

    SELECT *
    FROM Employees
    WHERE Department IN ('Sales', 'Marketing', 'Finance');

    This query will return all rows from the "Employees" table where the "Department" column value matches any of the specified values.


    Example 2: Using a subquery
    Let's assume you have a table named "Orders" with a column named "CustomerID," and you want to retrieve all orders placed by customers from a specific list of customer IDs. You can use the IN operator with a subquery like this:

    SELECT *
    FROM Orders
    WHERE CustomerID IN (SELECT CustomerID FROM Customers WHERE Country = 'USA');

    In this example, the subquery `(SELECT CustomerID FROM Customers WHERE Country = 'USA')` returns a list of customer IDs from the "Customers" table where the country is 'USA'. The main query then selects all rows from the "Orders" table where the "CustomerID" column value matches any of the customer IDs returned by the subquery.


    The IN operator provides a flexible way to compare a column's value against a list of values or the result of a subquery, allowing you to filter data based on multiple conditions.



    About the Author



    Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

    We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





     PreviousNext