internalsGet Pro

Module 15 · Lesson 1 of 12Free

dbt: transformations as version-controlled code

Level
Beginner
Topic
Transformations with dbt
Reading time
13 min

When you finish this page you can

  1. Explain the problem dbt solves for SQL transformations inside a warehouse.
  2. Describe what a dbt project holds: models, tests, seeds, a project file and a connection profile.
  3. Trace what dbt build does, in what order, and what it skips when a test fails.
  4. Build and test a two-model dbt project on a laptop with DuckDB.
  1. ETL vs ELT: where the transformation happens13 min
0:00 / 5:16
Show transcript

0:00 Kartly is an online store in India. It takes about 500,000 orders a day. Its data team copies those orders into Snowflake, the warehouse, and turns them into finance tables. Daniel leads finance reporting, and he reads the numbers by seven every morning. Today an analyst adds one new file. It holds a single SQL query. It says nothing about when to run, or what to do if its inputs are wrong. That night, one command runs. It builds the new table after the two tables it needs. And it checks the result before Daniel can read it. So how does one command turn a folder of SQL files into tested tables, in the right order? That’s what dbt does.

0:37 Before dbt, the data engineer, Meera, ran this work from a Python script. The script held a list written by hand. Lookup tables first, then order tables, then summaries. Each step was one SQL file, and the checks were written in Python too. It worked while one engineer owned every file. But as a team grows, it goes weak in three places. First, the order lives in a list, not in the SQL. Add a table, forget to move it in the list, and it quietly reads yesterday’s data. Second, every script invents its own way to pass in the day and the schema name. Third, the checks sit apart from the tables they protect. A new table can ship with no check at all. The SQL is the valuable part. It needs the same care as application code: review, tests and a known build order.

1:16 dbt is a command-line tool for that care. You write each table as one select statement, in its own file. That file is called a model. dbt turns the files into tables and views. Here is what you write, and what dbt adds. You write a select over other models. dbt adds the real table names. You write nothing about storage. dbt adds the create table statement. You write a few lines of YAML naming rules. dbt adds one query per rule, to look for bad rows. And you write nothing about order. dbt works it out for you. So how does dbt know the order? Inside your SQL, you write ref, with a table name in brackets. It means: the table built by that model. dbt swaps in the real name, and it notes an arrow in a graph. The new model refers to fct_orders and to dim_customer. So it gets two arrows in, and it runs after both. A common belief is that dbt moves data. It doesn’t move a single row. dbt reads your project and writes the SQL. It sends that SQL to Snowflake. Snowflake reads the raw data, does the work, and writes the finished tables. dbt only waits for answers. This is the ELT idea: load first, then transform inside the warehouse.

2:19 A dbt project is a folder. At its root is the project file. Next to it is a folder of models, with one SQL file per table. There is also a YAML file for tests, and folders for snapshots and shared pieces of SQL. Kartly keeps this folder in git, next to its Airflow code. Airflow is the tool that schedules the nightly job. Here is the new model in full. It joins two tables, then groups by city and day. Connection details are not in the folder. They live in a separate file, on the machine that runs dbt, and secrets are read from the environment. The project names a profile. The profile says which warehouse and schema to use. So the same code builds a developer’s own schema on a laptop, and the real schema in production. Every change goes through the same pull request: review, an automated build, then merge.

3:01 Kartly’s scheduler, Airflow, starts the nightly job, and its dbt task runs one command: dbt build. First, dbt reads the project and builds the graph. Then it walks the graph. It builds each model in order, and runs that model’s tests straight after it. A test is a query that looks for bad rows. If it finds none, it passed. Models with no path between them can run at the same time. Now suppose a test on fct_orders fails. dbt skips every model below it. The new table is not built from bad input. But one thing matters for Daniel. A failed test does not undo the table it tested. fct_orders has already been replaced. What protects him is the step after dbt. The dbt task fails, so Airflow never runs the finance checks or the publish step. His report stays on the last good day, and says so. The commands dbt run and dbt test still exist on their own. Run, then test, builds every table first and tests afterwards. By then, a wrong number has already spread. That is why Kartly’s nightly job uses dbt build.

