Variables store values you want to reuse. Control flow helps decide what code runs based on conditions.
Variables and control flow in dbt
{% set variable_name = value %}
{% if condition %}
-- code to run if true
{% elif other_condition %}
-- code to run if other condition true
{% else %}
-- code to run if none above
{% endif %}
{% for item in list %}
-- code to repeat for each item
{% endfor %}Variables are set with {% set %} and can hold strings, numbers, or lists.
Control flow uses {% if %}, {% elif %}, {% else %}, and {% for %} blocks.
start_date and use it inside SQL with double curly braces.{% set start_date = '2024-01-01' %}
select * from orders where order_date >= '{{ start_date }}'{% if target.name == 'prod' %}
select * from prod_table
{% else %}
select * from dev_table
{% endif %}{% set tables = ['customers', 'orders', 'products'] %}
{% for table in tables %}
select count(*) from {{ table }};
{% endfor %}This example sets a variable env to the current environment. Then it chooses the table name based on the environment. Finally, it selects data from the chosen table.
{% set env = target.name %}
{% if env == 'prod' %}
{% set table_name = 'sales_prod' %}
{% else %}
{% set table_name = 'sales_dev' %}
{% endif %}
select * from {{ table_name }} where sale_date >= '2024-01-01';Remember to use double curly braces {{ }} to insert variables inside SQL code.
Control flow blocks must be properly closed with {% endif %} or {% endfor %}.
Variables and control flow help make your dbt models dynamic and reusable.
Variables store reusable values in dbt using {% set %}.
Control flow lets you run different code with {% if %} and loops with {% for %}.
Use variables and control flow to make your dbt projects flexible and easier to maintain.
Practice
{% set %} statement do in dbt?Solution
Step 1: Understand the purpose of
The{% set %}{% set %}statement in dbt is used to create a variable that holds a value.Step 2: Identify its use in code reuse
This variable can then be reused multiple times in your dbt project, making code cleaner and easier to maintain.Final Answer:
It creates a variable to store a value for reuse. -> Option DQuick Check:
{% set %}creates variables [OK]
{% set %} means 'store this value' [OK]- Thinking
{% set %}runs SQL directly - Confusing it with model creation
- Assuming it deletes data
my_var with value 10 in dbt?Solution
Step 1: Recall dbt variable syntax
In dbt, variables are created using the Jinja syntax:{% set variable_name = value %}.Step 2: Check each option
{% set my_var = 10 %} matches the correct syntax exactly. Options B and C use wrong keywords or delimiters. {% set 'my_var' = 10 %} incorrectly quotes the variable name.Final Answer:
{% set my_var = 10 %} -> Option AQuick Check:
Correct variable syntax uses{% set %}[OK]
- Using {{ }} instead of {% %} for setting variables
- Quoting variable names incorrectly
- Using wrong keywords like var
{{ greeting }}?
{% set hour = 15 %}
{% if hour < 12 %}
{% set greeting = 'Good morning' %}
{% elif hour < 18 %}
{% set greeting = 'Good afternoon' %}
{% else %}
{% set greeting = 'Good evening' %}
{% endif %}
{{ greeting }}Solution
Step 1: Evaluate the value of
The variablehourhouris set to 15.Step 2: Check the if-elif-else conditions
Since 15 is not less than 12, the first condition fails. It is less than 18, so the second condition is true, settinggreetingto 'Good afternoon'.Final Answer:
Good afternoon -> Option CQuick Check:
15 < 18 -> 'Good afternoon' [OK]
- Assuming hour 15 is morning
- Ignoring elif and jumping to else
- Thinking variable is undefined
{% set total = 0 %}
{% for i in range(3) %}
{% set total = total + i %}
{% endfor %}
{{ total }}Solution
Step 1: Understand Jinja in dbt limitations
dbt uses Jinja templating, which does not support Python'srange()function directly.Step 2: Identify the error cause
Usingrange(3)will cause an error becauserangeis undefined in this context.Final Answer:
Therangefunction is not available in dbt Jinja. -> Option AQuick Check:
Norange()in dbt Jinja [OK]
- Assuming Python functions work in Jinja
- Trying to update variables inside loops without workarounds
- Using wrong loop syntax
env to switch between two schemas: dev_schema if env is 'dev', else prod_schema. Which code snippet correctly implements this control flow?Solution
Step 1: Understand conditional variable assignment in dbt
dbt supports control flow with{% if %}and{% else %}blocks to set variables based on conditions.Step 2: Check each option for correctness
{% if env == 'dev' %} {% set schema = 'dev_schema' %} {% else %} {% set schema = 'prod_schema' %} {% endif %} SELECT * FROM {{ schema }}.my_table uses proper if-else blocks to setschemacorrectly before using it in the query. {% set schema = 'dev_schema' if env == 'dev' else 'prod_schema' %} SELECT * FROM {{ schema }}.my_table uses Python-style inline if which is not supported in Jinja. {% set schema = env == 'dev' and 'dev_schema' or 'prod_schema' %} SELECT * FROM {{ schema }}.my_table uses a Python boolean expression which is not valid in Jinja. {% set schema = 'prod_schema' %} {% if env == 'dev' %} {% set schema = 'dev_schema' %} SELECT * FROM {{ schema }}.my_table setsschemafirst then tries to reset inside if but the query is inside the if block, causing scope issues.Final Answer:
Option B correctly uses if-else to set schema and then queries it. -> Option BQuick Check:
Use {% if %}...{% else %}...{% endif %} for control flow [OK]
- Trying Python inline if syntax in Jinja
- Placing SQL inside if blocks incorrectly
- Not setting variable before using it
Ready to prove it?
Pass the test and this pattern moves unknown
