SQL IN, SOME, ANY, ALL Operators

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

Overview

Operators in SQL have the same meaning as the operators in mathematics. They are keywords used in SQL statements to perform comparisons or logical operations. There are four types of operators in SQL - Arithmetic operator, Logical Operator, Comparison operator, and Bitwise Operators.

In this article, we will study some of the logical operators such as SOME, ALL, ANY, and IN. They are used to perform the comparison between a condition and a subquery. IN returns true if the operand is equal to one of the values returned by the subquery. Similarly, ALL & ANY return true if ALL or ANY of the values in the subquery satisfies the condition.

Let's study each one of them in detail.

SQL IN operator

The SQL IN operator allows testing a list of values in the where clause. The values are separated using a comma(,). It signifies multiple OR conditions in a SELECT, INSERT, UPDATE or DELETE statement in the SQL Query.

Syntax

The comma-separated values are compared and tested by the WHERE clause. If any of the values satisfy the conditions, the IN statement will be true else false.

OR

The subquery is a SELECT statement whose result will be compared and tested by the WHERE clause. If any of the conditions is true, the IN statement will be true else false.

Examples:

In this example, we will take a table named companies with the following set of data:

company_idcompany_namecitystate
1MicrosoftBangaloreKarnataka
2OracleGurgaonUttar Pradesh
3AmazonChennaiTamil Nadu
4AdobePuneMaharastra
5PayPalKolkataWest Bengal
6CodenationPatnaBihar

SQL Query with IN Operator with a String Value

The IN statement can have values of any data type like char, string or int. Let's have a look at the IN condition with multiple string values.

company_idcompany_namecitystate
1MicrosoftBangaloreKarnataka
3OracleGurgaonUttar Pradesh
8PayPalKolkataWest Bengal

The * in the SELECT statement shows all the fields from the companies table in the output. The WHERE statement checks the IN condition and returns all the rows whose company name is Microsoft, Oracle, or PayPal.

The equivalent SQL statement with OR condition looks like:

It is clear from the statement that the IN condition makes the code more readable, shorter, and less repetitive than OR conditions.

SQL Query with IN Operator with a Numeric Value

Have you ever wondered how to search a numeric value using the IN operator in a SELECT statement?

Note: The IN Operator uses values of all data types like string, char or interger.

Let's look at the IN condition with integer values using an example. In this example, we consider the table used in the previous examples.

The output of the following code is:

company_idcompany_namecitystate
1MicrosoftBangaloreKarnataka
3AmazonChennaiTamil Nadu
6CodenationPatnaBihar

The * in the SELECT statement shows all the fields from the companies table in the output. The WHERE statement checks the IN condition and returns all the rows whose company id is either 1, 3, or 6.

The equivalent SQL statement with OR condition looks like:

Let's create another table named Vacancy which has the following set of data:

company_idrole
1Finance
3App Developer
6Backend Developer
10FrontEnd Developer
15Analyst
18Java Developer

The IN condition is used with a SELECT statement returning a record from a single column. It cannot return values from more than one column in the SELECT statement.

Let's see it with an example.

In the above SQL Query, the subquery SELECT company_id from Vacancy WHERE role like '%Developer' returns four company_id, 3, 6, 10, and 18. So, now the previous SQL Query will look like SELECT * FROM companies WHERE company_id IN(3,6,10,18);

The above SQL Query will give the following result:

company_idcompany_namecitystate
3AmazonChennaiTamil Nadu
6CodenationPatnaBihar

Using IN condition with NOT Operator

Let's see how to use the IN condition with the NOT operator. We know that the NOT operator is used to reverse the condition. The same happens when we use the IN condition with the NOT operator to create the NOT IN condition. It returns a true value if the expression does not match any of the values in the list.

Let's see it with an example.

The above SQL query would return all rows from the company table where the company_name is not present in the given list that is Microsoft, Oracle, and Paypal. This method is more efficient at times when the list of values you want is larger than the list of values you don't want.

The result of the following SQL query would be:

company_idcompany_namecitystate
3AmazonChennaiTamil Nadu
4AdobePuneMaharastra
6CodenationPatnaBihar

Its equivalent SQL statement is written using AND conditions and not by OR condition as the IN condition is negated.

The SQL ALL Operator

