The NOT IN operator is used when you want to retrieve a column that has no entries in the table or referencing table.
This is important for keywords when we want to find records to management questions such as: Which customer has not done any transaction with us? Which product is not selling in the past few days?
For Example: If you want to find the names of customers who have not done any transactions A customer table will be containing records of all the customers and the transaction table keeps the records of any transaction between the store and the customer.
Customers SQL table contains the following:
Transaction table contains:
The NOT IN query would be like:
Select first_name, last_name, cust_id from customer where cust_id NOT IN ( Select cust_id from transactions)
The output will be:
Thus these are the 2 names of customers who have not done any transactions