Challenge - 5 Problems
Macro Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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() }}Attempts:
2 left
💡 Hint
Remember that the macro inserts the exact WHERE clause with the string 'active' in quotes.
✗ Incorrect
The macro inserts the WHERE clause with status = 'active' exactly as defined, so the compiled SQL includes that condition.
🧠 Conceptual
intermediate1:30remaining
Purpose of macros in dbt
What is the main purpose of using macros in dbt projects?
Attempts:
2 left
💡 Hint
Think about how macros help with writing SQL code efficiently.
✗ Incorrect
Macros allow you to write SQL code once and reuse it many times, making your project cleaner and easier to maintain.
🔧 Debug
advanced2: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() }}Attempts:
2 left
💡 Hint
Check if the macro is called with the required argument.
✗ Incorrect
The macro requires one argument 'date_col', but it is called without any arguments, causing a TypeError.
❓ data_output
advanced2: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() }}Attempts:
2 left
💡 Hint
Macros can call other macros and insert their code exactly where called.
✗ Incorrect
The full_filter macro calls base_filter, inserting "status = 'active'" inside the WHERE clause combined with AND and the date condition.
🚀 Application
expert3: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']) }}Attempts:
2 left
💡 Hint
Think about how to join column names with ' IS NULL OR ' and add ' IS NULL' only once at the end.
✗ Incorrect
The macro joins column names with ' IS NULL OR ' and adds ' IS NULL' at the end, producing the correct SQL condition checking if any column is NULL.