DBT DBT Macros & Jinja 1 — Questions and Answers
Question 1: Which delimiter is used in Jinja to output a value in dbt SQL files?
- {% ... %}
- {{ ... }} (Correct answer)
- {# ... #}
- << ... >>
Correct answer: {{ ... }}
`{{ }}` is the Jinja expression delimiter that evaluates and renders a value into the SQL output.
Question 2: What Jinja delimiter is used for control flow statements like `if`, `for`, and `set` in dbt?
- {{ ... }}
- {% ... %} (Correct answer)
- {# ... #}
- {$ ... $}
Correct answer: {% ... %}
`{% %}` is used for Jinja statements such as `{% if %}`, `{% for %}`, and `{% set %}` which control logic without producing direct output.
Question 3: How do you define a reusable macro in dbt?
- Create a function in models/macros.sql
- Define it with {% macro name(args) %} ... {% endmacro %} in the macros/ folder (Correct answer)
- Add it to dbt_project.yml under macros:
- Use the def keyword in a .py file
Correct answer: Define it with {% macro name(args) %} ... {% endmacro %} in the macros/ folder
Macros are defined with `{% macro macro_name(arg1) %}...{% endmacro %}` blocks stored in any `.sql` file inside the `macros/` directory.
Question 4: What built-in dbt variable returns the current target name (e.g., 'dev' or 'prod')?
- {{ env_var('DBT_TARGET') }}
- {{ target.name }} (Correct answer)
- {{ this.schema }}
- {{ run_started_at }}
Correct answer: {{ target.name }}
`{{ target.name }}` returns the name of the active target as defined in profiles.yml, commonly used to branch logic between environments.
Question 5: Which dbt Jinja function safely reads an environment variable and supports a default value?
- {{ var('VAR_NAME', 'default') }}
- {{ env_var('VAR_NAME', 'default') }} (Correct answer)
- {{ get_env('VAR_NAME') }}
- {{ os.environ['VAR_NAME'] }}
Correct answer: {{ env_var('VAR_NAME', 'default') }}
`env_var('NAME', 'default')` reads a process environment variable and returns the default if the variable is not set.
Question 6: What does `{{ this }}` refer to inside a dbt model?
- The dbt project object
- The relation (schema.table) of the model currently being built (Correct answer)
- The current Jinja context
- The source table being referenced
Correct answer: The relation (schema.table) of the model currently being built
`{{ this }}` is a special dbt variable that resolves to the fully qualified relation of the model being compiled, useful in incremental logic.
Which delimiter is used in Jinja to output a value in dbt SQL files?