Union of multiple SQL queries

Suppose we have a union of three queries:

union = query1 | query2 | query3
union.all_ = True

Why does the ALL key only apply to the union of query2 and query3?

We need the ALL key to be applied to all union operations.

union = query1 | query2
union.all_ = True
union |= query3
union.all_ = True

Is it correct?

Because __or__ operator is binary so you create a first union which is union-ed again with the last one. This is the last union that you get.

Well you can just write:

union = Union(query1, query2, query3, all_=True)
1 Like

Thank you for the tip.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.