Creating Demo Tables

Before directly moving to the NOT IN and NOT EXISTS, we have to create two tables in our database. We going to create two tables, with the names ‘w3wiki’ and ‘courses’.

Table 1: ‘w3wiki’

Creating a fable name w3wiki:

CREATE TABLE w3wiki(
id int PRIMARY KEY,
name varchar(100),
total_score int
);

Inserting data to table w3wiki:

INSERT INTO w3wiki(id,name,total_score)
VALUES(01,'Vishu',100);
INSERT INTO w3wiki(id,name,total_score)
VALUES(02,'Neeraj',95);
INSERT INTO w3wiki(id,name,total_score)
VALUES(03,'Aayush',85);
INSERT INTO w3wiki(id,name,total_score)
VALUES(04,'Vivek',70);

Output :

Table – w3wiki

Table 2: ‘courses’

Creating a fable name courses:

CREATE TABLE courses(
id int,
course varchar(100),
);

Inserting data to table courses:

INSERT INTO courses(id, course)
VALUES(01,'Python');
INSERT INTO courses(id, course)
VALUES(02,'Python');
INSERT INTO courses(id, course)
VALUES(01,'Java');
INSERT INTO courses(id, course)
VALUES(04, 'JavaScript');
INSERT INTO courses(id, course)
VALUES(02, 'C language');

Output:

Table – courses

NOT IN vs NOT EXISTS in SQL

Structured Query Language (SQL) is a domain-specific language used in managing and manipulating data in a relational database. In SQL, we use these two operators i.e. NOT IN and NOT EXISTS to filter out and efficiently retrieve our data from a table.

Both of these operators are negations of IN and EXISTS operators respectively. In short, they perform exact opposite types of operations compared to IN and EXISTS operators.

In this article, we are going to compare NOT IN and NOT EXISTS operators. We are going to discuss their syntax, working, and differences between them. We will also see examples related to them.

Similar Reads

Creating Demo Tables

Before directly moving to the NOT IN and NOT EXISTS, we have to create two tables in our database. We going to create two tables, with the names ‘geeksforgeeks’ and ‘courses’....

NOT IN Operator

NOT IN operator is a negation operator that results in the rows which do not contain the specified values. We can clearly say that it performs tasks exact opposite of the IN operator. It can be used to filter out the data resulting from the subquery. Suppose we specified a list that contains id’s 101, 102, and 103. NOT IN operator returns all the rows that do not have these id’s present in them....

NOT EXISTS Operator

In SQL, NOT EXISTS Operator return boolean values. It is used for testing that if any records exists or not. If the any subquery do not results any values or return 0 then only the parent query will execute. In simple words, if the child query returns 0 then only the parent query will execute....

Differences between NOT IN and NOT EXISTS, Operators

In this, we are going to list the difference between these two operators i.e. NOT IN and NOT EXISTS....

Conclusion

Although NOT IN and NOT EXISTS seem to be pretty much similar in terms of working, there is a vast difference in them. NOT IN operator is preferred for finite list of values whereas, NOT EXISTS operator takes less time and becomes more efficient when it comes to large datasets. Both of these operators are the negation of IN and EXISTS operators respectively. We have covered syntax and examples of both NOT IN and NOT EXISTS operators. We have also discussed how they are different from each other....