Sunday, October 21, 2018

Union and Union All

1. What is Union.

It is used to combine result set of two or more select statements. In union select statement contain same no of columns and datatypes.

Union operator selects only distinct value by default. if you want to select duplicate value then used union all.

It remove duplicate records.

Example:

SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2

Union All

This operation again allows you to join multiple data sets into one data set, but it does not remove any duplicate rows. Because this does not remove duplicate rows this process is faster then Union.

Rules to union data:

Each query must have the same number of columns
Each column must have compatible data types
Column names for the final result set are taken from the first query
ORDER BY and COMPUTE clauses can only be issued for the overall result set and not within each individual result set
GROUP BY and HAVING clauses can only be issued for each individual result set and not for the overall result set











No comments:

Post a Comment

How to use SQL pagination using LIMIT and OFFSET

  How to extract just a portion of a larger result set from a SQL SELECT. LIMIT n is an alternative syntax to the FETCH FIRST n ROWS ONLY. T...