UNION joins two results and remove duplicates, while UNION ALL does not remove duplicates.
UNION also sort the final output.
What I want is the UNION ALL without duplicates and without the sort. Is that possible?
The reason for this is that I want the result of the first query to be on top of the final result, and the second query at the bottom (and each sorted as if they where run individually).
I notice this question gets quite a lot of views so I’ll first address a question you didn’t ask!
Regarding the title. To achieve a “Sql Union All with “distinct”” then simply replace
UNION ALLwithUNION. This has the effect of removing duplicates.For your specific question, given the clarification “The first query should have “priority”, so duplicates should be removed from bottom” you can use