Union Introduction
Introduction
A Union of 2 or more tables allows you to combine rows from different tables into a single result set or “stack” tables one on another. In this tutorial, we will discuss the concept of union and provide an example to illustrate the process.
An Overview
Tabula Union works similarly to the SQL UNION operator, but with more flexibility. It can combine tables that have different column sets by matching columns based on their names and data types. The column order in the original tables does not matter. This feature makes union tables a powerful tool for merging data from various sources with varying structures.
Example.
Consider the following two tables:
Table A:
id | name | age | country |
---|---|---|---|
1 | John Smith | 34 | USA |
2 | Jane Doe | 28 | UK |
Table B:
country | id | city | name |
---|---|---|---|
USA | 3 | Los Angeles | Mike Brown |
UK | 4 | London | Emma Watson |
Although the columns in both tables are not in the same order, the union tool can combine them based on the matching column names and data types.
Result:
id | name | age | country | city |
---|---|---|---|---|
1 | John Smith | 34 | USA | NULL |
2 | Jane Doe | 28 | UK | NULL |
3 | Mike Brown | NULL | USA | Los Angeles |
4 | Emma Watson | NULL | UK | London |
In the resulting union table, the matched columns (id, name, and country) are combined, while the columns with no match in the other table (age and city) are filled with NULL values.
Last updated