Menu

Cheat sheet · Sheet 5 of 10

dbt Cheat Sheet

A quick dbt reference: models, ref and source, materializations, incremental models, tests, snapshots and the commands and selectors used to run a project.

  • 2 min read
  • Updated Oct 2026
On this page
  1. Models
  2. Materializations
  3. Incremental model
  4. Tests
  5. Snapshots (SCD Type 2)
  6. Commands

Models

A model is a SELECT in a .sql file under models/. dbt wraps it in the DDL to build a view or table.

-- models/staging/stg_orders.sql
select
    order_id,
    customer_id,
    cast(amount as numeric(12, 2)) as amount,
    order_date
from {{ source('shop', 'raw_orders') }}
-- models/marts/fct_orders.sql
select o.*, c.segment
from {{ ref('stg_orders') }} o
join {{ ref('stg_customers') }} c using (customer_id)

ref() builds the dependency graph and resolves the right schema per environment; source() points at raw tables declared in YAML.

Materializations

Materialization Builds Use for
view A view Light staging models
table A full table rebuild Small to medium marts
incremental Inserts/merges only new rows Large fact tables
ephemeral Inlined CTE, nothing built Reusable logic

Incremental model

{{ config(materialized='incremental', unique_key='order_id') }}

select * from {{ ref('stg_orders') }}
{% if is_incremental() %}
where order_date >= (select max(order_date) from {{ this }})
{% endif %}

With unique_key, reruns update existing rows instead of duplicating them.

Tests

# models/marts/schema.yml
models:
  - name: fct_orders
    columns:
      - name: order_id
        data_tests: [unique, not_null]
      - name: customer_id
        data_tests:
          - relationships: { to: ref('dim_customers'), field: customer_id }
      - name: status
        data_tests:
          - accepted_values: { values: ['placed', 'shipped', 'returned'] }

Snapshots (SCD Type 2)

Snapshots record changes to mutable source rows over time with validity columns, which gives Type 2 history without hand-written merge logic.

Commands

dbt deps                     # install packages
dbt run                      # build models
dbt test                     # run tests
dbt build                    # run + test + seed + snapshot in dependency order
dbt run --select fct_orders+ # a model and everything downstream
dbt run --select +fct_orders # a model and everything upstream
dbt docs generate && dbt docs serve

By Data Career Hub Editorial · Last reviewed Oct 2026 · dbt Core 1.x conventions; examples were not executed against a warehouse

Search
Filter by type