Introduction
When working with SQL databases, one of the most versatile tools at your disposal is the UNION operator. It allows you to combine the results of two or more queries into a single result set, making it easier to analyze and manipulate data that spans multiple tables or queries. In this post, we’ll explore what the UNION operator is, how it works, and some practical examples to help you harness its power effectively.
What is UNION?
The UNION operator in SQL is used to combine the results of two or more SELECT queries into a single result set. It effectively merges the results while eliminating duplicate rows, providing a consolidated view of the data. To use UNION, each SELECT query must have the same number of columns, and the corresponding columns must have compatible data types.
Basic Syntax
The basic syntax for the UNION operator is straightforward:
SELECT column1, column2, ...
FROM table1
WHERE condition
UNION
SELECT column1, column2, ...
FROM table2
WHERE condition;
Here’s a breakdown of the syntax:
Each SELECT statement retrieves data from different tables (or sometimes the same table) but with similar structures.
The UNION keyword combines the results of these SELECT statements.
The result set will only include unique rows, meaning duplicates are automatically removed.
Example Usage
Imagine you have two tables: employees_us and employees_eu. Both tables have the same columns: id, name, and position. You want to create a unified list of all employees across these two regions.
SELECT id, name, position
FROM employees_us
UNION
SELECT id, name, position
FROM employees_eu;
In this example, the UNION operator combines the results from both tables. The final result set will list all employees from both employees_us and employees_eu without any duplicates.
UNION vs. UNION ALL
It's important to distinguish between UNION and UNION ALL:
UNION: Combines the results of two queries and removes duplicate rows.
UNION ALL: Combines the results of two queries but does not remove duplicates.
If you want to include all rows from both queries, even if they are identical, use UNION ALL:
SELECT id, name, position
FROM employees_us
UNION ALL
SELECT id, name, position
FROM employees_eu;
In this case, if there are employees with the same id, name, and position in both tables, they will appear multiple times in the result set.
Practical Considerations
1. Column Order and Data Types: Ensure that the columns in each SELECT statement are in the same order and have compatible data types. For instance, if the first column in the first SELECT is an integer, the first column in the second SELECT should also be an integer.
2. Performance: While UNION is powerful, it can be resource-intensive, especially with large datasets due to the overhead of eliminating duplicates. If you do not need to remove duplicates, UNION ALL is generally faster.
3. Ordering Results: If you want to sort the final result set, use an ORDER BY clause at the end of the last SELECT statement:
SELECT id, name, position
FROM employees_us
UNION
SELECT id, name, position
FROM employees_eu
ORDER BY name;
4. Null Values: UNION will treat NULL values as equal to other NULL values, which can affect the outcome if your dataset includes NULLs.
Pixel Perfect Reports Support of Union
Unioning two lists together is very easy to achieve in Pixel Perfect Reports. To perform the Union operation, go to the canvas where the list is located. Double click on the list. On the Data tab, sets up the first list.

Right click on the light blue pane and click the 'Attach New Database' menu to add a datasource for the second list. Sets up the second list.

Adding a Union Group
Right click on the light blue pane to bring up the context menu and click the 'Union Group' menu to open the 'Union Groups Edit' dialog.

Add a Union Group and click the OK button to dismiss the dialog.

Right click the button with the database icon corresponding to the first list to bring up the context menu and click the Union menu to open the Union Edit dialog.

In the Union Group dropdown, select the Union Group you just created.

Click the OK button to dismiss the Union Edit dialog.

Repeat the same steps for the second list. In addition, select an Union Operator.

Click the OK button to dismiss the Union Edit dialog.

That is it. You have create an Union between the two lists.
Conclusion
The UNION operator is a powerful feature in SQL for combining results from multiple queries into a single cohesive dataset. By understanding how it works and when to use it, you can efficiently manage and analyze data that spans across multiple tables or sources. Whether you need to remove duplicates or simply want to gather data from different queries, mastering UNION can significantly enhance your SQL skills and data handling capabilities.
Feel free to experiment with different scenarios and queries to see how UNION can best serve your data needs. Happy querying!