In SQL Server the logical operators are used to perform conditional checks in SQL Queries. There are many different types of Logical operators like ANY, AND, LIKE, EXISTS, NOT, IN, and BETWEEN in SQL Server. These operators are very useful to filter and compare single or multiple data in SQL Queries. In this article let us discuss the 'ALL' operator available in SQL Server.
ALL Operator
The ALL operator compares a scalar expression with the values from a subquery result set. It returns a Boolean TRUE or FALSE as a result. It returns TRUE if the subquery result set values meet the condition. The data type of the single column used in the subquery 'Select' statement should be the same as the scalar expression.
Syntax:
scalar_expression { comparison_operator} ALL ( subquery )
- scalar_expression: This can be any valid expression.
- comparison_operator: From one of the comparison operators list - = , <> , != , > , >= , !> , < , <= , !<
- subquery: This is a 'SELECT' statement with a single column that returns a result set.
How Do the 'ALL' Operators Work?
- The ALL operator verifies the result set returned by the subquery satisfies the condition
- The ALL operator can be used when you want to check if at least one value satisfies the condition
- The Subquery after the ALL operator must return a single column and the column should be of same data type as the scalar expression column type.
Examples of How to Use ALL Operator
Below are examples to explain how the ALL operator works
Sample Data tables used in the examples
Products Table:
Products TableOrders Table:
Orders TableExample 1
The below example query explains of displaying products which are other than specific category:
Select * from Products where ProductID <> ALL (Select ProductID from Products where categoryId = 2)
Output:
Example 1 OutputExample 2
The below example query explains how to check for all Products Sold NOT within the price range of 20 and 40:
Select * from Products where Price !=ALL (Select Price from Products where Price Between 20 and 40)
Output:
Example 2 OutputExample 3
The below example query explains about, how to check for Specific Products Ordered based on OrderID:
Select * from Products where ProductID > ALL (Select ProductID from OrderDetails where OrderID = 10250 )
Output:
Example 3 OutputConclusion
To summarise, the 'ALL' operator in SQL Server can be used to compare a value from scalar expression to all values returned by a subquery using a comparison operator. It efficiently verifies whether the condition holds true for the entire subquery result set. The 'ALL' operator is particularly useful when ensuring that a specified condition is met across all values, providing flexibility in querying and filtering data in SQL Server.
Similar Reads
SQL Server ANY Operator In SQL Server there are many different types of Logical operators like ANY, AND, LIKE, EXISTS, NOT, IN, and BETWEEN. The logical operators are used to perform conditional checks in SQL Queries. These operators are very useful to filter and compare single or multiple data in SQL Queries. In this arti
3 min read
PL/SQL ALL, ANY Operator The ALL and ANY operators in PL/SQL are powerful tools used to compare a given value against a set of values returned by a subquery. These operators allow for more dynamic and flexible queries by evaluating conditions against multiple results. The ALL operator checks if a condition holds true for ev
4 min read
SQL OR Operator The SQL OR operator is a powerful tool used to filter records in a database by combining multiple conditions in the WHERE clause. When using OR, a record will be returned if any of the conditions connected by the operator are true. This allows for more flexibility in querying and is important when w
7 min read
Arithmetic Operators in SQL Server Arithmetic operators play a crucial role in performing mathematical calculations within SQL Server. These operators allow you to perform addition, subtraction, multiplication, division, and more on numeric data stored in your database tables. In this article, we'll explore the various arithmetic ope
4 min read
SQL - Logical Operators SQL Logical Operators are essential tools used to test the truth of conditions in SQL queries. They return boolean values such as TRUE, FALSE, or UNKNOWN, making them invaluable for filtering, retrieving, or manipulating data. These operators allow developers to build complex queries by combining, n
9 min read