Introduction to MySQL NOT IN
MySQL NOT IN function is used as a subquery statement which guarantees that the given expression does not contain any of the values that are passed as arguments in the function. MySQL NOT IN clause is used to select all records or rows from the table that do not matches with the values passed in the NOT IN function. It basically eliminates all the records or rows that contain the values that gets matched with the values passed in the function and prints only those records other than the matched ones.
Syntax:
Expression NOT IN (value1, value2, value3……valuen)
Using MySQL NOT IN Keyword with WHERE Clause
When the where clause is used with the NOT IN keyword in the MySQL statement, it does not affect the rows that do not matches with the values that are provided to the NOT IN function as arguments.
Examples of MySQL NOT IN
To know the concept of mysql NOT IN function, we will create table named “students”.
Below is query for create statement:
Code:
create table students (roll_no int, student_name varchar (150), course varchar (150));
After creating table students, now it’s time to insert values into the table. Once the table is created we can now insert values into it.
Below is the query for insert statement:
Code:
Insert into students values (1,'ashish','java');
Insert into students values (2,'rahul','C++');
Insert into students values (3,'divya','Arch');
Select * from students;
Output:
Example #1
Below is the query of mysql IN function with where clause which selects only the rows or records whose values matches with the values passed in the mysql IN function as arguments.
Code:
Select * from students where roll_no IN (1, 2, 3);
Above query will select all records from table students where roll_no of students is either is 1 or 2 or 3.
Output:
Example #2
NOT IN keyword works simply opposite to IN clause. Since the IN clause will select all the records that contain or matches with the values that are passed in the IN function. The NOT IN will just eliminate all the records that matches with the values passed in the function and selects all the records that are not present in the values of the function.
Below is the query of mysql NOT IN with where clause:
Code:
Select * from students where roll_no NOT IN (1, 2);
Output:
The above query will select all rows from table students where roll_no of students is not either 1 or 2.
MySQL IN functions and NOT In function both works with multiple column strategy.
Example #3
To explain MySQL IN and NOT IN function we will take another table named employee and project.
Code:
create table employee(e_id int primary key,e_name varchar(150),Address varchar(150));
insert into employee values(1,'ashish','delhi');
insert into employee values(2,'aman','lucknow');
insert into employee values(3,'somya','banglore');
insert into employee values(4,'sonika','chennai');
insert into employee values(5,'ajay','hyderabad');
insert into employee values(6,'rahul','noida');
select * from employee;
Output:
Code:
create table project(e_id int not null,p_id int primary key,e_name varchar(150),Address varchar(150));
insert into project values(1,101,'java','chennai');
insert into project values(5,102,'c++','lucknow');
insert into project values(3,103,'payments','delhi');
insert into project values(4,104,'oauth','chennai');
insert into project values(4,105, 'wallet', 'gurgoan');
select * from project;
Output:
To find detail of employee whose address is either delhi or banglore or noida.
Code:
Select * from employee where address IN('delhi','banglore','noida');
Above query will select all records from table employee where address of employee is either delhi or banglore or Noida.
Output:
Code:
Select * from employee where address NOT IN('delhi','banglore','noida');
Above query will eliminate all records that have address of employee either as delhi or banglore or Noida and prints all records other than these 3 whose address is delhi or banglore or Noida.
Output:
Use of IN and NOT IN keyword in Subqueries
Find the names of employees who are working on a project.
Code:
Select e_name from employee where e_id IN(Select distinct e_id from project);
Above query will first select rows that have distinct employee id from project table and the select employee name from employee table that matches with the employee id of project table.
Output:
Code:
Select e_name from employee where e_id NOT IN (Select distinct e_id from project);
Above query will first select rows that do not have distinct employee id from project table and then select employee name from employee table that matches with the selected employee id of project table.
Output:
So, this will select only 2 employee name i.e. aman and Rahul as an output.
Conclusion
In this article, we saw about MySQL NOT IN keyword. We also saw about NOT IN clause in main query in select statement with where clause and we also saw about how to use it in subquery statement. We also saw about MySQL IN clause which helps us to understand the NOT IN query in much easier way. This article also contains simple examples along with the screenshots of the output which helps the reader to understand it properly.
Recommended Articles
This is a guide to MySQL NOT IN. Here we discuss the introduction, using MySQL NOT IN Keyword with WHERE clause, examples, use of IN and NOT IN keyword in subqueries. You may also have a look at the following articles to learn more –
12 Online Courses | 10 Hands-on Projects | 92+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses