Skip to content
Advertisement

UNION two SELECT queries but result set is smaller than one of them

In a SQL Server statement there is

The first query returned 131,972 lines of data; the 2nd one, 147,692 lines. I didn’t notice there is any commonly shared line of data from these two tables, so I expect the result set after UNION should be the same as the sum of 131972 + 147692 = 279,384.

However the result set after UNION is 133,857. Even though they might have overlapped lines that I accidently missed, the result should be at least the same as the larger result set of those two. I can’t figure how the number 133,857 came from.

Is my understanding about SQL UNION correct? I use SQL server in this case.

Advertisement

Answer

To expand comment given under the question, which I think states what you already know:

UNION takes care of duplicates also within one table as well.

Just take a look at a example:

SETUP:

Query

will produce output

DB fiddle

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement