Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
How dbt works (SQL + Jinja + YAML)
📖 Scenario: You are working as a data analyst in a company that uses dbt (data build tool) to transform raw data into clean, usable tables for reporting. You want to understand how dbt combines SQL, Jinja templating, and YAML configuration to build data models step-by-step.
🎯 Goal: Build a simple dbt model using SQL with Jinja templating and configure it with YAML. You will create a source table, add a configuration variable, write a SQL model using Jinja, and finally display the compiled SQL output.
📋 What You'll Learn
Create a source table dictionary in YAML format
Add a configuration variable for filtering data
Write a SQL model using Jinja templating to filter data based on the config variable
Print the final compiled SQL query as output
💡 Why This Matters
🌍 Real World
dbt is widely used in data teams to manage and transform data in a clear, version-controlled way. Understanding how SQL, Jinja, and YAML work together helps you build reliable data models.
💼 Career
Data analysts and engineers use dbt to create clean datasets for reporting and analysis. Knowing how to write dbt models and configurations is a valuable skill in modern data roles.
Progress0 / 4 steps
1
Create a source table configuration in YAML
Create a YAML string variable called source_yaml that defines a source named raw_data with a table named users. The table should have columns id, name, and age.
dbt
Hint
Use triple quotes to create a multi-line YAML string. Follow the indentation exactly as shown.
2
Add a configuration variable for filtering age
Create a variable called min_age and set it to 18. This will be used to filter users who are at least 18 years old.
dbt
Hint
Just assign the number 18 to the variable named min_age.
3
Write a SQL model using Jinja templating to filter users by age
Create a string variable called model_sql that contains a SQL query using Jinja templating. The query should select id, name, and age from {{ source('raw_data', 'users') }} where age >= {{ min_age }}.
dbt
Hint
Use triple quotes for the SQL string. Use Jinja syntax with double curly braces for source and min_age.
4
Print the compiled SQL query
Write a print statement to display the model_sql string.
dbt
Hint
Use print(model_sql) to display the SQL string.
Practice
(1/5)
1. What is the main role of Jinja in dbt projects?
easy
A. To add logic and dynamic behavior to SQL queries
B. To write raw SQL queries without any modification
C. To manage configuration and documentation files
D. To execute the SQL queries on the database
Solution
Step 1: Understand Jinja's purpose in dbt
Jinja is a templating language that allows adding logic like loops and conditions inside SQL files.
Step 2: Differentiate roles of SQL, Jinja, and YAML
SQL writes queries, YAML manages configs/docs, and Jinja adds dynamic logic to SQL.
Final Answer:
To add logic and dynamic behavior to SQL queries -> Option A
A. Because the indentation for 'users' is incorrect under 'my_project'
B. Because '+materialized' cannot be set in YAML
C. Because tags must be a string, not a list
D. Because 'models' key is missing
Solution
Step 1: Check YAML indentation rules for dbt configs
In dbt, model configs under a project must be indented properly; 'users' should be at the same level as '+materialized'.
Step 2: Identify the indentation error
'users' is indented too far, making it a child of '+materialized' which is invalid.
Final Answer:
Because the indentation for 'users' is incorrect under 'my_project' -> Option A
Quick Check:
YAML indentation matters for nested configs [OK]
Hint: Check YAML indentation carefully for nested configs [OK]
Common Mistakes:
Ignoring YAML indentation importance
Thinking '+materialized' is invalid syntax
Assuming tags cannot be lists
5. You want to create a dbt model that selects only active users from a table, but the 'active' flag is stored in a YAML config. Which approach correctly combines SQL, Jinja, and YAML to achieve this?
hard
A. Use Jinja to read YAML directly inside SQL without defining variables
B. Write WHERE active = true directly in SQL without YAML or Jinja
C. Define 'active_flag: true' in YAML, then use WHERE active = {{ var('active_flag') }} in SQL with Jinja
D. Set 'active_flag' in YAML but forget to use Jinja in SQL, so filter is missing
Solution
Step 1: Store the filter value in YAML as a variable
Define 'active_flag: true' in YAML under vars or config to make it accessible.
Step 2: Use Jinja to insert the variable in SQL WHERE clause
Use WHERE active = {{ var('active_flag') }} so the SQL filters active users dynamically.
Final Answer:
Define 'active_flag: true' in YAML, then use WHERE active = {{ var('active_flag') }} in SQL with Jinja -> Option C
Quick Check:
YAML vars + Jinja in SQL = dynamic filters [OK]
Hint: Use YAML vars + Jinja {{ var() }} in SQL WHERE [OK]