How does BigQuery MERGE work, and how would you use it for upserts and slowly changing dimensions?

Quick Overview

Asks how the BigQuery MERGE statement matches a source to a target table and how to use it for upserts and slowly changing dimensions. Tests knowledge of WHEN clause actions, Type 1 versus Type 2 history, duplicate keys, NULL handling and partition costs.

How does BigQuery MERGE work, and how would you use it for upserts and slowly changing dimensions?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

How does the `MERGE` statement work in BigQuery, and how would you use it to implement upserts or slowly changing dimensions? ```hint One source row, one action For a Type 2 history change, count how many actions one changed record needs, and compare that with what a single source row can trigger in one `MERGE`. ``` ```hint Check the edge rows Consider what happens when the staging data has two rows for the same key, or when the column you compare is NULL. ``` ### What a Strong Answer Covers - The structure of `MERGE`: target, source, join condition, and the kinds of `WHEN` clause with the actions each allows - The upsert pattern and SCD Type 1 - How to produce SCD Type 2 history given the limits on what one source row can do - Correctness pitfalls such as duplicate source keys, NULL comparisons, and deletes for keys missing from an incremental batch - How to keep the cost of a `MERGE` down on a partitioned table ### Follow-up Questions - How would you make a daily `MERGE` idempotent, so rerunning the same batch does not create extra history rows? - How would you handle a late-arriving record that is older than the current version in a Type 2 dimension? - When would you rebuild a dimension with `CREATE OR REPLACE TABLE` instead of merging changes? - How would you represent rows deleted in the source system?

Overview: Asks how the BigQuery MERGE statement matches a source to a target table and how to use it for upserts and slowly changing dimensions. Tests knowledge of WHEN clause actions, Type 1 versus Type 2 history, duplicate keys, NULL handling and partition costs.

PracHub logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

How does the MERGE statement work in BigQuery, and how would you use it to implement upserts or slowly changing dimensions?

What a Strong Answer Covers Guidance

  • The structure of MERGE : target, source, join condition, and the kinds of WHEN clause with the actions each allows
  • The upsert pattern and SCD Type 1
  • How to produce SCD Type 2 history given the limits on what one source row can do
  • Correctness pitfalls such as duplicate source keys, NULL comparisons, and deletes for keys missing from an incremental batch
  • How to keep the cost of a MERGE down on a partitioned table

Follow-up Questions Guidance

  • How would you make a daily MERGE idempotent, so rerunning the same batch does not create extra history rows?
  • How would you handle a late-arriving record that is older than the current version in a Type 2 dimension?
  • When would you rebuild a dimension with CREATE OR REPLACE TABLE instead of merging changes?
  • How would you represent rows deleted in the source system?