Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 106/ 160
06. Data Transformation (dbt) & Workflow Orchestration

Jinja Templating, Macros & Custom SQL Generation in dbt

Generate DRY SQL with Jinja macros while keeping compiled SQL reviewable.

IntermediateJinja · Macros · DRY SQL3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Narrow reusable generation intent. Next, explicit dbt nodes and adapter-safe operations. Finally, macro branches and resulting warehouse results.

Think of it like…

Jinja is a SQL stencil: it avoids repainting repeated boilerplate, but the reviewer must inspect the cut-out compiled statement sent to the warehouse.

Where you’ve seen it

{% macro currency_to_minor(column) %}CAST(round({{ column }} * 100, 0) AS BIGINT){% endmacro %} is testable, but table names should still come from {{ ref('stg_payments') }} rather than run_query globals.

Words you’ll hear

Jinja
A compile-time templating language for expressions, loops, conditions, and block reuse in dbt files.
Macros
A named Jinja function accepting arguments and returning SQL or scalar strings for models/tests/hooks.
DRY SQL
Avoiding duplicated source text only when the abstraction clarifies intent and keeps generated SQL understandable.
Operational boundary
A macro that conditionally hides a table reference from Jinja parsing breaks lineage; dynamic SQL based on query result can fail in CI, and excessive abstraction makes a model easier to edit but harder to debug. Keep templates deterministic, make relation and column inputs explicit, use ref/source in the compiled code, compile in CI and inspect target/compiled, and write macro unit cases for adapter/dialect differences.

Visual diagram

Loading interactive diagram

Theory & deep dive

Jinja: the working mechanism

Jinja expressions and dbt macros generate parameterized SQL at compile time, letting projects reuse logic while retaining explicit dependency discovery and a reviewable compiled statement.

Narrow reusable generation intent. Explicit dbt nodes and adapter-safe operations, then inspect SQL for a dev target and edge flags; this exposes the state governed by DRY SQL.

Jinja Templating, Macros & Custom SQL Generation in dbt in one concrete design

{% macro currency_to_minor(column) %}CAST(round({{ column }} * 100, 0) AS BIGINT){% endmacro %} is testable, but table names should still come from {{ ref('stg_payments') }} rather than run_query globals.

Key takeaway: Jinja expressions and dbt macros generate parameterized SQL at compile time, letting projects reuse logic while retaining explicit dependency discovery and a reviewable compiled statement

Jinja Templating, Macros & Custom SQL Generation in dbt: failure mode and production response

A macro that conditionally hides a table reference from Jinja parsing breaks lineage; dynamic SQL based on query result can fail in CI, and excessive abstraction makes a model easier to edit but harder to debug.

Keep templates deterministic, make relation and column inputs explicit, use ref/source in the compiled code, compile in CI and inspect target/compiled, and write macro unit cases for adapter/dialect differences. Track Compiled size as "generated model remains reviewable and bounded", and make Branches visible to the dataset owner.

Key takeaway: Keep templates deterministic, make relation and column inputs explicit, use ref/source in the compiled code, compile in CI and inspect target/compiled, and write macro unit cases for adapter/dialect differences.

How it works, step by step

Step 1 of 4
Jinja

1. Parameterize

Narrow reusable generation intent. Jinja expressions and dbt macros generate parameterized SQL at compile time, letting projects reuse logic while retaining explicit dependency discovery and a reviewable compiled statement.

narrow reusable generation intent
Key numbers at this step
Compiled size
generated model remains reviewable and bounded
Macros
A named Jinja function accepting arguments and returning SQL or scalar strings for models/tests/hooks.

When to use it & trade-offs

What it is

Jinja expressions and dbt macros generate parameterized SQL at compile time, letting projects reuse logic while retaining explicit dependency discovery and a reviewable compiled statement. {% macro currency_to_minor(column) %}CAST(round({{ column }} * 100, 0) AS BIGINT){% endmacro %} is testable, but table names should still come from {{ ref('stg_payments') }} rather than run_query globals.

Why it matters: A macro that conditionally hides a table reference from Jinja parsing breaks lineage; dynamic SQL based on query result can fail in CI, and excessive abstraction makes a model easier to edit but harder to debug. Keep templates deterministic, make relation and column inputs explicit, use ref/source in the compiled code, compile in CI and inspect target/compiled, and write macro unit cases for adapter/dialect differences.

When to reach for it

  • Generate DRY SQL with Jinja macros while keeping compiled SQL reviewable.
  • Choose Jinja because it provides this capability: A compile-time templating language for expressions, loops, conditions, and block reuse in dbt files.
  • Keep evidence for Compiled size at "generated model remains reviewable and bounded" so the last production stage-Test - macro branches and resulting warehouse results-remains testable.

When to avoid or be careful

  • A macro that conditionally hides a table reference from Jinja parsing breaks lineage; dynamic SQL based on query result can fail in CI, and excessive abstraction makes a model easier to edit but harder to debug.
  • Do not conflate Macros with Jinja. The second concept contributes different behavior: A named Jinja function accepting arguments and returning SQL or scalar strings for models/tests/hooks.
  • Release only after this guardrail is in place: Keep templates deterministic, make relation and column inputs explicit, use ref/source in the compiled code, compile in CI and inspect target/compiled, and write macro unit cases for adapter/dialect differences.

Jinja / Macros / DRY SQL - roles, limits, and operating cost

Jinja
Best for: A compile-time templating language for expressions, loops, conditions, and block reuse in dbt files.

Strengths: A compile-time templating language for expressions, loops, conditions, and block reuse in dbt files. Jinja expressions and dbt macros generate parameterized SQL at compile time, letting projects reuse logic while retaining explicit dependency discovery and a reviewable compiled statement.

Watch out: A macro that conditionally hides a table reference from Jinja parsing breaks lineage; dynamic SQL based on query result can fail in CI, and excessive abstraction makes a model easier to edit but harder to debug.

Macros
Best for: A named Jinja function accepting arguments and returning SQL or scalar strings for models/tests/hooks.

Strengths: A named Jinja function accepting arguments and returning SQL or scalar strings for models/tests/hooks. In this workflow it supports: Compile - inspect SQL for a dev target and edge flags.

Watch out: The operating cost is real: each conditional is covered by dbt compile/unit fixture.

DRY SQL
Best for: Avoiding duplicated source text only when the abstraction clarifies intent and keeps generated SQL understandable.

Strengths: Avoiding duplicated source text only when the abstraction clarifies intent and keeps generated SQL understandable. Its output can be checked with Node references.

Watch out: Operating guardrail: Keep templates deterministic, make relation and column inputs explicit, use ref/source in the compiled code, compile in CI and inspect target/compiled, and write macro unit cases for adapter/dialect differences.

Interview signals & numbers to know

When this topic comes up

  • “Where is the failure boundary for Jinja, and how would Compiled size reveal it?”
  • “Why is Macros distinct from this behavior: avoiding duplicated source text only when the abstraction clarifies intent and keeps generated SQL understandable?”
  • Production clue: A macro that conditionally hides a table reference from Jinja parsing breaks lineage; dynamic SQL based on query result can fail in CI, and excessive abstraction makes a model easier to edit but harder to debug.
  • Design probe: Keep templates deterministic, make relation and column inputs explicit, use ref/source in the compiled code, compile in CI and inspect target/compiled, and write macro unit cases for adapter/dialect differences.

Benchmarks & rules of thumb

Compiled size -
generated model remains reviewable and bounded
Branches -
each conditional is covered by `dbt compile`/unit fixture
Node references -
dependency edges should remain visible in manifest

Common mistakes & follow-ups

Common interview mistakes

  • A macro that conditionally hides a table reference from Jinja parsing breaks lineage; dynamic SQL based on query result can fail in CI, and excessive abstraction makes a model easier to edit but harder to debug.
  • Reporting success from a job exit code without checking Compiled size and Branches.
  • Treating this example as complete without replay, rollback, or access boundaries: {% macro currency_to_minor(column) %}CAST(round({{ column }} * 100, 0) AS BIGINT){% endmacro %} is testable, but table names should still come from {{ ref('stg_payments') }} rather than run_query globals.

Likely interviewer follow-ups

  • How would you prove this contract instead of only checking that the command ran: A compile-time templating language for expressions, loops, conditions, and block reuse in dbt files.
  • What does the recovery plan do when this occurs? A macro that conditionally hides a table reference from Jinja parsing breaks lineage; dynamic SQL based on query result can fail in CI, and excessive abstraction makes a model easier to edit but harder to debug.
  • When is Jinja insufficient for Branches, and what does adding Macros cost in Compiled size?
Interview tip: Lead with the data path and failure boundary: Jinja expressions and dbt macros generate parameterized SQL at compile time, letting projects reuse logic while retaining explicit dependency discovery and a reviewable compiled statement. Then make the answer operational: Keep templates deterministic, make relation and column inputs explicit, use ref/source in the compiled code, compile in CI and inspect target/compiled, and write macro unit cases for adapter/dialect differences.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Jinja Templating, Macros & Custom SQL Generation in dbt with data engineer interview Q&A and hands-on practice.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Building Incremental Models (is_incremental(), Merge Strategies, Lookback Windows)
Next topic
dbt Packages (dbt_utils, dbt_expectations, codegen)