Union Function in BigQuery: Syntax & Examples

Formulas & functions · Updated November 23, 2026

The UNION function in BigQuery is not a standalone function but a set operator (UNION ALL and UNION DISTINCT) that combines rows from two or more tables or query results into a single result set. To use it, write query1 UNION ALL query2 or query1 UNION DISTINCT query2; the columns must match in number and compatible data types.

What UNION Does in BigQuery

UNION vertically stacks rows: it appends the results of one SELECT statement to another. For example, if you have a table of 2023 sales and a table of 2024 sales with identical columns (date, product, amount), SELECT * FROM sales_2023 UNION ALL SELECT * FROM sales_2024 returns all rows from both tables in one result. The key distinction is between the two variants:

  • UNION ALL: Returns every row from every query, including duplicates. It is faster because it does not perform deduplication.
  • UNION DISTINCT: Removes duplicate rows across the combined result. It is equivalent to UNION in standard SQL (BigQuery treats UNION as UNION DISTINCT).

Syntax and Column Matching Rules

The basic syntax is:

SELECT column1, column2 FROM table1
UNION ALL
SELECT column1, column2 FROM table2

Rules you must follow:

  • Each SELECT must have the same number of columns.
  • Corresponding columns must have compatible data types (e.g., INT64 with INT64, STRING with STRING). BigQuery will implicitly coerce some types (like INT64 to FLOAT64), but not all.
  • Column names in the final result come from the first SELECT. If you want different names, alias them in the first query.
  • You can chain multiple UNIONs: q1 UNION ALL q2 UNION ALL q3.

UNION ALL vs UNION DISTINCT: How to Choose

Use UNION ALL when you need every row, even duplicates, and when performance matters. Use UNION DISTINCT when you need a deduplicated list. For example, to get a list of all unique customers from two regional tables, use UNION DISTINCT. To combine raw log files where duplicates are meaningful, use UNION ALL. In practice, UNION DISTINCT can be expensive on large datasets because it requires sorting or hashing to find duplicates; UNION ALL simply concatenates.

Common Problems and Pitfalls

Here are frequent issues users encounter:

  • Column count mismatch: If one SELECT returns 3 columns and the other returns 2, BigQuery throws an error. Always align the columns explicitly.
  • Type mismatch: Trying to UNION a STRING column with an INT64 column fails. Cast with SAFE_CAST or CAST to a common type.
  • ORDER BY placement: You cannot put an ORDER BY inside each subquery. Place it at the end of the entire UNION to sort the final result.
  • Parentheses for complex queries: If you need to use ORDER BY or LIMIT on individual subqueries, wrap them in parentheses: (SELECT ... ORDER BY col LIMIT 10) UNION ALL (SELECT ...).

UNION vs JOIN vs UNION ALL in BigQuery

UNION and JOIN are often confused. UNION stacks rows vertically; JOIN combines columns horizontally based on a key. For example, to combine customer names with their orders, you use a JOIN (e.g., INNER JOIN). To combine a list of customers from two different sources, you use UNION. A common misconception is that UNION can merge tables with different schemas; it cannot—it only works when the columns align. If you need to bring together tables with different columns, consider using JOIN or restructuring the data with SELECT to pick matching columns.

Performance and Cost Considerations

BigQuery charges based on the amount of data processed by each SELECT, not the output size. With UNION ALL, the cost is the sum of the bytes processed by all subqueries. With UNION DISTINCT, there is an additional cost for the deduplication operation, which can be significant on large datasets. To minimize costs, filter data early with WHERE clauses and only select the columns you need. Also, note that BigQuery supports UNION in both standard SQL and legacy SQL, but standard SQL is recommended and the default for new projects.

Alternatives to UNION

In some cases, other approaches may be more efficient:

  • Wildcard tables: If your tables follow a pattern like sales_2023*, you can query them directly: SELECT * FROM `project.dataset.sales_*` which implicitly unions all matching tables.
  • UNION in subqueries: You can use UNION inside a subquery to combine data before further aggregation.
  • Using ARRAY and STRUCT: For complex transformations, you might use UNNEST with arrays, but that is not a direct replacement for UNION.

Frequently Asked Questions

Can I use UNION with different column names?

Yes, the column names in the final result come from the first SELECT. For example, if the first query has a column named id and the second has customer_id, the result will have a column named id. You can alias columns in the first query to control names.

Does UNION DISTINCT remove duplicates across all columns?

Yes, UNION DISTINCT considers a row duplicate only if all columns match. If any column differs, the row is kept. For partial duplicate detection, you need to group or use a window function.

What is the difference between UNION and UNION ALL in BigQuery?

UNION is a shorthand for UNION DISTINCT, which removes duplicate rows. UNION ALL keeps all rows, including duplicates. In BigQuery, you must explicitly write UNION ALL or UNION DISTINCT; writing just UNION is not allowed—it will result in a syntax error.

Can I UNION more than two queries?

Yes, you can chain multiple SELECT statements: SELECT ... UNION ALL SELECT ... UNION ALL SELECT .... There is no strict limit, but performance may degrade with many subqueries.

How do I sort the results of a UNION?

Place an ORDER BY clause at the very end of the entire UNION statement. For example: SELECT name FROM a UNION ALL SELECT name FROM b ORDER BY name. You cannot order each subquery individually without using parentheses.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.