MySQL
NOT IN Operator
The NOT IN operator in MySQL is part of the Data Query Language (DQL) and is used to filter query results by excluding rows that match any value in a specified list. This operator is helpful when you want to retrieve data that does not belong to a certain set of values. Similar to IN, it simplifies queries by eliminating the need for multiple OR conditions, making queries more concise and readable.
Here’s a detailed explanation of the NOT IN operator with examples and tips for new students:
Basic Syntax of
NOT IN:- The
NOT INoperator is used to exclude rows where the column value matches any value within the provided list. - Syntax:
SELECT * FROM employees WHERE department_id NOT IN (1, 2, 3);- This query will return all employees who do not belong to departments with IDs 1, 2, or 3.
- The
Using
NOT INwith Subqueries:- You can use
NOT INwith a subquery to exclude rows based on values from another table. - Example:
SELECT * FROM employees WHERE department_id NOT IN (SELECT department_id FROM departments WHERE branch_id = 1);- This query returns all employees who are not part of departments in branch 1.
- You can use
Using
NOT INwith Strings:- The
NOT INoperator works with both numbers and strings. - Example:
SELECT * FROM employees WHERE job_title NOT IN ('Manager', 'Engineer', 'Analyst');- This query returns all employees whose job title is not Manager, Engineer, or Analyst.
- The
Handling
NULLValues withNOT IN:- If any value in the list contains a
NULL,NOT INwill return no results becauseNULLis not comparable. To avoid this, make sure your list does not includeNULLvalues, or handleNULLseparately. - Example:
SELECT * FROM employees WHERE department_id NOT IN (1, 2, 3) OR department_id IS NULL;- This query returns employees who are not in departments 1, 2, or 3, or who have a
NULLdepartment_id.
- If any value in the list contains a
Combining
NOT INwith Other Conditions:- You can combine
NOT INwith other operators such asAND,OR, andBETWEENto create more complex queries. - Example:
SELECT * FROM employees WHERE department_id NOT IN (4, 5) AND salary > 50000;- This query returns employees who are not in departments 4 or 5 and have a salary greater than 50,000.
- You can combine
Performance Considerations:
- The performance of
NOT INcan be affected when used with a large number of values or complex subqueries. Indexing columns used inNOT INconditions can help improve query performance. - Example of an alternative using
JOIN:
SELECT e.* FROM employees e LEFT JOIN departments d ON e.department_id = d.department_id WHERE d.branch_id != 1 OR d.department_id IS NULL;- This query may offer better performance when excluding specific departments based on a condition.
- The performance of
Using
NOT INwith Multiple Columns:- MySQL does not allow the use of
NOT INdirectly with multiple columns, but you can achieve this by combining multiple conditions withAND. - Example:
SELECT * FROM employees WHERE (department_id, job_title) NOT IN ((1, 'Manager'), (2, 'Analyst'));- This excludes employees who belong to department 1 with the title of Manager, or department 2 with the title of Analyst.
- MySQL does not allow the use of
The NOT IN operator is a powerful tool for excluding specific values from your query results. It simplifies the exclusion of data sets and is useful in a variety of scenarios, such as filtering records that don't belong to a specific group or matching against a list of values from a subquery.
To gain complete access, login with gmail or outlook, no need of signup, click here
Test code
Here is an example SQL query employing the NOT IN operator with string values. The query retrieves all rows from the employees table where the designation column does not correspond to any of the specified values ('Sales Manager', 'Financial Analyst', 'CFO'). In other words, it retrieves all employee rows where the designation DOES NOT belong to ('Sales Manager', 'Financial Analyst', 'CFO').
SELECT *
FROM org_employee
WHERE designation NOT IN ('Sales Manager', 'Financial Analyst', 'CFO');Below is an example SQL query utilizing the NOT IN operator with numeric values. The query retrieves all rows from the clients table where the birth year column does not correspond to any of the specified values (1990, 1980). In other words, it retrieves all rows where the birth year DOES NOT match either 1990 or 1980.
SELECT *
FROM org_client
WHERE birth_year NOT IN (1990, 1980);Here's an example SQL query using the NOT IN operator with datetime values. The query retrieves all rows from the orders table where the birth year column does not match any of the specified values ('2023-12-09', '2023-12-10', '2023-12-18', '2023-12-25'). In other words, it fetches all rows where the shipped date DOES NOT match the dates provided in the list.
SELECT *
FROM act_order
WHERE desired_date NOT IN ('2023-12-09', '2023-12-10', '2023-12-18', '2023-12-25');
-- Oracle
-- WHERE desired_date NOT IN (to_date('09/12/23'), to_date('10/12/23'), to_date('18/12/23'), to_date('25/12/23'));Example 1:
Let's explore the procedure of extracting information from a designated table using the NOT IN SQL operator with string data. This query retrieves all rows from the employees table where the designation column does not corresponds to any of the specified values ('Sales Manager', 'Financial Analyst', 'CFO').
Example 1 - Raw data from employee table

Example 1 - Query
SELECT * FROM org_employee WHERE designation NOT IN ('Sales Manager', 'Financial Analyst', 'CFO');Example 1 - Query data mapping

In the provided image, the green color signifies the data that has been selected or satisfies the conditions specified in our query. Data points in red indicate information that does not meet the criteria set by the query.
Example 1 - Query Output

Example 2:
Let's explore the procedure for extracting information from a designated table using the NOT IN SQL operator on integer data. The query fetches all rows from the clients table where the birth year column do not match any of the specified values (1990, 1980).
Example 2 - Raw data from client table

Example 2 - Query
SELECT * FROM org_client WHERE birth_year NOT IN (1990, 1980);Example 2 - Query data mapping

In the provided image, the green color signifies the data that has been selected or satisfies the conditions specified in our query. Data points in red indicate information that does not meet the criteria set by the query.
Example 2 - Query Output

Example 3:
Let's explore the procedure for extracting information from a designated table using the NOT IN SQL operator on date time data. This query retrieves all rows from the orders table where the desired ship date column does not corresponds to any of the specified values ('2023-12-09', '2023-12-10', '2023-12-18', '2023-12-25').
Example 3 - Raw data from order table

Example 3 - Query
SELECT * FROM act_order WHERE desired_date NOT IN ('2023-12-09', '2023-12-10', '2023-12-18', '2023-12-25');Example 3 - Query data mapping

In the provided image, the green color signifies the data that has been selected or satisfies the conditions specified in our query. Data points in red indicate information that does not meet the criteria set by the query.
Example 3 - Query Output



Comments Not Found