PL/SQL – set operators

There are a few set operators:
UNION – concatenates results from multiple queries. Rows are sorted, duplicates are removed.
UNION ALL – like above, but no sorting, no removing duplicates. Use this rather than union when you know that there are no duplictes – it is more performant as sorting is not performed.
MINUS – substracts second result from the first one. Rows are sorted, duplicates are removed.
INTERSECT – returns results which occurred in both queries. Rows are sorted, duplicates are removed.

Every single query must have the same number of columns and appropriate data types should be similar. Column names in result are taken from first query.
Final result is sorted by all columns from left to right. If you want to sort by yourself remember to do this on full (final) query. Union All is exception from this rule – you can order by after the last single query and it will sort full union result.

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