SpinTheMic
Pro

dbt · Beginner

Why do we use ref() instead of hard-coded table names in dbt?

By Anurag Pandey, Lead Data Engineer ·

The short answer

ref() tells dbt that one model depends on another. dbt uses it to build models in the right order, to point at the right schema for each environment, and to draw the lineage graph.

Practice this out loud

Opens this question on SpinTheMic. Start without signing up; sign in free for an AI report card.

Say it like this

About 50 seconds out loud. Use it as a model, then put it in your own words.

When I write select from ref('stg_orders'), I'm doing three things a hard-coded table name can't. First, dbt learns that my model depends on stg_orders, so it builds them in the right order and can run just a model and its parents or children. Second, ref resolves to the correct database and schema for the environment I'm running in. In development it points at my dev schema, in production at the production schema, without changing any SQL. Third, those dependencies make up the lineage graph in dbt docs, so anyone can see where data comes from. If I hard-code analytics.stg_orders, my dev run reads production data and dbt doesn't know the dependency exists. For raw tables that dbt doesn't build, I use source() instead.

What interviewers look for

  • You mention dependency order, environment-aware names and lineage.
  • You explain what goes wrong with hard-coded names in development.
  • You know source() is the equivalent for raw tables.

Common mistakes

  • Saying ref is just a shortcut for typing the schema.
  • Using ref for raw source tables instead of source().
  • Not knowing that ref enables node selection like +model or model+.

Follow-up questions to expect

  1. 1What does the + in dbt run --select +orders mean?
  2. 2How does dbt decide which schema a model is built in?
  3. 3What is a cross-project ref in dbt Mesh?
Practice this out loud

Opens this question on SpinTheMic. Start without signing up; sign in free for an AI report card.

Checked against

All interview question guides