SQL IN And NOT IN Operators

SQL AVG and SQL SUM

Understanding SQL IN & NOT IN Function

SQL (Structured Query Language) is a powerful language used for managing and manipulating data in relational databases.  Learn how to use SQL’s IN and NOT IN operators to efficiently query and filter data in your database.

Two commonly used operators in SQL are the IN and NOT IN operators. These operators allow you to specify multiple values or a set of values to search for or exclude in a query.

Understanding SQL IN Operator

The SQL IN operator is used to check if a value matches any value in a list or a subquery. It is commonly used in the WHERE clause of a SELECT statement to filter records based on a specific set of values. The IN operator simplifies the process of writing multiple OR conditions for each value.

SELECT column1, column2, ...
FROM table_name
WHERE column_name IN (value1, value2, ...);

The IN operator returns true if the specified column matches any value in the list; otherwise, it returns false. It is important to note that the values in the list must be of the same data type as the column being compared.

SQL IN OPERATOR

The given SQL query is used to retrieve the first names and last names of customers from the “Customers” table who are from either the “USA” or “UAE” countries. Let’s break down the query and explain its components:

  • SELECT first_name, last_name: This part of the query specifies the columns we want to select and retrieve from the table. In this case, we are interested in the “first_name” and “last_name” columns.
  • FROM Customers: Here, we specify the table from which we want to retrieve the data, which is the “Customers” table in this case.
  • WHERE country IN (‘USA’, ‘UAE’): This is the condition used to filter the records. The “WHERE” clause is followed by the condition that checks if the value in the “country” column is either ‘USA’ or ‘UAE’. Only the rows that meet this condition will be included in the result.
SQL IN Operator With Value
  • SELECT first_name, last_name: This part of the query specifies the columns we want to select and retrieve from the table. In this case, we are interested in the “first_name” and “last_name” columns.
  • FROM Customers: Here, we specify the table from which we want to retrieve the data, which is the “Customers” table in this case.
  • WHERE country = ‘INDIA’: This is the condition used to filter the records. The “WHERE” clause is followed by the condition that checks if the value in the “country” column is equal to ‘INDIA‘. Only the rows that meet this condition will be included in the result.

The result of this query will be a table with two columns: “first_name” and “last_name“. It will contain the data of customers whose country is ‘INDIA’.

SQL NOT IN Operator

The SQL NOT IN operator is used to exclude rows that match any value in a list or a subquery. It is the negation of the IN operator. By using the NOT IN operator, you can retrieve records that do not match any of the specified values.

SELECT column1, column2, ...
FROM table_name
WHERE column_name NOT IN (value1, value2, ...);
SQL NOT IN OPERTOR

The provided SQL query is used to retrieve the first name and last name of customers from the “Customers” table whose country is not ‘USA’ or ‘UAE’.

SELECT first_name, last_name
FROM Customers
WHERE country NOT IN ('USA', 'UAE');
  • SELECT first_name, last_name: This part of the query specifies the columns we want to select and retrieve from the table. In this case, we are interested in the “first_name” and “last_name” columns.

  • FROM Customers: Here, we specify the table from which we want to retrieve the data, which is the “Customers” table in this case.

  • WHERE country NOT IN (‘USA’, ‘UAE’): This is the condition used to filter the records. The “WHERE” clause is followed by the condition that checks if the value in the “country” column is not ‘USA’ or ‘UAE’. Only the rows that meet this condition will be included in the result.

    The result of this query will be a table with two columns: “first_name” and “last_name”. It will contain the data of customers whose country is neither ‘USA’ nor ‘UAE’.

Question 1. Are SQL IN and NOT IN operators case-sensitive?

No, the SQL IN and NOT IN operators are typically not case-sensitive. They compare values based on the collation settings of the database. In most databases, the default collation is case-insensitive, meaning that it treats uppercase and lowercase letters as the same. However, there might be certain database configurations where case-sensitivity is enabled. It’s important to consult the documentation or check the collation settings of your specific database to confirm the behavior of the IN and NOT IN operators with regards to case-sensitivity.

 

Question 2. How do I use the SQL IN operator with multiple values?

To use the SQL IN operator with multiple values, you need to provide a list of values within parentheses and separate them with commas.

For example: 

SELECT *
FROM Customers
WHERE Country IN (‘USA’, ‘Canada’, ‘Germany’);

This query will retrieve customers from the United States, Canada, and Germany.

Question 3. Can I use the SQL NOT IN operator with NULL values?

Yes, you can use the SQL NOT IN operator with NULL values. However, it’s important to note that the results might not be as expected. The NOT IN operator doesn’t work directly with NULL values since NULL represents an unknown value. To handle NULL values, you may need to use additional conditions or the IS NULL operator in combination with the NOT IN operator.

Question 4. What is the difference between SQL IN and EXISTS operators?

The SQL IN operator is used to check if a value matches any value in a list or subquery, whereas the EXISTS operator is used to check for the existence of rows returned by a subquery. In other words, the IN operator compares a value to a list of values, while the EXISTS operator checks for the existence of a condition or a set of rows.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription