Consider the following query, where both sets need to be filtered by the same Location. Is there a more efficient/performant approach whereby the Location filter is only applied once across both sets, or are you forced to apply the same filter to each set individually?
SELECT *
FROM Orders
WHERE Quantity BETWEEN 1 AND 100
AND Location = 'SE'
EXCEPT
SELECT *
FROM Orders
WHERE Quantity BETWEEN 50 AND 75
AND Location = 'SE';
NOTE: Not looking to refactor the set operations per se- it’s just a dummy example. My question is only about how you would handle common WHERE clauses across set operations.
You can do this with a CTE:
If CTEs are not available, you could do the same thing with a view.