Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
dbt Labs · dbt Analytics Engineering

Sources, ref, and the DAG

dbt Analytics Engineering · Lesson 1 of 17 · 3:28

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the dbt Analytics Engineering Certification exam. In this lesson: declaring sources in YAML and reading them with the source function, how ref sets the schema for each environment and records the dependencies of the DAG, checking which models depend on a raw table (lineage graph and source selector), and a clean DAG from sources to staging, intermediate models, and marts, including refactoring legacy SQL. Includes three original practice questions, with Orchardline Meal Kits, a fictional meal-kit company, as the example.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Hard-coded tables, unknown lineage

Orchardline Meal Kits delivers meal kits every week, and an extract and load tool copies its orders, subscriptions, and deliveries into a raw schema of the warehouse. Its first dbt models were pasted from old queries, with hard coded table names, and nobody can tell which raw tables feed which report. This lesson covers sources, the ref function, the DAG, and how to verify every raw dependency.

Sources

A source declares a raw table that something other than dbt loads, in a YAML file under the sources key. Each source has a name, a database, a schema, and a list of tables. In a model, the source function takes the source name and the table name, and compiles to the full name of the table in the warehouse. If you leave out the schema, dbt uses the source name as the schema name. And when the warehouse names are messy, the identifier property maps a clean table name in dbt to the real name in the warehouse.

ref and the DAG

Models read other models with the ref function, which does two things. First, it puts in the right schema for the environment, so the same code builds in your development schema and in production. Second, it records a dependency, and dbt assembles all of these dependencies into the DAG, a directed acyclic graph that sets the order in which models run. A table name typed by hand creates no dependency at all, so dbt may build a model before its parent, and a development run may read production data. The ref function also accepts a project or package name as a first argument, and a version argument for versioned models, which you will meet in lesson ten.

Verifying raw dependencies

To see which models depend on a raw table, open the lineage graph in the generated documentation, where every source appears as its own node. On the command line, the source selector does the same: followed by a plus sign, it picks everything downstream of that source. Data tests declared on a source check the raw data itself, before any model uses it. Source freshness, in lesson sixteen, checks that new data keeps arriving. And when a model suddenly reads the wrong data, check that the database, schema, and identifier of its source still match the real table.

A clean DAG

A clean DAG flows in one direction, from sources to staging, then intermediate models, then marts. Each raw table is read once, by its staging model, which is the only place that calls the source function. Intermediate models and marts only use ref, so a change in a raw table is absorbed in one place. When you move legacy SQL into dbt, build the new models alongside the old query, and start each model with import CTEs that select from sources or refs, then logical CTEs, and a final CTE. Before you switch reports over, compare the old and new results, for example with the audit helper package.

Recap

Let's recap. Sources declare raw tables in YAML, and the source function compiles to their full name. The schema defaults to the source name, and the identifier maps a messy table name. The ref function sets the right schema for each environment, and tracks the dependencies that form the DAG. The lineage graph and the source selector show what depends on each raw table. And in a clean DAG, only staging models call the source function.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. Orchardline declares a source named orchard_app, with a database, but no schema property. Which schema does dbt read its tables from?

    • The schema of the current target
    • A schema named raw, the default for sources
    • A schema named orchard_app, like the source
    • None: dbt stops, since schema is required
    Show the answer

    A schema named orchard_app, like the source

    When the schema is left out, dbt uses the source name as the schema name. “The schema of the current target” is wrong, since the target schema is where dbt builds models, not where it reads raw tables. “A schema named raw, the default for sources” is wrong, since dbt has no default raw schema. And “None: dbt stops, since schema is required” is wrong, since the schema property is optional.

  2. A developer builds the customers mart in a development schema, but it shows production numbers. Its code selects from analytics.stg_orchard_app__orders, typed by hand. What explains it?

    • The staging model must be a table in development
    • The typed name always points at the production schema
    • dbt reads production for any model not selected in the run
    • The orchard_app source has no schema property
    Show the answer

    The typed name always points at the production schema

    A typed table name skips ref, so dbt neither swaps in the development schema nor tracks the dependency. “The staging model must be a table in development” is wrong, since the materialization does not change which schema a typed name points to. “dbt reads production for any model not selected in the run” is wrong, since dbt does not read production models on its own, that only happens with defer, when you ask for it. And “The orchard_app source has no schema property” is wrong, since this model does not call the source function at all.

  3. The subscriptions mart calls source() on the raw subscriptions table and renames its columns. Three other marts repeat the same renaming. What should change?

    • Add a staging model on the source and ref it in every mart
    • Materialize the subscriptions mart as ephemeral
    • Move the renaming into a macro that each mart calls
    • Move the raw subscriptions table into the seeds folder
    Show the answer

    Add a staging model on the source and ref it in every mart

    A staging model renames the columns once, every mart reads it with ref, and the source function stays in the staging layer. “Materialize the subscriptions mart as ephemeral” is wrong, since an ephemeral mart still reads the raw table, and is not even built for reports. “Move the renaming into a macro that each mart calls” would only share the renaming, while four marts would still read the raw table directly. And “Move the raw subscriptions table into the seeds folder” is wrong, since seeds are small files kept in the project, not tables loaded by another tool.