Yesterday I posted a puzzle SQL SERVER – Simple Puzzle Using Union and Union All, today we will see the answer of this. Following image explains the answer of puzzle.
You can read the explanation of why this is answer read my previous article SQL SERVER – Union vs. Union All – Which is better for performance?
Reference : Pinal Dave (https://blog.sqlauthority.com)
7 Comments. Leave new
this is because of union, when ur using union in record than it shows distinct records
yep, the perenthesis are rearanging the order of the unions which is causing the
1 union all (1 union 2) to
(1 union all 1) union 2
trixy!
A UNION statement effectively does a SELECT DISTINCT on the results set. If you know that all the records returned are unique from your union, use UNION ALL instead, it gives faster results.
If we use another UNION between both scripts it will return (1,2)