To perform a comparison between a single value and a range of values in an SQL query, the ALL and ANY operators are used.

ALL operator is used to return all records of the SELECT statement. The result of the ALL operator is true only if the condition satisfies all values in the range. It returns a boolean value which is true when the subquery does not return any row.

Greater than (>) ALL means greater than the maximum value. For example, greater than ALL (10, 60, 100) means larger than 100 as the numbers greater than it will be greater than others. Less than (<) ALL means less than the minimum value. For example, Less than (<) ALL (20,56,13,5,74) means less than the minimum value that is 5 as the numbers less than the minimum are less than others. It would return false if no such data is possible.

The ALL operator is used with SELECT, WHERE and HAVING statements in SQL Queries.

ALL Syntax With SELECT

ALL Syntax With WHERE or HAVING

Only standard comparison operator(=, <>, !=, >, >=, <, or <=) are used here.

Examples:

Let's us consider the following "Products" table as the sample database:

ProductIDProductNameSupplierIDCategoryIDUnitPrice
1Tea1120 bags18
2Coffee1124 packets19
3Tropicana1212 bottles350
4Gulab Jamun2248 jars220
5Dosa Mix2236 packets135
6Bread3212 loaf25
7Organic Soaps37100 pkgs.30
8Tomato Sauce3212 bottles140
9Mishti Dahi4618 cups.17

The selection of values will be done from the following "OrderDetails" table:

OrderDetailIDOrderIDProductIDQuantity
1108112
2108210
3108315
410918
510944
610956
711035
8110418
911152
1011168
1111279
1211289
13110920
1411994

Example

This is an SQL query to get the name of all products from the Products table.

ProductName
Tea
Coffee
Tropicana
Gulab Jamun
Dosa Mix
Bread
Organic Soaps
Tomato Sauce
Mishti Dahi

The output contains the names of all the products as the condition is always true.

Example 2

In the above SQL Query, we get the product names of all the products from the Product table using the SELECT statement whose quantity is equal to 15.

The output of the above SQL query is:

ProductName
Tropicana

The subquery returns the ProductID of the products from the OrderDetails table whose quantity is equal to 15. The outer query returns the Product Name from the Product table whose ProductId is equal to those returned by the subquery.

Example 3

This SQL Query is used to obtain the OrderID of the products such that the maximum quantity among all products of that OrderId is greater than the average quantity of all OrderID. The output of the following SQL query is:

OrderID
108
110

The subquery returns the average of the quantity of all OrderID. The outer query returns the OrderID whose maximum quantity is greater than the maximum of the values returned by the subquery.

Using the NOT ALL Operator

Syntax

Example

The subquery in the SQL statement returns the ProductID from the OrderDetails table whose quantity is equal to 15. The above SQL Query returns the records of the products whose ProductID is not equal to any of the values returned by the subquery.

The result of the SQL Query is as following:

ProductIDProductNameSupplierIDCategoryIDUnitPrice
1Tea1120 bags18
2Coffee1124 packets19
4Gulab Jamun2248 jars220
5Dosa Mix2236 packets135
6Bread3212 loaves25
7Organic Soaps37100 pkg.30
8Tomato Sauce3212 bottles140
9Mishti Dahi4618 cups.17

The SQL ANY Operator

The result of the ANY operator is true if the expression or condition matches any of the values in the range. The result of ANY operator is always a boolean value.

Greater than (>) ANY means greater than at least one value or the minimum value. For example, greater than (>) ANY (10, 60, 100) means greater than the minimum value that is 10. Less than (<) ANY means less than at least one value or the maximum value. For example, Less than (<) ANY (20,56,13,5,74) means less than the maximum value which is 74. It would return false if no such data is possible.

Syntax

Only standard comparison operator(=, <>, !=, >, >=, <, or <=) are used here.

Examples:

Let's look at a SQL Query to find the Distinct CategoryID of the products which have any record in the OrderDetails table.

In the above SQL Query, the subquery returns all the ProductID from the OrderDetails table. The SELECT statement gives the distinct CategoryID for the products from the Products table whose ProductID matches the result of the subquery.

The output of the above SQL Query is the following:

CategoryID
1
2
7
6

Let's look at a SQL query to obtain any records from the OrderDetails table such that Quantity = 9.

