MySQL Tutorial

MySQL Installation and Configuration

MySQL Database Operations

Database Design

MySQL Data Types

MySQL Storage Engines

MySQL Basic Operations of Tables

MySQL Constraints

MySQL Operators

MySQL Function

MySQL Manipulate Table Data

MySQL View

MySQL Indexes

MySQL Stored Procedure

MySQL Trigger

MySQL Transactions

MySQL Character Set

MySQL User Management

MySQL Database Backup and Recovery

MySQL Log

MySQL Performance Optimization

MySQL IN and NOT IN

The IN operator allows you to specify multiple values in a WHERE clause. It is a shorthand for multiple OR conditions.

The NOT IN operator is used to exclude the rows with certain values.

Here are the basic syntaxes for IN and NOT IN:

  • IN
column_name IN (value1, value2, ...);
  • NOT IN
column_name NOT IN (value1, value2, ...);

Examples:

  • IN
SELECT * FROM employees WHERE employee_id IN (1, 2, 3);

This query will select all rows from the employees table where employee_id is either 1, 2, or 3.

  • NOT IN
SELECT * FROM employees WHERE employee_id NOT IN (1, 2, 3);

This query will select all rows from the employees table where employee_id is not 1, 2, or 3.

The IN and NOT IN operators can also be used with subqueries. Here are examples:

  • IN with subquery
SELECT * FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE location_id = 1000);

This query will select all employees who work in departments located at location_id 1000.

  • NOT IN with subquery
SELECT * FROM employees WHERE department_id NOT IN (SELECT department_id FROM departments WHERE location_id = 1000);

This query will select all employees who do not work in departments located at location_id 1000.

Remember that the IN operator is logically equivalent to multiple OR conditions and the NOT IN operator is logically equivalent to multiple AND conditions. They are used to make the SQL statement cleaner and easier to read.

  1. MySQL IN operator example:

    • The IN operator in MySQL is used to specify multiple values in a WHERE clause.
    SELECT * FROM table_name WHERE column1 IN (value1, value2, value3);
    
  2. How to use IN in MySQL queries:

    • Use the IN operator to filter rows where a column's value matches any value in a specified list.
    SELECT * FROM products WHERE category_id IN (1, 3, 5);
    
  3. Filtering data with IN operator in MySQL:

    • The IN operator simplifies queries where you want to match a column against multiple values.
    SELECT * FROM users WHERE user_type IN ('admin', 'editor', 'moderator');
    
  4. Using NOT IN for exclusion in MySQL:

    • The NOT IN operator is used to exclude rows where a column's value does not match any value in a specified list.
    SELECT * FROM employees WHERE department_id NOT IN (2, 4, 6);
    
  5. Combining multiple values with IN in MySQL:

    • The IN operator can be used to combine multiple values in a concise way.
    SELECT * FROM orders WHERE status IN ('pending', 'processing') AND customer_id = 1001;
    
  6. IN vs OR in MySQL queries:

    • While both IN and OR can be used for similar purposes, IN is often more readable and can be more efficient in certain situations.
    -- Using IN
    SELECT * FROM products WHERE category_id IN (1, 3, 5);
    
    -- Equivalent using OR
    SELECT * FROM products WHERE category_id = 1 OR category_id = 3 OR category_id = 5;
    
  7. Handling NULL values with IN and NOT IN in MySQL:

    • The IN and NOT IN operators can be used to filter rows where a column is or is not NULL.
    -- Rows with non-null values
    SELECT * FROM users WHERE role IN ('admin', 'editor') AND email IS NOT NULL;
    
    -- Rows with null values
    SELECT * FROM users WHERE role NOT IN ('admin', 'editor') OR email IS NULL;
    
  8. Examples of using IN and NOT IN in MySQL queries:

    • Applying IN and NOT IN in various scenarios for effective data retrieval.
    -- Example 1: Filtering by multiple IDs
    SELECT * FROM products WHERE product_id IN (101, 102, 105);
    
    -- Example 2: Excluding specific categories
    SELECT * FROM articles WHERE category_id NOT IN (4, 7);
    
    -- Example 3: Combining with other conditions
    SELECT * FROM customers WHERE country IN ('USA', 'Canada') AND last_purchase_date > '2023-01-01';