dbt Analytics Engineering (50 Qs)•Part 1: dbt Core Concepts & Fundamentals (Junior / Entry-Level)
What is the `ref()` function and why is it revolutionary?
junior LevelCode ExampleVue & Nuxt
In Simple Words:
In traditional SQL pipelines, queries hardcode physical table names (SELECT * FROM analytics_prod.dim_customers). If you switch to a development schema, your queries break or accidentally overwrite production!
ref('model_name') is dbt's dependency resolution function:
- Builds the DAG (Directed Acyclic Graph): dbt reads all
ref()calls and automatically determines the exact order in which models must be built. - Environment Agnostic: In development,
ref('stg_orders')compiles todev_manth.stg_orders. In production, it compiles toanalytics.stg_orderswithout changing a single line of SQL!
sql
-- models/marts/fct_orders.sql
SELECT
o.order_id,
o.customer_id,
c.customer_name,
o.order_total
FROM {{ ref('stg_orders') }} o
JOIN {{ ref('dim_customers') }} c ON o.customer_id = c.customer_id