UNION ALL
Example –
select name, rollnumber
from student
UNION
select name, rollnumber
from marks
The UNION operator is used to combine the result-set of two or more SELECT statements.
Every SELECT statement within UNION must have the same number of columns
The columns must also have similar data types
The columns in every SELECT statement must also be in the same order
Two different tables are being used here for extraction of rows yet the column specified for extraction is the same for both. An error occurs if different columns are being used. The data type specified also must be the same for both the queries.
2. Union All :
A union is used for extracting rows using the conditions specified in the query while Union All is used for extracting all the rows from a set of two tables.
Syntax –
query1 UNION ALL query2
The same conditions are applicable to Union All. The only difference between Union and Union All is that Union extracts the rows that are being specified in the query while Union All extracts all the rows including the duplicates (repeated values) from both the queries.