0
0
dbtdata~20 mins

For loops for dynamic SQL in dbt - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dynamic SQL Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a dbt for loop generating SQL columns
What is the output SQL snippet generated by this dbt Jinja for loop?

{% raw %}{% for col in ['id', 'name', 'age'] %}
  {{ col }} AS {{ col }}_alias{% if not loop.last %}, {% endif %}
{% endfor %}{% endraw %}
dbt
{% for col in ['id', 'name', 'age'] %}
  {{ col }} AS {{ col }}_alias{% if not loop.last %}, {% endif %}
{% endfor %}
Aid AS id_alias, name AS name_alias, age AS age_alias
Bid AS id_alias, name AS name_alias age AS age_alias
Cid AS id_alias; name AS name_alias; age AS age_alias;
Did AS id_alias name AS name_alias age AS age_alias
Attempts:
2 left
💡 Hint
Look at how commas are added only between items, not after the last one.
data_output
intermediate
1:30remaining
Number of columns generated by a dynamic for loop in dbt
Given this dbt for loop generating SQL columns from a list of 5 column names, how many columns will be generated?

{% raw %}{% for col in ['a', 'b', 'c', 'd', 'e'] %}
  {{ col }} AS {{ col }}_alias{% if not loop.last %}, {% endif %}
{% endfor %}{% endraw %}
dbt
{% for col in ['a', 'b', 'c', 'd', 'e'] %}
  {{ col }} AS {{ col }}_alias{% if not loop.last %}, {% endif %}
{% endfor %}
A0
B4
C6
D5
Attempts:
2 left
💡 Hint
Count the items in the list.
🔧 Debug
advanced
2:00remaining
Identify the error in this dbt for loop for dynamic SQL
What error will this dbt Jinja code produce when rendering?

{% raw %}{% for col in columns %}
  {{ col }} AS {{ col }}_alias,
{% endfor %}{% endraw %}

Assume columns = ['x', 'y', 'z'].
dbt
{% for col in columns %}
  {{ col }} AS {{ col }}_alias,
{% endfor %}
AExtra comma after last column causing SQL syntax error
BUndefined variable error for 'columns'
CSyntaxError in Jinja due to missing endif
DNo error, renders correctly
Attempts:
2 left
💡 Hint
Check how commas are placed after each column.
🚀 Application
advanced
2:30remaining
Generate dynamic WHERE clause with dbt for loop
You want to generate a dynamic WHERE clause in dbt SQL using a list of filters: filters = ['age > 30', "country = 'US'", 'active = true'].
Which dbt Jinja code snippet correctly generates:

WHERE age > 30 AND country = 'US' AND active = true
A{% raw %}WHERE {% for f in filters %}{{ f }} OR {% endfor %}{% endraw %}
B{% raw %}WHERE {% for f in filters %}{{ f }}{% if not loop.last %} AND {% endif %}{% endfor %}{% endraw %}
C{% raw %}WHERE {% for f in filters %}AND {{ f }}{% if not loop.last %} {% endif %}{% endfor %}{% endraw %}
D{% raw %}WHERE {{ filters | join(' AND ') }}{% endraw %}
Attempts:
2 left
💡 Hint
Look for correct use of AND and no trailing AND.
🧠 Conceptual
expert
1:30remaining
Why use for loops for dynamic SQL in dbt?
Which of the following is the best reason to use for loops in dbt to generate dynamic SQL code?
ATo make SQL queries run faster by using loops
BTo replace all SQL queries with Python code inside dbt models
CTo automate repetitive SQL code generation based on variable lists, reducing manual errors and improving maintainability
DTo avoid writing any SQL and rely only on Jinja templating
Attempts:
2 left
💡 Hint
Think about the purpose of templating and loops in code generation.