Home > Mobile >  Order of execution for PostgreSQL when UNION and INTERSECT are used
Order of execution for PostgreSQL when UNION and INTERSECT are used

Time:03-29

I am trying to build a query string that involves the use of multiple UNION and INTERSECT clauses on PostgresSQL/google-BigQuery. I want to understand the order in which each of the select statements will get executed in the following case.

A UNION B INTERSECT C UNION D

(((A UNION B) INTERSECT C) UNION D)

(A UNION (B INTERSECT (C UNION D)))

Will it be right-to-left or left-to-right or something different?

Thanks in advance!

CodePudding user response:

Left to right but INTERSECT binds more tightly than UNION:

(A UNION (B INTERSECT C)) UNION D

See https://www.postgresql.org/docs/13/queries-union.html

  • Related