What is the SQL UNION ALL Operator and how it can be used with the database?
The SQL UNION ALL Operator is used to combine the data from multiple SQL Select Statetements.
SQL UNION ALL Operator Example:
Lets assume that you have 2 tables for customers. One table contains all customers from USA and other contains all customers from INDIA. You want to get name and email addresses from both the tables.
Use following syntax of sql union operator for that case:
SELECT name, email FROM customer_usa
SELECT name, email FROM customer_india;
As you can see above, UNION ALL Operator is used to combine the resultset of two SQL Select statements. Make sure that both SQL Statement must have same column counts and same data types in respective columns.
Difference between UNION and UNION ALL operator:
"UNION" operator returns only unique data set. If duplicate rows are avialable, it will return only one of them.
"UNION ALL" retrieve all rows and combine it, even if it is a duplicate.