๐Ÿ“ŠData Engineering

dbt

Data Build Tool

A SQL-based data transformation tool. Handles the T (Transform) in ELT. Supports modularity and testing.

Detailed Explanation

dbt (Data Build Tool) is a SQL-based data transformation framework for analytics engineering. It handles the Transform step in the ELT (Extract-Load-Transform) pattern, and automates the creation of tables/views when you simply write SELECT statements. It applies software engineering best practices (version control, testing, documentation) to data transformation.

dbt's core feature is managing dependencies between models through the ref() function. When you reference another model, as in ref('stg_orders'), dbt automatically calculates the execution order (DAG) and substitutes the schema. The source() function provides references to source data and freshness checks.

Jinja templates and macros let you reuse SQL like a programming language, and dbt test validates data quality (unique, not_null, relationships, etc.). dbt docs generate produces automatic documentation and a data lineage graph.

dbt Cloud is a SaaS that provides scheduling, CI/CD, and an IDE, while dbt Core is a CLI open-source tool. It integrates with major data warehouses such as Snowflake, BigQuery, Redshift, and Databricks, and has become a standard tool for the Analytics Engineering job function.

Code Example

-- dbt ๋ชจ๋ธ ์˜ˆ์ œ: ref()์™€ source() ํ™œ์šฉ

-- ========== models/staging/stg_orders.sql ==========
-- source()๋กœ ์›์ฒœ ๋ฐ์ดํ„ฐ ์ฐธ์กฐ
{{ config(materialized='view') }}

SELECT
    id AS order_id,
    user_id AS customer_id,
    order_date,
    status,
    amount AS order_amount,
    _loaded_at
FROM {{ source('raw', 'orders') }}
WHERE _loaded_at > CURRENT_DATE - INTERVAL '90 days'

-- ========== models/staging/stg_customers.sql ==========
{{ config(materialized='view') }}

SELECT
    id AS customer_id,
    name AS customer_name,
    email,
    created_at AS signup_date,
    segment
FROM {{ source('raw', 'customers') }}

-- ========== models/marts/fct_orders.sql ==========
-- ref()๋กœ ๋‹ค๋ฅธ ๋ชจ๋ธ ์ฐธ์กฐ (์˜์กด์„ฑ ์ž๋™ ๊ด€๋ฆฌ)
{{ config(
    materialized='incremental',
    unique_key='order_id',
    on_schema_change='sync_all_columns'
) }}

WITH orders AS (
    SELECT * FROM {{ ref('stg_orders') }}
    {% if is_incremental() %}
    WHERE order_date > (SELECT MAX(order_date) FROM {{ this }})
    {% endif %}
),

customers AS (
    SELECT * FROM {{ ref('stg_customers') }}
)

SELECT
    o.order_id,
    o.customer_id,
    c.customer_name,
    c.segment AS customer_segment,
    o.order_date,
    o.status,
    o.order_amount,
    CURRENT_TIMESTAMP AS _updated_at
FROM orders o
LEFT JOIN customers c ON o.customer_id = c.customer_id

How It's Said in Practice

Senior: "If you build this aggregation logic as a dbt model and reference it with ref(), you can reuse it. Right now the same logic is copy-pasted in 5 places, so changing it in one place will update everything."

Junior: "If I build it as an incremental model, don't I have to reprocess everything?"

Senior: "Right, the is_incremental() condition processes only new data."

Interviewer: "What is the difference between ref() and source() in dbt?"

Candidate: "source() references source data (raw layer) and enables freshness checks. ref() references other dbt models and automatically builds the dependency graph (DAG)."

Take a mock interview on this topic โ†’ WIA SOOM AI Interview ยท Start for free

Senior: "This model has no tests. Please add at least unique and not_null tests on the PK, and a relationships test on the FK."

Junior: "I'll add a tests section to schema.yml."

Cautions

Learn More