Difference between UNION and UNION ALL

What is the difference between UNION and UNION ALL?

UNION selects only distinct values whereas UNION ALL selects all values and not just distinct ones.
UNION: SELECT column_names FROM table_name1
UNION
SELECT column_names FROM table_name2
UNION All: SELECT column_names FROM table_name1
UNION ALL
SELECT column_names FROM table_name2

What is the difference between UNION and UNION ALL?

UNION command selects distinct and related information from two tables. On the other hand, UNION ALL selects all the values from both the tables.

Example:
Table employee:-
Employee nameSalary
John1000
Mike2000
Peter2500
Micheal1000


Table appraisal:-
Employee nameAppraisal amount
Mike450
Lisa1000
Mark230
Peter400

SELECT Employee_name FROM employee
UNION ALL
SELECT Employee_name FROM appraisal

Returns:
John
Mike
Peter
Micheal
Mike
Lisa
Mark
Peter
SQL Server - What is Log Shipping?
What is Log Shipping? - Log shipping enables high availability of database.....
Difference between a Local and a Global temporary table
What is the difference between a Local and a Global temporary table? - A local temporary table lives until the connection is valid....
SQL Server - What is the STUFF and how does it differ from the REPLACE function?
What is the difference between UNION and UNION ALL? - .....
Post your comment