2

Consider I have a query like

select * from A
Except 
select * from B 
union all
select * from B 
except
select * from A

Query is processed like

select * 
from 
(
select * from A
Except 
select * from B 
) a
union all
(
select * from B
Except 
select * from A 
) b

How is the order of processing defined in sql. Will it process like this at any case

select * from A
Except 
select * from
(
select * from B 
union all
select * from B 
) a
except
select * from A
ask_Overflow
  • 275
  • 3
  • 15

2 Answers2

5

EXCEPT and UNION are processed "left to right". Meaning that without any parenthesis to make the determination they will process in the order they appear in the sql.

https://msdn.microsoft.com/en-us/library/ms188055.aspx

Sean Lange
  • 33,028
  • 3
  • 25
  • 40
3

UNION and EXCEPT have equal precedence but will bind from left to right, meaning they are evaluated in "left to right" order, as they are processed.

From @SeanLange's URL (TL;DR), worth taking note of:

If EXCEPT or INTERSECT is used together with other operators in an expression, it is evaluated in the context of the following precedence:

  • Expressions in parentheses
  • The INTERSECT operator
  • EXCEPT and UNION evaluated from left to right based on their position in the expression
Community
  • 1
  • 1
Radu Gheorghiu
  • 20,049
  • 16
  • 72
  • 107