Skip to content

dbt (data build tool)

dbt (data build tool) turns SQL SELECT queries into tables or views in your data warehouse, runs them in order and tests the results: dbt run builds the models, dbt test checks them.

Select any words here to ask your Maestro about them.

dbt run built fct_sales from stg_orders' 1,000 orders with no errors. How many rows does fct_sales have?

An online store and its dbt project. Sources have a dashed outline. Example data.

What it's for: keeping your data transformations in tidy SQL files, with their dependencies and their tests, instead of loose queries nobody knows what order to run in. It works inside your database or data warehouse, like Snowflake, BigQuery or DuckDB.

How it works: each model is a SELECT in a file. With ref() you say which other model it builds on, and dbt works out the order. By convention, stg_ (staging) models each clean one source, and fct_ (fact) models join them into the table you report from. Tests are like unit tests for your tables: a key doesn't repeat, a column isn't blank, a value is on an allowed list.

A unique test on the key tells you whether the table's grain is what you think. And a reconciliation against another source catches what tests can't see.

Practice it in You already do data engineering, where your checks turn into tests that run on their own.

How it's used

The model

models/fct_sales.sql

  1. marked: select o.order_id, o.amount, c.regionA model is just a SELECT; dbt turns it into a table or a view.
  2. from {{ ref('stg_orders') }} oref() tells dbt which model this one builds on, so it runs them in order.
  3. join {{ ref('stg_customers') }} c using (customer_id)
You don't write CREATE TABLE or the run order: dbt works both out from the ref() calls.

Its tests

models/schema.yml

  1. models:
  2. - name: fct_sales
  3. columns:
  4. - name: order_idThe key: one order per row.
  5. marked: data_tests: [unique, not_null]dbt test checks both and tells you which fail, and on how many rows. Older versions call it tests.
The SQL and its tests live in text files, so you can version and review them like any code.

When to look closer

All tests pass

After changing a filter

  1. $ dbt test
  2. marked: Done. PASS=4 WARN=0 ERROR=0

All four tests pass. What could be wrong with a table that passes them all?

Ask your Maestro about dbt

It answers from the free course page closest to this word, the way it answers while you study.

Example

You: What does "dbt" mean, in plain words?

Your Maestro: I'll explain it with a case of yours: it sticks better than a definition. Which query at work do you rerun by hand every month?

Asking about: Answers from the page "How do I build a dashboard people actually use? Start here"

Only this browser keeps what you ask here. How it works

Learn more

  • SQL

    SQL is the language for asking a database for data: which columns, from which table, which rows and how to summarize them. You get a table back.

  • Database

    A database stores data in connected tables, with rules for each column, so many people and programs can read and change it without stepping on each other.

  • Unit test

    Giving a piece of logic (a formula, a function) an input whose correct output you already know, and checking that it returns that.

  • Grain (of a table)

    What one row of a table stands for: a person, a transaction, a planet, a person per language. Counting rows counts what you want only if you know the grain.

The whole glossary