The NOT IN operator in SQL is used when you want to retrieve columns that do not contain specific entries in a table or reference table. It is quite significant, especially when searching for keywords.
For instance, if you need to find answers to questions:
Which customers have not engaged in transactions with the store?
Which products have not been sold in the past few days?
Example of the NOT IN Operator in SQL
For example, if you wish to find the names of customers who have not conducted transactions with you.
Below is a customer table containing profiles of all customers and a transaction table containing information about transactions between the store and customers.
The customer table contains profiles of various customers:
The transaction table holds information about transactions:
The SQL NOT IN command is written as follows:
Select first_name, last_name, cust_id from customer where cust_id NOT IN ( Select cust_id from transactions)
Output:
These are the names of 2 customers who have not completed any transactions.
Above is some information, examples of the NOT IN operator in SQL. In the next article, Mytour will introduce you to other comparison operators in SQL, including LIKE, IN, IS NULL, BETWEEN ... AND operators. Additionally, readers can refer to some articles on the CREAT, UPDATE, DELETE ... commands in SQL on Mytour for more information. Don't forget to explore what SQL is and share your opinions and reviews with Mytour.
