PostgreSQL: SubQuery Expressions

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 15

References



Note: Comparing NULL and NULL in PostgreSQL with a Boolean operator in PostgreSQL results in NULL, not TRUE or FALSE.


Notes


These SQL-compliant subquery expressions are available in PostgreSQL - all expressions return Boolean results.

EXISTS

EXISTS (subquery)

The argument of EXISTS is an arbitrarySELECT statement, or subquery. The subquery is evaluated to determine whether it returns any rows. If it returns at least one row, the results of EXISTS is true. If the subquery returns no rows, the result of EXISTS is false. The subquery can refer to variables from the surrounding query, which will act as constants during any one evaluation of the subquery. The subquery will generally only be executed long enough to determine whether at least one row is returned, not all the way to completion. A common convention for subqueries is SELECT 1 WHERE... since it doesn't matter what is in the rows that are returned.


IN

expression|row_constructor IN (subquery)
  • expression
    • The right-hand side is a parenthesized subquery, which must return exactly one column. The left-hand expression is evaluated and compared to each row of the subquery result. The result of IN is true if any equal subquery is found. The result is false is no equal row is found.
  • row_constructor
    • The left hand side is a row constructor and the right-hand side is a parenthesized subquery, which must return exactly as many columns as there are expressions in the left-hand row. The left-hand expressions are evaluated and compared row-wise to each row of the subquery result. The result of IN is true if any equal subquery row is found. The result is false if no equal row is found.


NOT IN

expression|row_constructor NOT IN (subquery)

Basically the opposite of IN.


ANY/SOME

expression operator ANY (subquery)
expression operator SOME (subquery)

The right hand side is a parenthesized subquery, which must return exactly one column. The left-hand expression is evaluated and compared to each row of the subquery result using the given operator, which must yield a Boolean result. The result of ANY (and SOME, which is a synonym for ANY) is true if any result is obtained. The result is false is no true result is found.


ALL

exppression|row_cosntructor operator ALL (subquery)

The right-hand side is a parenthesized subquery.

  • expression
    • The left hand expression is evaluated and compared to each row of the subquery result using the given operator, which must yield a Boolean result. The result of ALL is true if all rows yield true. The result is false if any false result is found. The result is NULL if no comparison with a subquery row returns false, and at least one comparison returns NULL
  • row_constructor
    • The row_constructor and subquery must return the same number of columns. The results are evaluated and compared row-wise to each row of the subquery result, using the given operator. The result of ALL is true if the comparison returns true for all subquery rows. The result is false if the comparison returns false for any subquery rows. The result is NULL if no comparison with a subquery row returns false, and at least one comparison returns NULL.


Single-Row Comparison

row_constructor operator (subquery)

row_constructor and subquery must return equal number of columns. The subquery cannot return more than one row. The left hand side is evaluated and compared row-wise to the single subquery result row. See this section of the documentation for the meaning of a row constructor comparison.


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