Friday, 20 March 2026

ANY keyword in SQL server

In SQL Server, ANY is a comparison keyword used with a subquery. It compares a value against multiple values returned by a subquery, and returns TRUE if the condition matches at least one value. Think of ANY as OR logic across the subquery results.

Syntex

expression operator ANY (subquery)

ANY must always be used with a comparison operator like =, >, <, >=.

Suppose We have an employee table, and we want to find employees whose salary is greater than ANY salary in the IT department. Here we can use Any to find out to list of employees.

See the Demo

Creating a table an inserting few record

CREATE TABLE Employees

(

    EmpID   INT,

    EmpName VARCHAR(50),

    Dept    VARCHAR(20),

    Salary  INT

);

 INSERT INTO Employees VALUES

(1,'Amit','IT',50000),

(2,'Bagesh','IT',60000),

(3,'Neha','HR',55000),

(4,'Pooja','HR',70000),

(5,'Rahul','Sales',45000);

 

Below is script to get the list

SELECT *

FROM Employees

WHERE Salary > any

(

    SELECT Salary

    FROM Employees

    WHERE Dept = 'IT'

);

See the result

How SQL evaluates this is very important. It becomes: Salary > 50,000 OR Salary > 60,000. If the condition is true for at least one value, the row is returned. Employees with salary greater than 50,000 satisfy the condition. That’s why this query returns HR employees with 55,000 and 70,000 salaries.

Condition true for at least one value.

ANY compares a value with a set of values returned by a subquery and returns true if the condition is satisfied by at least one value.

 

No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts