dbt Analytics Engineering (50 Qs)•Part 1: dbt Core Concepts & Fundamentals (Junior / Entry-Level)
How does Jinja Templating work in dbt?
junior LevelCode Exampledbtdbt Core Concepts
In Simple Words: dbt combines SQL with the Jinja2 templating language:
{{ ... }}: Expressions that output text (e.g.{{ ref('model') }}or{{ var('my_var') }}).{% ... %}: Control structures (loops, conditionals):
sql
-- Dynamically pivot columns without writing 20 CASE statements
{% set payment_methods = ['credit_card', 'paypal', 'bank_transfer', 'crypto'] %}
SELECT
order_id,
{% for method in payment_methods %}
SUM(CASE WHEN payment_method = '{{ method }}' THEN amount ELSE 0 END) AS {{ method }}_amount
{% if not loop.last %},{% endif %}
{% endfor %}
FROM {{ ref('stg_payments') }}
GROUP BY 1