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 name | Salary |
John | 1000 |
Mike | 2000 |
Peter | 2500 |
Micheal | 1000 |
Table appraisal:-Employee name | Appraisal amount |
Mike | 450 |
Lisa | 1000 |
Mark | 230 |
Peter | 400 |
SELECT Employee_name FROM employee
UNION ALL
SELECT Employee_name FROM appraisal
Returns:John
Mike
Peter
Micheal
Mike
Lisa
Mark
Peter