In the above SQL Query, the subquery returns all the ProductID from the OrderDetails table whose quantity is equal to 9. The SELECT statement gives the names of the products from the Products table whose ProductID matches any of the results of the subquery.

The output of the above SQL Query is the following:

ProductName
Organic Soaps
Tomato Sauce

Let's look at a SQL query to obtain any records from the OrderDetails table such that Quantity is greater than 99.

In the above SQL Query, the subquery returns all the ProductID from the OrderDetails table whose quantity is greater than 99. The SELECT statement gives the names of the products from the Products table whose ProductID matches any of the results of the subquery. Since there are no such products available, the query returns a value and no record is shown in the output.

The output of the above SQL Query is the following:

ProductName

Using the NOT ANY Operator

Syntax

Example

The subquery in the SQL statement returns the ProductID from the OrderDetails table whose quantity is equal to 15. The above SQL Query returns the records of the products whose ProductID is not equal to all of the values returned by the subquery.

The result of the SQL Query is as following:

ProductIDProductNameSupplierIDCategoryIDUnitPrice
1Tea1120 bags18
2Coffee1124 packets19
4Gulab Jamun2248 jars220
5Dosa Mix2236 packets135
6Bread3212 loaves25
7Organic Soaps37100 pkgs.30
8Tomato Sauce3212 bottles140
9Mishti Dahi4618 cups.17

SQL Some Operator

The SOME operator in SQL is used to compare a value with a set of records of a column returned by a subquery. The result of a SQL Query with the SOME operator is true when the condition matches at least one value in the subquery preceded by a comparison operator.

Greater than (>) SOME means greater than at least one value or the minimum value. For example, greater than (>) SOME (10, 60, 100) means greater than the minimum value that is 10. Less than (<) SOME means less than at least one value or the maximum value. For example, Less than (<) SOME (20,56,13,5,74) means less than the maximum value which is 74. It would return false if no such data is possible.

Generally, the SOME operator is used with the WHERE clause to check whether the values returned by the subquery match with the given column values.

Syntax

The above SQL Query returns values only when the values in column1 match with the values returned by column1 in the subquery.

Examples:

Let's consider the table "Employees" with the following data:

emp_idemp_namedesignationsalarylocation
1NishaMTS75000Bangalore
2SumanBusiness Analyst65000Chennai
3AmishSDE50000Gurgaon
4DurgeshConsultant20000Chennai
5SarfarazFinance Analyst10000Bangalore

Let's have a look at a few examples of SQL queries using the SOME operator.

The subquery in the given SQL statement returns the salary of the employees that is greater than 25000.

The above SQL query will return the details of employees whose salary matches with the result of the subquery that is the salary greater than 25000. The result of the above SQL query is as given below.

emp_idemp_namedesignationsalarylocation
1NishaMTS75000Bangalore
2SumanBusiness Analyst65000Chennai
3AmishSDE50000Gurgaon

Let's see another example of SOME with the Greater than the operator.

This SQL Query returns the details of the employees whose salary is greater than at least one of the values or the minimum value returned by the subquery. The subquery in the above SQL Query returns the salary of the employees that are greater than 25000.

The above statement transforms to

The result of the above SQL query is as given below:

emp_idemp_namedesignationsalarylocation
1NishaMTS75000Bangalore
2SumanBusiness Analyst65000Chennai

The above SQL Query returns the salary that is greater than at least one of the values returned by the subquery. The subquery returns the salary of the employees having a location as Chennai. The salary of an employee with a Chennai location is 20000 and 65000. The above statement transforms to

Thus the final result returns the details of the employees having salary greater than 20000.

emp_idemp_namedesignationsalarylocation
1NishaMTS75000Bangalore
2SumanBusiness Analyst65000Chennai
3AmishSDE50000Gurgaon

Conclusion

In this article, we learned about SQL logical operators and their uses with real-life examples. We learned how to use them to compare a value with a range of values.

  • The IN operators compares a value with a range of comma-separated values and return the records from the table that matches.
  • The ANY operator compares a value with a range of values and returns true if the subquery contains at least one row.
  • The ALL operator is used to compares a value to all the values present in the list or the result of the inner subquery.
  • The SOME operator compares a value to every value from a range of values and returns true if the subquery contains at least one row.