PostgreSQL: Row and Array Comparisons

I kind of skipped over the functions/operators section of the PostgreSQL documentation when reading it. I want to review the sections that I think would be useful to me.

Date Created:
1 6

References



Notes


This section describes specialized constructs for making comparisons between groups of values. These forms are syntactically involved to subquery forms, but do not involve subqueries.

IN

expression IN (value [, ...])

The right hand side is a parenthesized list of expressions. The result is true if the left-hand expression's result is equal to any of the right-hand expressions. If the left-hand expression yields null, or if there are no equal right-hand values and at least one right-hand expression yields null, the result of the IN construct will be null, not false. This is in accordance with SQL's normal rules for Boolean combinations of null values.

NOT IN

expression NOT IN (value, [...])

The right hand side is a parenthesized list of expressions. The result is true if the left-hand expression's result is unequal to all of the right-hand expressions.

ANY/SOME (array)

expresison operator ANY (array expression)
expression operator SOME (array expression)

The right hand side is a parenthesized expression, which must yield an array value. The left-hand expression is evaluated and compared to each element of the array using the given operator, which must yield a Boolean result. The result of ANY is true if any true result is obtained. The result is false if no true result is found (including the case where the array has zero elements). If the array expression yields a null array, the result of ANY will be null. If the left-hand expression yields null, the result of ANY is ordinarily null. SOME is a synonym for ANY

ALL (array)

expression operator ALL (array expression)

The right hand side is a parenthesized expression, which must yield an array value. The left-hand expression is evaluated and compared to each element of the array using the given operator, which must yield a Boolean result. The result of ALL is true if all comparisons yield true (including the case where the array has zero elements). The result if false if any false result is found.

Row Constructor Comparison

row_constructor operator row_constructor 

Each side is a row constructor. The two row constructors must have the same number of fields. The given operator is applied to each pair of corresponding fields. All the selected operators must be members of some B-tree operator class, or be the negator of an = member of a B-tree operator class, meaning that row constructor comparison is only possible when the operator is =, <>, <=, <, >, or >=, or something similar.

row_constructor IS DISTINCT FROM row_constructor

The constructor is similar to a <> row comparison, but it does not yield null for null inputs.

row_constructor IS NOT DISTINCT FROM row_constructor

This constant is similar to a = row comparison, but it does not yield null for null inputs.

Composite Type Comparison

The SQL specification requires row-wise comparison to return NULL if the result on comparing two NULL values or a NULL and a non-NULL. PostgreSQL does this only when comparing the results of two constructors or comparing a row constructor of the output of a subquery. In other contexts where two composite-type values are compared, two NULLL field values are considered equal, and a NULL is considered larger than a non-NULL. This is necessary in order to have consistent sorting and indexing behavior for composite types.

Each side is evaluated and they are compared row-wise.

Comments

You have to be logged in to add a comment

User Comments

Insert Math Markup

ESC
About Inserting Math Content
Display Style:

Embed News Content

ESC
About Embedding News Content

Embed Youtube Video

ESC
Embedding Youtube Videos

Embed TikTok Video

ESC
Embedding TikTok Videos

Embed X Post

ESC
Embedding X Posts

Embed Instagram Post

ESC
Embedding Instagram Posts

Insert Details Element

ESC

Example Output:

Summary Title
You will be able to insert content here after confirming the title of the <details> element.

Insert Table

ESC
Customization
Align:
Preview:

Insert Horizontal Rule

#000000

Preview:


View Content At Different Sizes

ESC

Edit Style of Block Nodes

ESC

Edit the background color, default text color, margin, padding, and border of block nodes. Editable block nodes include paragraphs, headers, and lists.

#ffffff
#000000

Edit Selected Cells

Change the background color, vertical align, and borders of the cells in the current selection.

#ffffff
Vertical Align:
Border
#000000
Border Style:

Edit Table

ESC
Customization:
Align:

Upload Lexical State

ESC

Upload a .lexical file. If the file type matches the type of the current editor, then a preview will be shown below the file input.

Upload 3D Object

ESC

Upload Jupyter Notebook

ESC

Upload a Jupyter notebook and embed the resulting HTML in the text editor.

Insert Custom HTML

ESC

Edit Image Background Color

ESC
#ffffff

Insert Columns Layout

ESC
Column Type:

Select Code Language

ESC
Select Coding Language

Insert Chart

ESC

Use the search box below

Upload Previous Version of Article State

ESC