3:58 So how should Kartly run its SQL every night? It could keep the Python runner. That works for one person, but a wrong position in the list fails silently. It could use one Airflow task per file. Then every model change becomes a Python change. Or it could use stored procedures inside Snowflake. Then the logic lives in the warehouse, not in git. The best fit is a dbt project, run by one Airflow task. With dbt, the order comes from the SQL itself. The tests sit next to the models. And analysts change the logic through pull requests. Airflow keeps what it’s good at: when to start, what to wait for and who to page. The choice does change with the work. With five files and one engineer, the simple runner is cheaper. And a heavy job that SQL can’t express well stays outside dbt.

4:39 Let’s recap. One: dbt turns a folder of SQL select files into tested tables, in the order their ref calls imply. Two: dbt sends SQL to the warehouse. Snowflake does the work, and no rows pass through dbt. Three: a project is a folder in git, and connection details live outside it. Four: dbt build runs each model and its tests in order, and skips everything below a failure. Five: Airflow decides when the build runs, and dbt decides the order inside it. Next, how models name each other, and the raw data they start from. See you in dbt models, sources and references.

A pull request with one new file

is an online store in India that takes about 500,000 orders a day. Its data team copies orders, payments and partner sales into , the warehouse, and turns them into finance tables. Meera is the data engineer who runs that work. Daniel leads finance reporting and reads the numbers by 07:00 IST every morning.

On Monday 7 February 2028, an analyst on Daniel’s team opens a pull request. It adds one SQL file, daily_sales_by_city.sql: sales per city per day, built from two existing tables, fct_orders and dim_customer. The file is a single select. It doesn’t say when to run, where to write the result or what to do if its inputs are wrong.

That night, Kartly’s nightly job runs one command, dbt build. The new table is built at the right moment, after both of its inputs, and it is checked before Daniel can read it.

The pull request and the daily_sales_by_city table are this lesson’s example.

This lesson answers one question: how does one command turn a folder of SQL files into tested tables, in the right order?

Check yourselfAnswer in your head first, then reveal
Kartly’s build already has many tables. The new file names fct_orders and dim_customer in its SQL and nothing else. Where in the night’s build does it run?Show answerHide answer
After both fct_orders and dim_customer are built and tested, because dbt reads which tables each file selects from and orders the build from that. Not at the end of a list someone maintains: nobody edits an order anywhere.
dbt reads which tables each model selects from and draws this graph itself. The new model slots in after its two inputs.

What the team ran before dbt

When Kartly was smaller, Meera ran the same tables from a Python script (Design: a data model for an online store). It held a hand-written list: dimensions first, then facts, then summaries. Each step was a SQL file, filled in with Python string formatting, followed by checks written in Python.

It worked while one engineer owned every file. It has three weak points as a team grows:

  • The order lives in a list, not in the SQL. Add a table and forget to move it in the list, and it reads yesterday’s input without any error.
  • Templating is home-made. Every script invents its own way to pass the day and the schema name.
  • Checks sit apart from the tables they protect. A new table ships with no check unless someone remembers to write one.

This is the transform step of : raw rows are loaded into the warehouse first, then SQL inside it builds the finished tables (ETL vs ELT). The SQL is the valuable part. What it needs is the discipline that application code gets: , review, tests and a known build order.

dbt writes the SQL around yours; Snowflake does the work

is a command-line tool for that discipline. You write each table as one select statement in its own file, called a . dbt turns the files into tables and views and decides the order they run in 1.

A common belief is that dbt is a pipeline tool that moves data, like a loader. It doesn’t move a single row. dbt compiles your into SQL and sends that SQL to the warehouse 1. Snowflake reads RAW, computes and writes ANALYTICS. dbt only waits for answers.

dbt sends statements and receives results. Rows never leave the warehouse.

Here is what dbt adds to each model before Snowflake sees it:

You write dbt adds Who runs it
A select over other models The real schema and table names dbt, at compile time
Nothing about storage create table, create view and their replacements 1 Snowflake
A few lines of YAML naming rules One query per rule that looks for breaking rows Snowflake
Nothing about order The order, from the graph dbt

Templating is done with , a text template language. In a model, {{ ref('fct_orders') }} means “the table built by the fct_orders model”. dbt replaces it with the real name and records an edge in the graph. dbt models, sources and references covers ref and source properly.

So the warehouse pays for every transform, as ELT intended. What does the folder dbt reads look like?

The project: a folder in git

A dbt project is a folder with a dbt_project.yml file at its root. Kartly’s sits in the same git repository as its Airflow DAGs and reaches /opt/kartly/dbt through Airflow’s Git bundle. Simplified:

Text
/opt/kartly/dbt/
  dbt_project.yml          project name, which profile to use, folder settings
  models/
    dim_date.sql
    dim_product.sql
    dim_customer.sql
    fct_order_lines.sql
    fct_orders.sql
    daily_sales_by_city.sql
    schema.yml             tests and descriptions for the models
  snapshots/               the customer history (type 2 city changes)
  tests/                   one-off checks written as SQL
  macros/                  shared pieces of SQL

The analyst’s new model, in full, compiled for Snowflake:

SQL
select
    o.date_key,
    c.city,
    count(*)          as orders,
    sum(o.amount_inr) as amount_inr
from {{ ref('fct_orders') }} as o
join {{ ref('dim_customer') }} as c
  on c.customer_key = o.customer_key
group by o.date_key, c.city

There are no connection details in the folder. They sit in a separate file, profiles.yml, on the machine that runs dbt, with secrets read from the environment. The project names a profile; the profile says which warehouse, database and schema to use. That split lets the same code build a developer’s own schema on a laptop and KARTLY.ANALYTICS in production.

Everything in the folder goes through the same pull request as the analyst’s file: review, an automated build of the changed models, then merge. Structuring a dbt project and running it in CI covers how teams lay this out.

What dbt build does at 02:25

Kartly’s orchestrator, Airflow, runs the nightly job kartly_nightly from 01:00 IST. Its dbt_build task waits until every source is loaded, or until a 05:30 cutoff, then runs one command (Design: orchestrating a daily reporting pipeline):

Bash
dbt build --project-dir /opt/kartly/dbt --target prod

Read the project

dbt parses every model, test and YAML file and builds the graph from the ref calls.

Walk the graph

It runs , snapshots and models in graph order, and each node’s tests straight after it 2. Nodes with no path between them, such as the three dimensions, can run at the same time.

Test each table as it lands

A is a query that returns the rows breaking a rule. Zero rows means it passed.

Skip what depends on a failure

If a test on fct_orders fails, every model downstream of it is skipped 2. daily_sales_by_city is not built from bad input.

Report and exit

about 02:25 start + 40 min build = about 03:05

dbt writes a record of every node’s result and exits with an error if anything failed. On a normal night, Airflow then runs the finance checks and publishes about 03:25.

The 02:25 start and 40-minute build are the course’s shared timeline from Modules 06 and 14.

One detail matters for Daniel. A failed test does not undo the table it tested: fct_orders has already been replaced when its test fails. What protects him is the step after dbt. The dbt_build task fails, so Airflow never runs the finance checks or the publish step. His report stays on the last good day, labelled as such.

Same bug, two commands. dbt build stops the damage at the failing table; run then test lets it flow downstream first.

dbt run and dbt test still exist on their own. Running them one after the other builds every table first and tests afterwards, so a wrong number has already spread. That is why Kartly’s nightly job uses build.

Choosing how Kartly runs its SQL

How should Kartly run its SQL transformations every night, with more added by analysts each month?

Keep the Python runner with its hand-written order

Works while one person owns every file. Each new table means editing the list, and a wrong position fails silently. Tests are whatever the script author wrote.

One Airflow task per SQL file

Airflow shows each step and retries it on its own. But every model change becomes a DAG change, written in Python, and analysts must wire dependencies by hand. Tests are still separate tasks.

Stored procedures and scheduled SQL inside Snowflake

No extra tool. But the logic lives in the warehouse, not in git, so review and rollback depend on discipline. Moving warehouses means rewriting it.

Why this choice

dbt wins because it puts the order, the tests and the SQL in one reviewed place, and the people who know the business rules can change them safely. Airflow keeps what it is good at: when to start, what to wait for and who to page.

The choice changes with the work. With five files and one engineer, the runner is cheaper to keep. A transform that SQL can’t express well, such as Kartly’s Spark job over every order since launch, stays outside dbt. Results needed within seconds belong to a stream processor, not a nightly build.

Try it yourself

You can build a two-model project on a laptop in about five minutes, with no account. This uses dbt v2, installed from the dbt package on PyPI, which includes DuckDB support 3 4. Use Python 3.12.

Bash
python3.12 -m venv .venv && source .venv/bin/activate
python -m pip install dbt==2.0.11
dbt --version
mkdir -p kartly_lab/models kartly_lab/seeds && cd kartly_lab

Create these files in kartly_lab. The profile points dbt at a local DuckDB file 5:

YAML
# dbt_project.yml
name: kartly_lab
version: "1.0.0"
config-version: 2
profile: kartly_lab
YAML
# profiles.yml
kartly_lab:
  target: dev
  outputs:
    dev:
      type: duckdb
      path: "./kartly_lab.duckdb"
      threads: 4

seeds/raw_orders.csv holds three made-up orders:

Text
order_id,order_date,customer_id,status,amount_inr
1001,2026-11-09,501,delivered,1200
1002,2026-11-09,502,shipped,800
1003,2026-11-09,503,placed,2500

Two models, one built on the other:

SQL
-- models/fct_orders.sql
select order_id, cast(order_date as date) as order_date, customer_id, status, amount_inr
from {{ ref('raw_orders') }}
SQL
-- models/daily_orders.sql
select order_date, count(*) as orders, sum(amount_inr) as amount_inr
from {{ ref('fct_orders') }}
group by order_date

And a test that every order appears once 6:

YAML
# models/schema.yml
models:
  - name: fct_orders
    columns:
      - name: order_id
        data_tests:
          - unique
          - not_null

Run dbt build in the folder. What to look for: the seed loads first, then fct_orders, then its two tests, then daily_orders, with every node passing. dbt worked out that order from the two ref calls.

Now add a fourth line to the CSV, 1003,2026-11-09,503,placed,2500, and run dbt build again.

Check yourselfAnswer in your head first, then reveal
With order 1003 in the CSV twice, what happens to fct_orders and daily_orders?Show answerHide answer
fct_orders is built with four rows, then its unique test fails and daily_orders is skipped. The tempting wrong answer is that fct_orders is left unchanged: the test runs after the table is replaced, so it now holds the duplicate.

Where this shows up

The analyst’s file used ref without explaining it, and Kartly’s raw tables were never declared. Next, how models name each other and the raw data they start from.

Common mistakes

Writing table names instead of ref

A model that selects from ANALYTICS.FCT_ORDERS directly works today. But dbt sees no edge, so it may build the model before fct_orders and read yesterday’s rows.

Running dbt run, then dbt test, in production

Every table is rebuilt before any test runs, so a bad input reaches every table below it. Use dbt build.

Putting connection details in the project

dbt_project.yml and the models go to git. Passwords and keys belong in profiles.yml outside the repository, read from the environment.

Going deeper

dbt v2, dbt Core 1.x and the two v2 packages (as of November 2026)

Check your understanding

Check yourselfAnswer in your head first, then reveal
Meera’s laptop has no Snowflake credentials. Can she still build and test Kartly’s models before opening a pull request?Show answerHide answer
Yes, if her profile points at a schema she is allowed to use, such as her own development schema. The project names a profile, and the profile decides where models are built, so the same SQL runs in her schema and in production.
Check yourselfAnswer in your head first, then reveal
A test on dim_product fails during the nightly dbt build. Which tables are skipped?Show answerHide answer
Every model downstream of dim_product, such as fct_order_lines and anything built on it. dim_customer and dim_date still build, because they don’t depend on it. Not the whole run: dbt skips only the affected branch.
Check yourselfAnswer in your head first, then reveal
A teammate says dbt is slow because it pulls every order line onto the Airflow worker each night. What is wrong with that?Show answerHide answer
dbt never pulls the rows. It sends SQL to Snowflake, which reads and writes the tables itself, so a slow build means slow queries or a small warehouse, not a slow worker.
Check yourselfAnswer in your head first, then reveal
An analyst adds a model that SQL can’t express well: it calls a Python library for each row. Should it go in the dbt project?Show answerHide answer
Probably not as SQL. Kartly already runs heavy non-SQL work, such as its Spark job, outside dbt and loads the result into the warehouse, where dbt models can read it.

Summary

  1. dbt turns a folder of SQL select files into tested tables, in the order their ref calls imply.
  2. dbt compiles the SQL and sends it to the warehouse. Snowflake does the work; no rows pass through dbt.
  3. A project is a git folder: dbt_project.yml, models, tests, snapshots and macros. Connection details live in profiles.yml, outside it.
  4. dbt build runs each node and its tests in graph order and skips everything downstream of a failure. A failed test doesn’t undo the table it tested.
  5. Airflow decides when the build runs; dbt decides the order inside it.

Report a mistake

Tell us what’s wrong in this lesson and we’ll fix it.