0
0
dbtdata~5 mins

Macros for reusable SQL logic in dbt - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a macro in dbt?
A macro in dbt is a reusable piece of SQL or Jinja code that you can call in multiple places to avoid repeating yourself.
Click to reveal answer
beginner
How do you define a macro in dbt?
You define a macro inside a .sql file in the macros/ folder using the syntax: {% macro macro_name(args) %} ... {% endmacro %}.
Click to reveal answer
beginner
Why use macros in SQL logic with dbt?
Macros help keep your SQL DRY (Don't Repeat Yourself), make your code easier to maintain, and allow dynamic SQL generation.
Click to reveal answer
beginner
How do you call a macro inside a dbt model?
You call a macro using the syntax: {{ macro_name(arguments) }} inside your SQL model file.
Click to reveal answer
intermediate
Can macros accept parameters? How does this help?
Yes, macros can accept parameters which lets you customize the SQL logic each time you call the macro, making it flexible and reusable.
Click to reveal answer
What folder do you place macros in within a dbt project?
Amacros/
Bmodels/
Cdata/
Dtests/
Which syntax correctly defines a macro in dbt?
A{% macro my_macro() %} ... {% endmacro %}
BCREATE MACRO my_macro() AS ...
Cdef my_macro(): ...
D<macro>my_macro</macro>
How do you insert a macro call inside a SQL model?
Amacro_name()
B[macro_name()]
CCALL macro_name()
D{{ macro_name() }}
What is a key benefit of using macros in dbt?
AIncrease query execution time
BStore data permanently
CAvoid repeating SQL code
DReplace database indexes
Can macros in dbt accept arguments to customize their behavior?
AOnly in models
BYes
COnly in tests
DNo
Explain what a macro is in dbt and why it is useful.
Think about how you can write SQL once and use it many times.
You got /4 concepts.
    Describe how to create and use a macro in a dbt project.
    Consider the steps from writing the macro to calling it in your SQL.
    You got /4 concepts.