![]() |
In SQL Server, the NOT IN operator replaces a set of arguments with the <> or != operator that is combined with the AND operator. This operator can be used to make code more readable and easier to understand for SQL statements like SELECT, UPDATE, and DELETE. In most cases, it won’t affect performance. Syntax:
Let’s create a database to understand NOT IN Operator in SQL. QueryCREATE TABLE Emp( Output![]() Emp Table NOT IN Operators with NumberExample: Find the Name of the Employees who have not a Age equal to 23, 22, or 21. Query
Output![]() output NOT IN Operator With Duplicate ValuesIn SQL, the NOT IN operator is used to filter out a row in a database table if the specified column’s value isn’t in the specified list of values. The NOT IN operator doesn’t mean that duplicate values are ignored. Instead, it treats each of the values in the list as an independent value to check against the column’s values. Example SELECT * FROM Emp WHERE age NOT IN (21, 23, 25, 23, 36);
Output![]() output The above query will be equivalent to the below query: SELECT * FROM Emp WHERE age NOT IN (21, 23, 25, 26);
Output![]() output NOT IN Operator with StringsNOT IN operator is used to find out the data that is not present in the table. We can also use NOT IN to exclude the rows in your list. We should note that any kind of duplicate entry will be retained. Example: Find the Name of all the Employees who are not belong to not Australia or Austria QuerySELECT name FROM Emp WHERE country NOT IN ('Australia', 'Austria');
Output![]() output NOT IN Operator with SELECT StatementWe can also use the NOT IN operator with a subquery of the WHERE Clause, We can return a list of records from the main query that are not present in the subquery of WHERE clause. QuerySELECT * from Emp Where EMP_id NOT IN (SELECT Emp-Id from Account_details );
The above Query will return a list of records from the Emp table that are present in the the Account_details table. ConclusionIn summary, the NOT IN operator in SQL is useful for filtering out rows that match a particular set of values. The NOT IN operator can be used for a variety of filtering and querying operations, but it is important to understand how it behaves, particularly when dealing with NULLs, and to think about the performance implications when dealing with large data sets. FAQs on NOT-IN Operator in SQLQ.1: How does SQL NOT IN Operator work?Answer:
Q.2: What is the Syntax of the SQL NOT IN Operator?Answer: The syntax of the SQL-IN Operator is as follows:
Q.3: Is SQL NOT-IN Operator Used for only numeric data?Answer:
|
Reffered: https://www.geeksforgeeks.org
DBMS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |