SQL ALL Operator with Examples

The SQL ALL operator is a special type of comparison operator used to compare a single value with all the values returned by a subquery. It returns TRUE only when the specified comparison condition is satisfied for every value returned by that subquery.

In simple words, the specified condition must be true for every value in the result set. For example, suppose that a company wants to identify employees whose salaries are higher than the salaries of all employees working in a specific department.

Writing separate comparison conditions for every employee would be complicated, especially when the database contains hundreds or thousands of records. To solve this type of problem, we use the ALL operator in SQL.

Syntax of the SQL ALL Operator


The general syntax for the SQL ALL operator is:

SELECT column_name(s)
FROM table_name
WHERE expression comparison_operator ALL (
    SELECT column_name
    FROM another_table
    WHERE condition
);

In the above syntax:

  • SELECT: Retrieves the required columns from the database.
  • column_name(s): Lists the columns selected from the main table.
  • FROM: Identifies the main table from which records are retrieved.
  • WHERE: Filters records based on a specified condition.
  • expression: Specifies the value or column that will be compared against the subquery results.
  • comparison_operator: Defines the comparison operator, such as greater than, less than, greater than or equal to, less than or equal to, equal to, or not equal to.
  • ALL: Requires the comparison to evaluate to true for every value returned by the inner query or subquery.
  • Inner SELECT: Retrieves the comparison values from a subquery, which must return a single column.
  • another_table: Specifies the table queried inside the subquery.
  • WHERE condition: Optionally filters the rows returned by the subquery before comparison.

Example of the SQL ALL Operator


Let’s understand the SQL ALL operator through a complete example. We will start by creating a database table, inserting records, viewing the data in the table, and finally writing an SQL query using the ALL operator.

Step 1: Create an Employees Table

First, we need a table to store employee information. We will create a table named employees with four columns:

  • employee_id: Stores a unique identifier (such as an integer primary key) for each employee.
  • employee_name: Stores the employee’s name.
  • department: Stores the department name where the employee works.
  • salary: Stores the employee’s numeric salary for comparison.

SQL Query:

CREATE TABLE employees (
    employee_id INT PRIMARY KEY,
    employee_name VARCHAR(50),
    department VARCHAR(30),
    salary DECIMAL(10, 2)
);

In the above SQL query:

  • CREATE TABLE employees creates a new table named employees.
  • employee_id INT PRIMARY KEY creates an integer column that uniquely identifies each employee.
  • employee_name VARCHAR(50) stores employee names up to 50 characters.
  • department VARCHAR(30) stores department names.
  • salary DECIMAL(10, 2) stores salaries with up to two decimal places.

Expected Result:

Table employees created successfully.

Step 2: Insert Records into the Employees Table

Now that we have created the table, let’s insert some employee records. Run the following SQL query to insert records into the Employees table.

INSERT INTO employees
(employee_id, employee_name, department, salary)
VALUES
(101, 'Rahul', 'IT', 60000),
(102, 'Priya', 'IT', 70000),
(103, 'Amit', 'HR', 80000),
(104, 'Neha', 'HR', 90000),
(105, 'Rohit', 'Sales', 65000),
(106, 'Sneha', 'Sales', 75000);

In the above SQL query:

  • INSERT INTO employees specifies the table where we want to insert records.
  • The column list identifies the columns receiving values.
  • VALUES specifies the records to insert into the Employees table.
  • Each set of parentheses represents one employee.

Expected Result:

6 rows inserted successfully.

Step 3: Display All Records from the Employees Table

Before using the SQL ALL operator, let’s check whether the records were inserted successfully into the Employees table.

SELECT * FROM employees;

Expected Output:

employee_idemployee_namedepartmentsalary
101RahulIT60000.00
102PriyaIT70000.00
103AmitHR80000.00
104NehaHR90000.00
105RohitSales65000.00
106SnehaSales75000.00

Now our table is ready to use with the ALL operator.

Displaying all records from the employees table, which is used to compare the value using SQL ALL operator.

Step 4: Understand the Problem Before Using ALL Operator

Suppose we want to find employees whose salaries are greater than the salaries of all employees working in the IT department. Let’s identify the IT employees first.

EmployeeDepartmentSalary
RahulIT60000
PriyaIT70000

We need to find all employees whose salaries are greater than both 60000 and 70000 from the table. For example:

  • Amit earns 80000, which is greater than both IT salaries.
  • Neha earns 90000, which is greater than both IT salaries.
  • Sneha earns 75000, which is also greater than both IT salaries.
  • Rahul, Priya, and Rohit do not satisfy the condition.

We can solve this problem using the SQL ALL operator.

Step 5: Execute the Subquery First

Before writing the complete ALL query, let’s execute its inner query separately.

SELECT salary
FROM employees
WHERE department = 'IT';

Expected Output:

salary
60000.00
70000.00

In this SQL query:

  • SELECT salary retrieves the salary column from the Employees table.
  • FROM employees specifies the employees table.
  • WHERE department = ‘IT’ filters employees belonging to the IT department.
  • The subquery returns two salary values, such as 60000.00 and 70000.00. These are the values against which the ALL operator will compare employee salaries.

Step 6: Use the SQL ALL Operator

Now let’s write the complete SQL query to find employees whose salaries are greater than all salaries in the IT department. Execute the following SQL query.

SELECT employee_name, department, salary
FROM employees
WHERE salary > ALL (
    SELECT salary
    FROM employees
    WHERE department = 'IT'
);

Expected Output:

employee_namedepartmentsalary
AmitHR80000.00
NehaHR90000.00
SnehaSales75000.00

Step-by-Step Explanation of the Complete Query

Step 1: Retrieve employee information

SELECT employee_name, department, salary
FROM employees

This query retrieves the employee name, department, and salary from the employees table.

Step 2: Apply the WHERE condition

WHERE salary > ALL

This query tells SQL to select employees whose salaries are greater than every value returned by the subquery.

Step 3: Execute the inner query

SELECT salary
FROM employees
WHERE department = 'IT'

This retrieves the salaries of employees working in the IT department. The result is:

60000
70000

Step 4: Compare the salaries

The ALL operator checks whether each employee’s salary is greater than both values. For example, consider Amit in the table:

80000 > 60000 = TRUE
80000 > 70000 = TRUE

Both conditions are TRUE, so Amit is included in the result. Now we consider Rohit:

65000 > 60000 = TRUE
65000 > 70000 = FALSE

One condition is FALSE, so Rohit is excluded from the result set.

Step 5: Display matching employees

SQL returns Amit, Neha, and Sneha because all three employees have more salaries than every employee in the IT department.

Here is the comparison table:

EmployeeSalaryComparison resultSelected?
Rahul60000FALSE, FALSENo
Priya70000TRUE, FALSENo
Amit80000TRUE, TRUEYes
Neha90000TRUE, TRUEYes
Rohit65000TRUE, FALSENo
Sneha75000TRUE, TRUEYes

This is the most important concept to understand about the SQL ALL operator.

All Comparision Operator Supported by Database Systems


SQLite does not support the standard ALL comparison operator in the same way those database systems do. Therefore, this query produces a syntax error.

SELECT employee_name, department, salary
FROM employees
WHERE salary > ALL (
    SELECT salary
    FROM employees
    WHERE department = 'IT'
);

However, other database systems, such as MySQL, PostgreSQL, SQL Server, and Oracle support the SQL ALL comparison operator.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.