UNION UNION vs UNION ALL: Simplifying Data Combination in SQL

UNION vs UNION ALL: Simplifying Data Combination in SQL

When combining the results of two tables with identical columns, we have to join the rows. That’s where UNION comes into play. By using UNION, we can create a single result set that merges the data from both tables, allowing for comprehensive analysis and comparison.

UNION vs UNION ALL

There are two variations of UNION that you can use depending on your needs:

  • UNION: If there are duplicate rows, this command will eliminate those. This ensures that each row in the result set is unique, which can be useful when you need to avoid redundancy.
  • UNION ALL: If there are duplicate rows, this command will include all rows without ignoring any. This is useful when retaining all occurrences of each row, including duplicates, for complete data representation.

Practical Uses of UNION

Understanding how to use UNION and UNION ALL can be very practical in various real-world scenarios. Here are a few examples:

  1. Combining Sales Data: As shown in our examples, you might have separate tables for store sales and online sales. Using UNION, you can combine these tables into a single dataset for analysis, making it easier to get a complete picture of your total sales.
  2. Merging Customer Information: Suppose you have customer data stored in multiple tables based on different sources (e.g., website sign-ups and in-store sign-ups). By using UNION, you can create a single view of all customers, which helps in better customer management and marketing strategies.
  3. Generating Reports: When creating reports that need to include data from multiple sources, UNION can be very helpful. For instance, if you need to generate a quarterly report that includes both domestic and international sales, you can use UNION to combine these data sources into a unified report.

Here’s an example of combining customer data:

SELECT 'WEBSITE', CustomerID, Name, Email
FROM WebsiteCustomers
UNION ALL
SELECT 'IN-STORE', CustomerID, Name, Email
FROM InStoreCustomers

This query will merge the customer data from both the website and in-store tables, giving you a comprehensive list of all customers.

By understanding and using UNION and UNION ALL effectively, you can streamline your data management processes, making your database operations more efficient and your data analysis more comprehensive.

You must have the same amount of columns

The situation is this: We have two tables. One contains information about store sales, while the other contains information about online sales. Our goal is to filter out results for both so that we can compare them in the same table. We want to filter out almost matching columns, where the difference is in the salesperson’s name for the store sales section. There is no salesperson in the online store. This can represent an error in the SQL logic since the number of columns for both sections must be the same.

Let’s look at the following example:

SELECT 'STORE', SalesOrderID, OrderDate, SubTotal, SalesPersonName
FROM StoreSales
UNION ALL
SELECT 'ONLINE', SalesOrderID, OrderDate, SubTotal
FROM OnlineSales

The situation here is that the STORE section has one more column selected than the ONLINE section. UNIONing these will cause an error. To solve this, we have to add a blank column ('') to the ONLINE section so that the query can work without error. So the correct code will look like this:

SELECT 'STORE', SalesOrderID, OrderDate, SubTotal, SalesPersonName
FROM StoreSales
UNION ALL
SELECT 'ONLINE', SalesOrderID, OrderDate, SubTotal, ''
FROM OnlineSales

This way, there will be a SalesPersonName column added with salesperson names listed for store sales, while for the online sales, the result in this column will be an empty string. Here is a dummy table so that we can understand this concept:

SourceSalesOrderIDOrderDateSubTotalSalesPersonName
STORE1342024-06-08300.00Peter
STORE1532024-06-0825.00Sarah
ONLINE151362024-06-0860.00
ONLINE135342024-06-0875.00

The ONLINE SalesPersonName results are showing empty results in this case because of the data type of SalesPersonName. If we were to use SalesPersonID instead, with results represented in numbers, then results would show as 0. But it refers to the same concept of “non-existing.”

Handling Different Data Types

When using UNION or UNION ALL, ensuring that the data types in corresponding columns from both tables are compatible is essential. If the data types don’t match, SQL will throw an error. For example, if you are trying to combine a column with numeric data from one table with a column containing text data from another table, you will need to convert one of the columns to the appropriate type.

Let’s say we have a column in the StoreSales table that contains numeric sales amounts and a column in the OnlineSales table that stores sales amounts as text. We can use the CAST or CONVERT function to ensure both columns have the same data type:

SELECT 'STORE', SalesOrderID, OrderDate, CAST(SubTotal AS VARCHAR) AS SubTotal, SalesPersonName
FROM StoreSales
UNION ALL
SELECT 'ONLINE', SalesOrderID, OrderDate, SubTotal, ''
FROM OnlineSales

In this example, we convert the SubTotal column in the StoreSales table to a VARCHAR type to match the data type of the SubTotal column in the OnlineSales table. This ensures that the UNION operation will work correctly.

Scroll to Top