0
0
dbtdata~20 mins

Why Jinja makes SQL dynamic in dbt - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Jinja SQL Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How does Jinja enable dynamic SQL in dbt?

Which of the following best explains why Jinja makes SQL dynamic in dbt?

AJinja stores SQL queries in a database for reuse.
BJinja converts SQL queries into machine code for faster execution.
CJinja allows embedding Python-like code to generate SQL statements based on conditions and variables at compile time.
DJinja automatically optimizes SQL queries by rewriting them.
Attempts:
2 left
💡 Hint

Think about how Jinja templates can change SQL before it runs.

Predict Output
intermediate
2:00remaining
Output of Jinja variable substitution in SQL

What is the output SQL after rendering this Jinja template?

SELECT * FROM users WHERE country = '{{ country }}';

Given the variable country = 'Canada'.

dbt
country = 'Canada'

from jinja2 import Template

sql_template = "SELECT * FROM users WHERE country = '{{ country }}';"
template = Template(sql_template)
output_sql = template.render(country=country)
print(output_sql)
ASELECT * FROM users WHERE country = 'Canada';
BSELECT * FROM users WHERE country = Canada;
CSELECT * FROM users WHERE country = {{ country }};
DSELECT * FROM users WHERE country = 'country';
Attempts:
2 left
💡 Hint

Remember Jinja replaces variables with their values as strings.

data_output
advanced
2:00remaining
Result of conditional logic in Jinja SQL template

Given this Jinja SQL template:

{% if is_active %}
SELECT * FROM users WHERE active = true;
{% else %}
SELECT * FROM users;
{% endif %}

What SQL is generated if is_active = False?

dbt
from jinja2 import Template

sql_template = """{% if is_active %}
SELECT * FROM users WHERE active = true;
{% else %}
SELECT * FROM users;
{% endif %}"""
template = Template(sql_template)
output_sql = template.render(is_active=False)
print(output_sql.strip())
ASELECT * FROM users;
BSELECT * FROM users WHERE active = true;
CSELECT * FROM users WHERE active = false;
DSyntaxError: invalid syntax
Attempts:
2 left
💡 Hint

Check which block runs when the condition is false.

🔧 Debug
advanced
2:00remaining
Identify the error in this Jinja SQL template

What error will this Jinja SQL template cause when rendered?

SELECT * FROM orders WHERE order_date >= '{{ start_date }}' AND order_date <= '{{ end_date }}'

Given start_date = '2023-01-01' but end_date is not defined.

dbt
from jinja2 import Template

sql_template = "SELECT * FROM orders WHERE order_date >= '{{ start_date }}' AND order_date <= '{{ end_date }}'"
template = Template(sql_template)
try:
    output_sql = template.render(start_date='2023-01-01')
except Exception as e:
    print(type(e).__name__)
ANo error, renders with empty end_date
BKeyError
CTypeError
DUndefinedError
Attempts:
2 left
💡 Hint

What happens if a variable is missing in Jinja rendering?

🚀 Application
expert
3:00remaining
Using Jinja to dynamically generate SQL with loops

What is the output SQL after rendering this Jinja template with columns = ['id', 'name', 'email']?

SELECT
{% for col in columns %}  {{ col }}{% if not loop.last %},{% endif %}
{% endfor %}
FROM users;
dbt
from jinja2 import Template

columns = ['id', 'name', 'email']
sql_template = """SELECT
{% for col in columns %}  {{ col }}{% if not loop.last %},{% endif %}
{% endfor %}
FROM users;"""
template = Template(sql_template)
output_sql = template.render(columns=columns)
print(output_sql.strip())
ASyntaxError: unexpected indent
B
SELECT
  id,
  name,
  email
FROM users;
C
SELECT
id name email
FROM users;
DSELECT id, name, email FROM users;
Attempts:
2 left
💡 Hint

Look at how the loop adds commas except after the last item.