0
0
dbtdata~20 mins

Multi-source fan-in patterns in dbt - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multi-source Fan-in Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this dbt model SQL?

Given two source tables sales and returns, this dbt model combines them using a UNION ALL and calculates net sales. What will be the output of the final query?

dbt
with sales_data as (
  select customer_id, sum(amount) as total_sales
  from {{ source('raw', 'sales') }}
  group by customer_id
),
returns_data as (
  select customer_id, sum(amount) as total_returns
  from {{ source('raw', 'returns') }}
  group by customer_id
),
combined as (
  select customer_id, total_sales, 0 as total_returns from sales_data
  union all
  select customer_id, 0 as total_sales, total_returns from returns_data
)
select customer_id, sum(total_sales) - sum(total_returns) as net_sales
from combined
group by customer_id
order by customer_id
A[{'customer_id': 1, 'net_sales': 100}, {'customer_id': 2, 'net_sales': -50}]
B[{'customer_id': 1, 'net_sales': 150}, {'customer_id': 2, 'net_sales': 0}]
C[{'customer_id': 1, 'net_sales': 100}, {'customer_id': 2, 'net_sales': 50}]
D[{'customer_id': 1, 'net_sales': 0}, {'customer_id': 2, 'net_sales': 50}]
Attempts:
2 left
💡 Hint

Think about how UNION ALL combines rows and how sums are aggregated after grouping.

data_output
intermediate
1:30remaining
How many rows will the final output have?

Consider a dbt model that merges three source tables orders, shipments, and invoices using UNION ALL without filtering duplicates. If orders has 100 rows, shipments has 80 rows, and invoices has 120 rows, how many rows will the combined output have?

dbt
select * from {{ source('raw', 'orders') }}
union all
select * from {{ source('raw', 'shipments') }}
union all
select * from {{ source('raw', 'invoices') }}
A120
B280
C100
D300
Attempts:
2 left
💡 Hint

UNION ALL stacks all rows without removing duplicates.

🔧 Debug
advanced
2:00remaining
Identify the error in this multi-source fan-in model

This dbt model tries to join two sources and then union with a third source. What error will this code raise?

dbt
with joined as (
  select a.id, a.value, b.status
  from {{ source('raw', 'table_a') }} a
  join {{ source('raw', 'table_b') }} b on a.id = b.id
),
unioned as (
  select * from joined
  union all
  select id, value, status from {{ source('raw', 'table_c') }}
)
select * from unioned
ARuntime error: column mismatch in UNION ALL
BSyntaxError due to missing comma
CNo error, query runs successfully
DRuntime error: ambiguous column reference
Attempts:
2 left
💡 Hint

Check if the columns selected in both parts of the UNION ALL match exactly.

🚀 Application
advanced
2:30remaining
Which approach best combines multiple sources with different schemas?

You have three source tables with different columns. You want to create a unified table with columns id, date, and amount. Which dbt SQL snippet correctly uses multi-source fan-in pattern to achieve this?

A
select id, date, amount from {{ source('raw', 'table1') }}
union all
select id, date from {{ source('raw', 'table2') }}
union all
select id, date, amount from {{ source('raw', 'table3') }}
B
select id, date, amount from {{ source('raw', 'table1') }}
union all
select id, date, amount from {{ source('raw', 'table2') }}
union all
select id, date, amount from {{ source('raw', 'table3') }}
C
select id, date, amount from {{ source('raw', 'table1') }}
union
select id, date, amount from {{ source('raw', 'table2') }}
union
select id, date, amount from {{ source('raw', 'table3') }}
D
select id, date, amount from {{ source('raw', 'table1') }}
union all
select id, date, amount, extra_col from {{ source('raw', 'table2') }}
union all
select id, date, amount from {{ source('raw', 'table3') }}
Attempts:
2 left
💡 Hint

All SELECT statements in UNION ALL must have the same number of columns and compatible types.

🧠 Conceptual
expert
1:30remaining
Why use multi-source fan-in patterns in dbt?

Which of the following best explains the main benefit of using multi-source fan-in patterns in dbt projects?

ATo enforce strict schema validation on each source table individually
BTo improve query performance by splitting data into multiple smaller tables
CTo combine data from multiple sources into a single model for easier analysis and reporting
DTo automatically generate documentation for all source tables
Attempts:
2 left
💡 Hint

Think about why you would want to merge data from different places into one place.