PostgreSQL

PostgreSQL ANY Operator in the WHERE Clause

The PostgreSQL ANY operator is used with the WHERE clause when comparing different values. For instance, you can manually specify the list of values to compare against or specify what tables to use for the comparison. The output is the result of the specified condition. This post is about how to use the PostgreSQL ANY operator in the WHERE clause. We presented different examples to ensure that you understand the ANY operator.

PostgreSQL ANY Operator and How to Use It

The PostgreSQL operator follows the following simple syntax:

expression operator ANY (query);

The expression represents the comparison operator and relies on the specified operator to understand how to compare the values in the query/array. The following are the different ways in which you can utilize the operators to compare the values.

1. expression = ANY(query) – With this expression, the output returns true when a value in your query matches the value in the expression.

2. expression > ANY(query) – If the value in the expression is less than the query’s value, it returns true.

The given methods are a few ways in which you can use the operators. Other operators that you can use include <, <>, <=, etc. Let’s go through a few examples.

Example 1: Working with an Array

You can quickly create an array and compare if a given value satisfies the operator condition that you wish to use for comparison. For instance, if we have an array [5, 10, 23, 7, 15], we can compare its elements to see if any value equals to 10.

Let’s run the following command:

SELECT 10 = ANY(ARRAY[5, 10, 23, 7, 15]);

We get “t” as our output which represents “true”.

Let’s move on to see how we can use the ANY operator in the WHERE clause.

Example 2: Combining PostgreSQL with the WHERE Clause

When working with tables, you can quickly compare the values using the ANY operator and specify the query using the WHERE clause.

For this example, let’s work with the following two tables:

Taking the “customers” table, we can use the ANY operator in the WHERE clause to extract all the values where the “order_id” matches the query. In the following query, we want to find all values whose order_id > 3420. Only the results whose “order_id” equals our query is displayed in the output.

From the previous output, we can see that we retrieved three rows. This is a case where the query returns true, and the other values were left out.

It’s also possible to work with two tables. For our case, we have the “order_id” as the primary key in one table and the foreign key in another. Let’s say we want to get all the records in the “customers” table whose “order_id” value equals the “order_id” in the “orders” table where the “order_status” matches a given value.

Let’s run our command as follows:

SELECT * FROM customers WHERE order_id = ANY(SELECT order_id FROM orders WHERE order_status = 'Processing');

Only two records match our comparison operator which we get in the previous output.
We can also use the > ANY comparison to change how we want our values in the two tables to compare. Here, we retrieve all values in the “customers” table that is provided the “order_id” which is greater than our comparison query.

The output that we get retrieves only one row. Suppose we check the “orders” table where the “order_status = ‘Canceled’” and notice that its order_id = 3432. Our comparison query matches the values where the “order_id” is greater than 3432. Checking all the values in the “order_id” column, only one entry matches this comparison query, so we only get one result.

Conclusion

The PostgreSQL ANY operator is used with the WHERE clause to compare the values in a given table. You can use it with a single table or merge the tables to compare their values. You can tune the ANY operator in different ways and specify what operator to use to match the values. The example in this post guides you on implementing the PostgreSQL ANY operator in the WHERE clause. With more practice, you will quickly get comfortable with it.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.