0
0
dbtdata~5 mins

Variables and control flow in dbt - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a variable in dbt and how is it used?
A variable in dbt is a named value that you can set and reuse in your models or macros. It helps make your code flexible by allowing you to change values without editing the whole code.
Click to reveal answer
beginner
How do you define and use an if-else control flow in dbt?
In dbt, you use Jinja templating for control flow. An if-else looks like: {% if condition %} do something {% else %} do something else {% endif %}. It helps run different code based on conditions.
Click to reveal answer
beginner
Explain the purpose of the 'set' statement in dbt.
The 'set' statement assigns a value to a variable inside a dbt model or macro. For example, {% set my_var = 10 %} stores 10 in my_var for later use in the code.
Click to reveal answer
intermediate
What is the difference between 'if' and 'elif' in dbt control flow?
'if' starts a condition check. 'elif' (else if) checks another condition only if the previous 'if' was false. It helps check multiple conditions in order.
Click to reveal answer
intermediate
How can you use variables to make dbt models more dynamic?
You can pass variables to dbt models or macros to change behavior without changing code. For example, use a variable to filter data by date or region dynamically.
Click to reveal answer
Which syntax correctly sets a variable named 'threshold' to 100 in dbt?
A{% set threshold = 100 %}
Bthreshold = 100
C{{ set threshold = 100 }}
Dlet threshold = 100
What does the following dbt code do? {% if is_active %} SELECT * FROM users {% else %} SELECT * FROM guests {% endif %}
AAlways selects from users table
BSelects from users if is_active is true, else from guests
CSelects from guests table only
DThrows an error because of syntax
How do you check multiple conditions in dbt control flow?
AUsing if, elif, and else blocks
BUsing multiple if statements without else
CUsing switch-case statements
DUsing only else statements
Which of these is NOT a valid way to use variables in dbt?
APassing variables to macros
BUsing variables to filter data
CChanging variable values during model execution
DSetting variables with {% set %} statement
What happens if the condition in an if statement is false and there is no else block?
ACode inside if runs twice
BCode inside if runs anyway
CError is thrown
DCode inside if is skipped
Explain how variables and control flow work together in dbt to make models flexible.
Think about how you can change model behavior without rewriting code.
You got /4 concepts.
    Describe a real-life example where you would use variables and if-else in a dbt model.
    Imagine you want to run different queries based on user input or environment.
    You got /3 concepts.