Union Vs Union All : Union Remove Duplicates ??

Posted: 01/02/2015 in T-SQL

The heading shoots a very basic interview question in your mind right ??

We might answered correctly for all the time as “Union avoid duplicates but Union all doesn’t” fine :). We are not going to discuss about the difference between Union and Union All  but about a hidden scenario lies while using Union operator- which can really trap you in interviews.

“If Union remove duplicates, does it remove duplicates within the table?” Yes/ No ? Keep your Answer ready.

See the below example.

Union_unionAll

So If we Union both the tables what result are expecting ?.

1. A completely duplicate eliminated result set.

2. The duplicate inside the table would exist.

Union_unionAll_2

Yes it eliminated all the duplicate rows from the result set(either across/within the tables), so it did a DISTINCT operation on your result set.

So please do comment if you were right/ wrong while in beginning of the post.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s