Difference between IN and ANY operators in PostgreSQL
IN
Today, we will take a closer look at how the and ANY
operators work
in PostgreSQL . However, remember that both are standard SQL operators, which means we can use them on multiple management platforms.
By looking at them one by one and through some examples, we will be able to learn a lot about them.
IN
Operators
in PostgreSQL
IN
You'll find and
under Subquery Expressions listed in the PostgreSQL documentation ANY
.
grammar:
expression IN (subquery)
The subquery above is a query written to return results, in our case, exactly one column. Why? Because different columns contain their own data sets.
We want to find the expression in this query, so we use the operator . The operator will return TRUE IN
if the expression matches any row under the columns returned by the subquery ; otherwise, it returns FALSE.IN
If the expression and the subquery tend to be NULL, IN
the operator returns NULL.
Now let's assume that we don't have a single expression, but a set of expressions, meaning we have a whole line where we need to see if it exists. In this case, IN
the operator also has an evaluation syntax:
row_constructor IN (subquery)
row_constructor
Constructing a valid row from a set of values, such as in the following query:
SELECT ROW('cat', 'adam');
Creates a row with two values under separate unsorted columns. Thus, it helps in building rows from any number of values provided to its constructor.
Now let's go back to our original statement.
The subquery in this statement returns a set of rows containing multiple columns instead of a set of rows containing a single column. So far, the entire row can be compared to the returned row.
In this case, it is better to use this syntax to evaluate. If the entire line matches, it returns TRUE, otherwise it returns FALSE.
Likewise, you can apply NOT IN
the operator and it will return the exact opposite result.
Now, let's look at an example. First, we'll create a table with two columns cat
: ID
and NAME
.
CREATE TABLE cat (
ID int PRIMARY KEY,
NAME TEXT
)
We will proceed by inserting two values into our table.
INSERT INTO cat VALUES(1, 'Adam'), (2, 'Jake')
Let's see if our table has a name Adam
.
Select 'Adam' in (Select Name from cat)
Output:
Now, what about searching an entire row? Let's continue using our row constructor.
Now, we want to find Adam
the but this time with an ID and compare it to NAME
the column as well as the entire row returned from the table.cat
select ROW(1, 'Adam') in (SELECT * from cat)
It will also return a TABLE with TRUE values.
Now that you understand IN
the main uses of the operator, let's move on to understanding ANY
how works.
ANY
Operators
in PostgreSQL
ANY
The operator IN
is the same as the operator. ANY
It also returns TRUE if the row matches, otherwise it returns FALSE.
It uses ANY/SOME
the keyword, which is similar to IN
the operator. To use ANY
the operator, you can continue to write the following for our table above:
Select 'Adam' = any (Select Name from cat)
So, we have to use an operator here, and ANY
the operator returns TRUE because Adam
exists. For the row constructor, use the following.
Select ROW(1, 'Adam' ) = any (Select * from cat)
One of the differences we can list here is ANY/SOME
the use of another operator in the syntax. If you want to see if something is equal to the returned rows and want it to return TRUE, use the query syntax given above.
But if, for example, you had to search for a value in a table John
and still return TRUE, you could go ahead and write the following:
Select 'John' != any (Select Name from cat)
It will return TRUE because there is no cat name John
.
Beyond that, there are various other differences worth mentioning.
IN
Difference between and ANY
operators
in PostgreSQL
There are two syntax variations in PostgreSQL IN
, and ANY
. We read the first syntax using a single value to search for, but we saw a slight difference in the second variation of the row constructor.
Variant differences for row search
In our table are the values Adam
and Jake
. We want WHERE
to use these to search in a statement.
We can write it like this.
SELECT * from cat
WHERE (ID, NAME) = any(ARRAY[(1, 'Adam'), (2, 'Jake')])
This will help in finding cat
all the values in the table which has the custom value that we are using for searching.
Assume we have the following configuration:
-----
(1, 'Adam')
(1, 'John')
(2, 'Marta')
(2, 'Mack')
(2, 'Kitty')
(3, 'Mars')
-----
cat
Check all the rows in the above table to see if there are some similar rows in
our table and then return those rows. We'd better ANY
use arrays in the operator, which will help us operate and query all these values.
Of course, this is primarily used as a function and cannot be JOIN
called by , making ANY
more efficient.
ANY
Various modifications to operators
You can also use operators like LIKE
, , WHERE
or ANY
with . So you can say:
SELECT 'foo' LIKE ANY('{FOO,bar,%oo%}');
It Adam
compares with any of the words given in the array. It is not LIKE FOO,
, but LIKE %oo%
, so the query returns TRUE.
in conclusion
Today, you learned the difference between the IN
and ANY
operators.
Although the two are similar, ANY
the operator IN
is bigger and better than . Due to its applications, it can save space and, to a lesser extent, time in major coding programs.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Article URL:https://www.jiyik.com/en/xwzj/sjk_10580.html
Related Articles
Terminate the PostgreSQL connection
Publish Date:2025/04/11 Views:199 Category:PostgreSQL
-
In this article, we will learn how to terminate a PostgreSQL session. Any open connections are run by background processes or tasks, PSQL which may no longer exist despite exiting the user interface or command line tool. Use ps -ef or grep
Single query to rename and change column type in PostgreSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
This article describes how to rename a column and change its type in PostgreSQL using only a single query. Renaming and changing column types in MySQL In MySQL , if you want to change the column type and rename it, you can use a simple stat
Joining columns using Select in PostgreSQL
Publish Date:2025/04/11 Views:176 Category:PostgreSQL
-
MySQL PostgreSQL is an object-relational database system, which means it can support more complex data types than its competitors . Today we will learn how to use SELECT the operator to join the columns of a table. Using operators to || joi
Using CASE in PostgreSQL
Publish Date:2025/04/11 Views:124 Category:PostgreSQL
-
This article shows how to use the statement in PostgreSQL CASE . CASE How to use the statement in PostgreSQL case Statements are similar to those in general-purpose programming languages if-else . But in SQL, if you want to write IF-ELSE ,
Using NOT IN with subqueries in PostgreSQL
Publish Date:2025/04/11 Views:93 Category:PostgreSQL
-
NOT IN The inverts the result of NOT simply using IN the operator. NOT IN The right side of the operator must have a subquery in which multiple columns are returned to check whether the expression matches the data. NOT IN Tends to return tr
Using variables in PostgreSQL
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article will demonstrate how we can declare and assign values to variables in PostgreSQL. In PostgreSQL, DECLARE variables are declared using Often you will need variables in your PL/SQL scripts. In DECLARE the section called , y
Connect to PostgreSQL using a password
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article shows various ways to connect to PostgreSQL using a password. It can be through the command line, pgpass a file, PGPASSWORD an environment variable or a connection string. Connecting to PostgreSQL with a password using the comm
Deleting a database in PostgreSQL via PSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
There are two ways to access PostgreSQL objects and databases on your system. One is through an interface, such as a graphical interface like PGADMIN, and the other is the basic command line tool psql. Today, we will look at DROP DATABASE t
Using the database in PostgreSQL
Publish Date:2025/04/11 Views:132 Category:PostgreSQL
-
This article demonstrates connecting to a database, creating a new database, and creating a table in PostgreSQL. Available databases in PostgreSQL You can run the following command after opening the Postgres command line to view all availab