Bird
Raised Fist0
dbtdata~5 mins

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

Choose your learning style10 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.

      Practice

      (1/5)
      1. What is the main purpose of using macros in dbt?
      easy
      A. To create visualizations from data
      B. To store data permanently in the database
      C. To reuse SQL code and make it easier to maintain
      D. To write Python scripts inside dbt

      Solution

      1. Step 1: Understand what macros do

        Macros are reusable pieces of SQL code that help avoid repetition.
      2. Step 2: Identify the main benefit

        Using macros makes SQL cleaner and easier to maintain by reusing code.
      3. Final Answer:

        To reuse SQL code and make it easier to maintain -> Option C
      4. Quick Check:

        Macros = reusable SQL code [OK]
      Hint: Macros = reusable SQL snippets for cleaner code [OK]
      Common Mistakes:
      • Thinking macros store data
      • Confusing macros with visualization tools
      • Assuming macros run Python code
      2. Which of the following is the correct way to define a macro in dbt?
      easy
      A. {% macro my_macro() %} SELECT * FROM table {% endmacro %}
      B. CREATE MACRO my_macro AS SELECT * FROM table;
      C. def my_macro(): SELECT * FROM table
      D. macro my_macro() { SELECT * FROM table }

      Solution

      1. Step 1: Recall dbt macro syntax

        dbt macros use Jinja templating with {% macro %} and {% endmacro %} tags.
      2. Step 2: Match syntax to options

        {% macro my_macro() %} SELECT * FROM table {% endmacro %} correctly uses Jinja syntax for defining a macro.
      3. Final Answer:

        {% macro my_macro() %} SELECT * FROM table {% endmacro %} -> Option A
      4. Quick Check:

        dbt macros use {% macro %} ... {% endmacro %} [OK]
      Hint: Look for {% macro %} and {% endmacro %} tags [OK]
      Common Mistakes:
      • Using SQL CREATE MACRO syntax (not dbt)
      • Writing Python function syntax
      • Using curly braces without Jinja tags
      3. Given this macro definition:
      {% macro select_active_users() %}
      SELECT * FROM users WHERE active = true
      {% endmacro %}

      What will be the output SQL of this dbt model?
      SELECT id, name FROM ({{ select_active_users() }})
      medium
      A. SELECT id, name FROM {{ select_active_users() }}
      B. SELECT id, name FROM users WHERE active = true
      C. Error: Macro call syntax incorrect
      D. SELECT id, name FROM (SELECT * FROM users WHERE active = true)

      Solution

      1. Step 1: Understand macro expansion

        The macro returns the SQL: SELECT * FROM users WHERE active = true.
      2. Step 2: Substitute macro call in model SQL

        The model SQL becomes: SELECT id, name FROM (SELECT * FROM users WHERE active = true)
      3. Final Answer:

        SELECT id, name FROM (SELECT * FROM users WHERE active = true) -> Option D
      4. Quick Check:

        Macro expands SQL inside parentheses [OK]
      Hint: Macros insert SQL code where called [OK]
      Common Mistakes:
      • Forgetting parentheses around macro SQL
      • Thinking macro returns table name only
      • Using macro call without {{ }}
      4. Identify the error in this macro definition:
      {% macro get_orders() %}
      SELECT * FROM orders
      
      medium
      A. Macro name cannot have underscores
      B. Missing {% endmacro %} tag to close the macro
      C. SELECT statement must be inside double quotes
      D. Macro cannot return SELECT statements

      Solution

      1. Step 1: Check macro syntax

        Macros must start with {% macro %} and end with {% endmacro %} tags.
      2. Step 2: Identify incorrect closing tag

        The macro incorrectly uses instead of {% endmacro %}.
      3. Final Answer:

        Missing {% endmacro %} tag to close the macro -> Option B
      4. Quick Check:

        Macro must end with {% endmacro %} [OK]
      Hint: Always close macros with {% endmacro %} [OK]
      Common Mistakes:
      • Using HTML-like closing tags
      • Thinking underscores are invalid
      • Believing SELECT needs quotes in macros
      5. You want to create a macro that accepts a table name and a column name, then returns SQL to count distinct values in that column. Which macro definition correctly achieves this?
      hard
      A. {% macro count_distinct(table, column) %} SELECT COUNT(DISTINCT {{ column }}) FROM {{ table }} {% endmacro %}
      B. {% macro count_distinct(table, column) %} SELECT COUNT(DISTINCT column) FROM table {% endmacro %}
      C. {% macro count_distinct(table, column) %} SELECT COUNT(DISTINCT '{{ column }}') FROM '{{ table }}' {% endmacro %}
      D. {% macro count_distinct(table, column) %} SELECT COUNT(DISTINCT $column) FROM $table {% endmacro %}

      Solution

      1. Step 1: Understand macro parameters usage

        Parameters must be inserted with {{ }} to inject SQL identifiers.
      2. Step 2: Check each option for correct syntax

        {% macro count_distinct(table, column) %} SELECT COUNT(DISTINCT {{ column }}) FROM {{ table }} {% endmacro %} correctly uses {{ column }} and {{ table }} without quotes, so SQL identifiers are injected properly.
      3. Final Answer:

        {% macro count_distinct(table, column) %} SELECT COUNT(DISTINCT {{ column }}) FROM {{ table }} {% endmacro %} -> Option A
      4. Quick Check:

        Use {{ }} to insert parameters as SQL code [OK]
      Hint: Use {{ }} to insert macro parameters in SQL [OK]
      Common Mistakes:
      • Putting parameters inside quotes (makes them strings)
      • Using $variable syntax (not Jinja)
      • Not wrapping parameters with {{ }}
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard