Oracle Tutorial

  • Home
  • Start Here
  • Basics
  • Advanced
    • Oracle View
    • Oracle Index
    • Oracle Synonym
    • Oracle Sequence
    • Oracle Administration
  • PL/SQL
  • Functions
    • Aggregate Functions
    • Analytic Functions
    • Comparison Functions
    • Date Functions
    • String Functions
  • API
    • Python Oracle
Home / Oracle Basics / Oracle ANY

Oracle ANY

Summary: in this tutorial, you will learn how to use the Oracle ANY operator to compare a value with a list or subquery.

Introduction to the Oracle ANY operator

The Oracle ANY operator is used to compare a value to a list of values or result set returned by a subquery. The following illustrates the syntax of the ANY operator when it is used with a list or subquery:

operator ANY ( v1, v2, v3) operator ANY ( subquery)

In this syntax:

  • The ANY operator must be preceded by a comparison operator such as =, !=, >, >=,<, <=.
  • The list or subquery must be surrounded by the parentheses.

When you use the ANY operator to compare a value to a list, Oracle expands the initial condition to all elements of the list and uses the OR operator to combine them as shown below:

SELECT * FROM table_name WHERE c > ANY ( v1, v2, v3 );

Oracle performs a transformation of the above query to the following:

SELECT * FROM table_name WHERE c > v1 OR c > v2 OR c > v3;

If you use the ANY operator to compare a value with result set returned by a subquery, Oracle uses the EXISTS operator to transform the query to an equivalent one without using the ANY operator. For example, the following statement returns all products whose list price is greater than any list price of products in the category 1:

SELECT product_name, list_price FROM products WHERE list_price > ANY( SELECT list_price FROM products WHERE category_id = 1 ) ORDER BY product_name;

Because the query uses a subquery with the ANY operator, Oracle performed a single transformation as shown below:

SELECT product_name, list_price FROM products p1 WHERE EXISTS( SELECT list_price FROM products p2 WHERE category_id = 1 AND p1.list_price > p2.list_price ) ORDER BY product_name;

Note that if the subquery returns no rows, the following condition evaluates to false:

operator ANY (subquery)

Hence, the whole query returns no rows:

SELECT * FROM table_name WHERE col operator ANY(subquery);

In Oracle, the SOME and ANY operators behave exactly the same therefore they are completely interchangeable.

Oracle ANY operator examples

If the subquery returns rows or list has value, the following statements apply to the ANY operator:

1) col = ANY ( list )

The expression evaluates to true if the col matches one or more values in the list, for example:

SELECT product_name, list_price FROM products WHERE list_price = ANY( 2200, 2259.99, 2269.99 ) AND category_id = 1;
Oracle ANY Operator with equal

2) col != ANY(list)

The expression evaluates to true if the col does not match one or more values in the list.

SELECT product_name, list_price FROM products WHERE list_price != ANY( 2200, 2259.99, 2269.99 ) AND category_id = 1 ORDER BY list_price DESC;
Oracle ANY Operator with not equal

3) col > ANY (list)

The expression evaluates to true if the col is greater than the smallest value in the list.

SELECT product_name, list_price FROM products WHERE list_price > ANY( 2200, 2259.99, 2269.99 ) AND category_id = 1 ORDER BY list_price DESC;
Oracle ANY Operator with greater than

4) col >= ANY (list)

The expression evaluates to true if the col is greater than or equal to the smallest value in the list.

SELECT product_name, list_price FROM products WHERE list_price >= ANY( 2200, 2259.99, 2269.99 ) AND category_id = 1 ORDER BY list_price DESC;
Oracle ANY Operator with greater than or equal to

5) col < ANY (list)

The expression evaluates to true if the col is smaller than the highest value in the list.

SELECT product_name, list_price FROM products WHERE list_price < ANY( 2200, 2259.99, 2269.99 ) AND category_id = 1 ORDER BY list_price DESC;

Oracle ANY Operator with less than

6) col <= ANY (list)

The expression evaluates to true if the col is smaller than or equal to the highest value in the list.

SELECT product_name, list_price FROM products WHERE list_price <= ANY( 2200, 2259.99, 2269.99 ) AND category_id = 1 ORDER BY list_price DESC;
Oracle ANY Operator with less than or equal to

In this tutorial, you have learned how to use the Oracle ANY operator to compare a value with a list or subquery.

  • Was this tutorial helpful?
  • YesNo
Previous Oracle NOT EXISTS
Next Oracle ALL

Getting Started

  • What Is Oracle Database
  • Install Oracle Database Server
  • Download Oracle Sample Database
  • Create Oracle Sample Database
  • Connect To Oracle Database Server

Oracle Data Manipulation

  • SELECT
  • Oracle DUAL Table
  • ORDER BY
  • SELECT DISTINCT
  • WHERE
  • Table & Column Aliases
  • AND
  • OR
  • FETCH
  • BETWEEN
  • IN
  • LIKE
  • IS NULL
  • Joins
  • INNER JOIN
  • LEFT JOIN
  • RIGHT JOIN
  • FULL OUTER JOIN
  • CROSS JOIN
  • Self Join
  • GROUP BY
  • HAVING
  • UNION
  • INTERSECT
  • MINUS
  • GROUPING SETS
  • CUBE
  • ROLLUP
  • PIVOT
  • UNPIVOT
  • INSERT
  • INSERT INTO SELECT
  • INSERT ALL
  • UPDATE
  • DELETE
  • MERGE
  • Subquery
  • Correlated Subquery
  • EXISTS
  • NOT EXISTS
  • ANY
  • ALL

Oracle Data Types

  • Oracle Data Types
  • NUMBER
  • FLOAT
  • BINARY_FLOAT
  • CHAR
  • NCHAR
  • VARCHAR2
  • NVARCHAR2
  • DATE
  • INTERVAL
  • TIMESTAMP
  • TIMESTAMP WITH TIME ZONE

Oracle Data Definition

  • CREATE TABLE
  • Identity Column
  • ALTER TABLE
  • ALTER TABLE ADD Column
  • ALTER TABLE MODIFY Column
  • Drop Columns
  • DROP TABLE
  • TRUNCATE TABLE
  • RENAME Table
  • Oracle Virtual Column

Oracle Constraints

  • PRIMARY KEY
  • FOREIGN KEY
  • UNIQUE
  • CHECK
  • NOT NULL

Oracle Views

  • CREATE VIEW
  • DROP VIEW
  • Updatable Views
  • Inline Views
  • WITH CHECK OPTION

About Oracle Tutorial

OracleTututorial.com website provides Developers and Database Administrators with the updated Oracle tutorials, scripts, and tips.

Search

Recent Tutorials

  • Oracle Implicit Statement Results
  • Calling PL/SQL Stored Functions in Python
  • Calling PL/SQL Procedures in Python
  • Managing Transaction in Python
  • Deleting Data From Oracle Database in Python

Site Links

  • Oracle Books
  • About
  • Contact
  • Privacy Policy
  • Terms of Use

Copyright © 2021 Oracle Tutorial. All Rights Reserved.