Write Modular SQL Code with dbt

Large SQL scripts can be difficult to read, understand, and maintain. With dbt, you can use the ref function to break down a long SQL statement into smaller components.

Consider this example:

Calculate total order amount:

-- models/total_order_amount.sql
SELECT 
    order_id, 
    customer_id, 
    SUM(order_amount) AS total_amount
FROM
    raw_orders
GROUP BY order_id, customer_id

models/average_order_amount.sql:

-- models/average_order_amount.sql
SELECT 
    customer_id, 
    AVG(total_amount) AS avg_amount
FROM {{ ref('total_order_amount') }}
GROUP BY customer_id

Benefits of this approach:

  • Collaboration: Team members can work on separate modules simultaneously.
  • Development: Easier to write and debug smaller, focused components.
  • Maintenance: Update specific modules without affecting the entire query structure.
  • Reusability: Encapsulate common SQL patterns for use across various queries.

By leveraging dbt’s ref function, data teams can transform complex SQL scripts into a series of interconnected, manageable modules. This not only improves code quality but also enhances overall workflow efficiency in data analytics and engineering projects.

Link to dbt.

Scroll to Top

Work with Khuyen Tran

Work with Khuyen Tran