dbt · dbt Fundamentals

source() vs ref()

Easysourcerefraw datalineage

Interview question

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

How to answer

The short answer

Both are Jinja functions that point at upstream data, but they mean different ownership.

source()

Points at raw tables you do not build with dbt (loaded by Fivetran, Airflow, jobs, etc.). You declare them in a YAML sources: block.

ref()

Points at another dbt model. dbt owns that relation and uses ref for the and environment resolution.

SQL
-- staging reads a source
select * from {{ source('stripe', 'payments') }}

-- mart reads a model
select * from {{ ref('stg_payments') }}

Diagram

Output
source(stripe.payments)  -->  stg_payments  -->  fct_payments
      (not dbt-built)         ref(...)            ref(...)

Why declare sources?

  • Central place for database/schema/table names
  • Source freshness checks (dbt source freshness)
  • Documentation and lineage from raw → staging → marts
  • source failures are clearer than opaque hardcoded table strings

Rule of thumb

Never ref a raw landing table you did not create with a dbt model. Never source a mart that dbt builds.

Interview tip: Interviewers love this distinction. Say "source is the boundary with the rest of the platform; ref is how dbt models talk to each other."

Freshness on a source

Declaring a source lets dbt check how stale it is:

YAML
sources:
  - name: stripe
    database: raw
    tables:
      - name: payments
        loaded_at_field: _loaded_at
        freshness:
          warn_after: {count: 12, period: hour}
          error_after: {count: 24, period: hour}

dbt source freshness compares the newest _loaded_at with now. That turns "the dashboard looks old" into a check a scheduler can run before the models. A hard-coded raw.stripe.payments in SQL gives you none of this, and breaks when the environment changes.