0
0
dbtdata~20 mins

Macros for reusable SQL logic in dbt - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Macro Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple macro call in dbt
Given the following dbt macro and model SQL, what is the output SQL after compilation?
dbt
{% macro filter_active_users() %}
  WHERE status = 'active'
{% endmacro %}

SELECT * FROM users {{ filter_active_users() }}
ASELECT * FROM users WHERE status = 'inactive'
BSELECT * FROM users WHERE status = active
CSELECT * FROM users WHERE status = 'active'
DSELECT * FROM users
Attempts:
2 left
💡 Hint
Remember that the macro inserts the exact WHERE clause with the string 'active' in quotes.
🧠 Conceptual
intermediate
1:30remaining
Purpose of macros in dbt
What is the main purpose of using macros in dbt projects?
ATo visualize data in dashboards
BTo create reusable SQL snippets that can be called multiple times to avoid repetition
CTo schedule dbt runs automatically
DTo store data permanently in the database
Attempts:
2 left
💡 Hint
Think about how macros help with writing SQL code efficiently.
🔧 Debug
advanced
2:00remaining
Identify the error in this macro usage
What error will occur when running this dbt model code?
dbt
{% macro get_date_filter(date_col) %}
  WHERE {{ date_col }} > '2023-01-01'
{% endmacro %}

SELECT * FROM sales {{ get_date_filter() }}
ATypeError: get_date_filter() missing 1 required positional argument: 'date_col'
BSyntaxError: Unexpected token in macro
CNo error, runs successfully
DRuntimeError: Undefined variable date_col
Attempts:
2 left
💡 Hint
Check if the macro is called with the required argument.
data_output
advanced
2:30remaining
Resulting SQL from nested macros
Given these macros and model code, what is the final compiled SQL?
dbt
{% macro base_filter() %}
  status = 'active'
{% endmacro %}

{% macro full_filter() %}
  WHERE {{ base_filter() }} AND created_at > '2023-01-01'
{% endmacro %}

SELECT * FROM users {{ full_filter() }}
ASELECT * FROM users WHERE status = 'active' AND created_at > '2023-01-01'
BSELECT * FROM users WHERE status = active AND created_at > '2023-01-01'
CSELECT * FROM users WHERE status = 'active' OR created_at > '2023-01-01'
DSELECT * FROM users WHERE created_at > '2023-01-01'
Attempts:
2 left
💡 Hint
Macros can call other macros and insert their code exactly where called.
🚀 Application
expert
3:00remaining
Using macros to dynamically generate SQL conditions
You want to create a macro that accepts a list of columns and generates a SQL condition checking if any of those columns are NULL. Which macro code produces the correct SQL condition for columns ['col1', 'col2', 'col3']?
dbt
{% macro null_check(cols) %}
  {{- cols | map('string') | join(' IS NULL OR ') }} IS NULL
{% endmacro %}

SELECT * FROM table WHERE {{ null_check(['col1', 'col2', 'col3']) }}
Acol1, col2, col3 IS NULL
BIS NULL col1 OR IS NULL col2 OR IS NULL col3
Ccol1 IS NULL AND col2 IS NULL AND col3 IS NULL
Dcol1 IS NULL OR col2 IS NULL OR col3 IS NULL
Attempts:
2 left
💡 Hint
Think about how to join column names with ' IS NULL OR ' and add ' IS NULL' only once at the end.