0
0
dbtdata~20 mins

Unique key for merge behavior in dbt - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Unique Key Merge Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the role of unique keys in dbt merge operations

In dbt, when performing a merge operation to update a target table from a source, why is defining a unique key important?

AIt speeds up the merge by indexing the entire target table automatically.
BIt ensures that each row in the source matches exactly one row in the target, preventing duplicate updates.
CIt allows dbt to skip the merge step if the source table is empty.
DIt automatically creates a primary key constraint on the target table.
Attempts:
2 left
💡 Hint

Think about what happens if multiple rows in the source match the same row in the target.

Predict Output
intermediate
2:00remaining
Output of a dbt merge with duplicate keys in source

Given the following simplified merge logic in dbt, what will happen if the source table contains duplicate rows for the unique key?

merge into target_table as t
using source_table as s
on t.id = s.id
when matched then update set t.value = s.value
when not matched then insert (id, value) values (s.id, s.value)
AThe merge will fail with an error due to multiple matches for the same target row.
BThe merge will update the target row multiple times, ending with the last source row's value.
CThe merge will insert duplicate rows into the target table.
DThe merge will ignore duplicate source rows and update only once.
Attempts:
2 left
💡 Hint

Consider how SQL merge handles multiple matches on the same key.

data_output
advanced
2:00remaining
Resulting target table after merge with unique key

Consider the following target and source tables before a dbt merge operation:

Target table:
id | value
1  | 'A'
2  | 'B'
3  | 'C'

Source table:
id | value
2  | 'X'
4  | 'Y'

After running this merge:

merge into target as t
using source as s
on t.id = s.id
when matched then update set t.value = s.value
when not matched then insert (id, value) values (s.id, s.value)

What will be the content of the target table?

A1|'A', 2|'X', 3|'C', 4|'Y'
B2|'X', 4|'Y'
C1|'A', 2|'X', 3|'C'
D1|'A', 2|'B', 3|'C', 4|'Y'
Attempts:
2 left
💡 Hint

Think about which rows get updated and which get inserted.

🔧 Debug
advanced
2:00remaining
Identify the cause of merge failure due to unique key violation

A dbt merge operation fails with the error: SQL Error: The MERGE statement attempted to UPDATE or DELETE the same row more than once. Given this merge code:

merge into target t
using source s
on t.user_id = s.user_id
when matched then update set t.status = s.status
when not matched then insert (user_id, status) values (s.user_id, s.status)

What is the most likely cause?

AThe source table is empty, so the merge cannot find rows to update.
BThe target table has duplicate user_id values violating primary key constraints.
CThe merge statement is missing a <code>delete</code> clause causing conflicts.
DThe source table contains duplicate user_id values causing multiple matches to the same target row.
Attempts:
2 left
💡 Hint

Consider what happens if the join condition matches multiple source rows to one target row.

🚀 Application
expert
3:00remaining
Designing a unique key for a complex merge scenario

You have a source table with columns user_id, event_date, and event_type. You want to merge this into a target table that stores the latest event per user and event type. Which unique key should you define for the merge to work correctly?

AA composite key on <code>(user_id, event_type)</code> to uniquely identify each event type per user.
BA composite key on <code>(user_id, event_date)</code> to uniquely identify events by date per user.
CA composite key on <code>(user_id, event_date, event_type)</code> to uniquely identify each event occurrence.
DA unique key on <code>event_date</code> only, since dates are unique.
Attempts:
2 left
💡 Hint

Think about what combination of columns uniquely identifies each event occurrence.