Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does INTERSECT have a higher precedence compared to UNION?

If we consider three single-column tables each having two rows: A = (1, 2), B = (2, 3), C = (3, 4). Then if we try UNION and INTERSECT together using parenthesis, the result is quite consistent:

  • (select * from a union select * from b) intersect select * from c -> 3
  • select * from a union (select * from b intersect select * from c) -> 1, 2, 3

But what about plain and simple...

  • select * from a union select * from b intersect select * from c ?

I've tried it on several databases (SQL Fiddle) and what I empirically got is:

  • In one corner we have Oracle, PostgreSQL, and H2 that consider INTERSECT having the same precedence as UNION (hence the result is 3).
  • Then, in the other corner is DB2, SQL Server, MariaDB, Apache Derby, and HyperSQL that consider INTERSECT having a higher precedence than UNION (hence the result is 1, 2, 3).
  • MySQL and Sybase ASE stay out of the ring, since they don't implement INTERSECT at all.

Do you guys know if there is any official definition on this? I skimmed the SQL-92 spec but couldn't find anything on the subject.

like image 426
The Impaler Avatar asked May 20 '19 15:05

The Impaler


People also ask

What is the precedence of set operators UNION UNION all INTERSECT and MINUS?

You can combine multiple queries using the set operators UNION , UNION ALL , INTERSECT , and MINUS . All set operators have equal precedence. If a SQL statement contains multiple set operators, then Oracle Database evaluates them from the left to right unless parentheses explicitly specify another order.

What is the difference between UNION and UNION all and INTERSECT?

UNION combines results from both tables. UNION ALL combines two or more result sets into a single set, including all duplicate rows. INTERSECT takes the rows from both the result sets which are common in both. EXCEPT takes the rows from the first result data but does not in the second result set.

What is the precedence of set operators?

All of the set operators have the same precedence. In complex queries that include more than one set operator, the precedence of operators is from left to right. Use parentheses to group set operators and their operands, if you need to override the default left-to-right precedence of set operators.

What is the difference between UNION MINUS and INTERSECT?

INTERSECT gives you the rows that are found in both queries by eliminating rows that are only found in one or the other query. MINUS gives you the rows that are found in the first query and not in the second query by removing from the results all the rows that are found only in the second query.


1 Answers

Oracle has this explanatory note in its documentation:

To comply with emerging SQL standards, a future release of Oracle will give the INTERSECT operator greater precedence than the other set operators. Therefore, you should use parentheses to specify order of evaluation in queries that use the INTERSECT operator with other set operators.

So, Oracle at least thinks that equal precedence is not consistent with the standard.

As a note: I often find the standard so inscrutable that hints like this are simpler than attempting to decipher the actual text.

like image 66
Gordon Linoff Avatar answered Sep 16 '22 11:09

Gordon Linoff