0
0
dbtdata~20 mins

Variables and control flow in dbt - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Variables and Control Flow in dbt
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of variable assignment and if condition in dbt
What is the output of the following dbt Jinja code snippet when run inside a model?
dbt
{% set x = 5 %}
{% if x > 3 %}
  {{ x * 2 }}
{% else %}
  {{ x + 2 }}
{% endif %}
A10
B7
C5
DError: Undefined variable
Attempts:
2 left
💡 Hint
Check the value of x and the condition in the if statement.
data_output
intermediate
2:00remaining
Result of loop with variable increment in dbt
What list will be output by this dbt Jinja code?
dbt
{% set result = [] %}
{% set counter = 0 %}
{% for i in range(3) %}
  {% set counter = counter + i %}
  {% do result.append(counter) %}
{% endfor %}
{{ result }}
A[0, 1, 3]
B[0, 0, 0]
C[1, 3, 6]
DError: 'do' statement not allowed
Attempts:
2 left
💡 Hint
Remember that counter starts at 0 and increments by i each loop.
🔧 Debug
advanced
2:00remaining
Identify the error in variable scope within dbt Jinja
Why does the following dbt code raise an error or produce unexpected output?
dbt
{% for i in range(2) %}
  {% set val = i * 2 %}
{% endfor %}
{{ val }}
Aval holds the last value from the loop, outputting 2
Bval is undefined outside the loop, causing an error
Cval is a list of all values from the loop, outputting [0, 2]
DSyntaxError due to missing endfor
Attempts:
2 left
💡 Hint
Consider variable scope inside and outside loops in Jinja.
🧠 Conceptual
advanced
2:00remaining
Understanding control flow with nested if-else in dbt
Given the variable 'score' set to 75, what will this dbt Jinja code output?
dbt
{% set score = 75 %}
{% if score >= 90 %}
  Excellent
{% elif score >= 70 %}
  Good
{% else %}
  Needs Improvement
{% endif %}
AExcellent
BError: Invalid syntax
CNeeds Improvement
DGood
Attempts:
2 left
💡 Hint
Check the conditions in order and which one matches 75.
🚀 Application
expert
3:00remaining
Predict the output of complex variable manipulation and control flow in dbt
What is the final output of this dbt Jinja code snippet?
dbt
{% set data = [1, 2, 3, 4] %}
{% set total = 0 %}
{% for num in data %}
  {% if num % 2 == 0 %}
    {% set total = total + num * 2 %}
  {% else %}
    {% set total = total + num %}
  {% endif %}
{% endfor %}
{{ total }}
A15
B20
C18
DError: Cannot modify variable inside loop
Attempts:
2 left
💡 Hint
Calculate total step by step, doubling even numbers before adding.