~7m10:55dbt с нуля: вся база для дата-инженера за 10 минут
Nov 13, 2026
Read: ~7m · You save: 4 min
dbt from Scratch: The Essential Basics for Data Engineers in 10 Minutes
What dbt is and why data engineers need it: models, tests, incremental tables, and snapshots explained with a clear online store example using DuckDB.
dbt is often mentioned in data engineer job postings alongside SQL and Airflow. This article describes how the tool works on a small educational project. All results are taken from the actual output of commands.
The Problem That dbt Solves
Imagine an online store. Every night, orders from the app are copied into a separate analytics database. Another tool performs the copying. At this stage, the data arrives in raw form:
- the data may include test orders;
- amounts are stored in cents;
- dates contain timestamps.
Analysts need a data mart. A data mart is a ready-made clean table for a specific question, for example, how much revenue was earned on a given day.
Without dbt, such a data mart is built with a set of SQL scripts:
- The first script cleans the orders and writes them to an intermediate table.
- The second script calculates revenue based on this table.
- The third script generates the final report.
The scripts are run sequentially, either manually or on a schedule using a third-party platform.
With three scripts, this scheme works reliably. With two hundred, serious problems arise:
- Nobody remembers which script depends on which, especially new team members.
- Renaming a column in the first script breaks five others. Nobody notices the error right away.
- There are no checks. The error is discovered later, for example, when the dashboard shows revenue twice the actual amount.
What is dbt
The abbreviation stands for Data Build Tool. The tool turns a set of SQL scripts into a coherent, structured project.
What dbt does not do
- Data extraction and loading. Other tools handle these tasks, such as Airbyte or dbt Cloud.
- Computation. dbt assembles ready-to-run SQL from the project files and sends it to the database. The database itself performs the calculations.
In the ELT (Extract, Load, Transform) scheme, dbt is responsible only for the last letter, T (Transform). The tool transforms data that has already been loaded into the warehouse.
Adapters
To make dbt work with a specific database, you install an adapter. Supported databases include PostgreSQL, ClickHouse, Greenplum, Snowflake, Spark, and others. The training example uses DuckDB, a compact database contained in a single file on a laptop. It requires no configuration.
Project Structure
A dbt project is a regular folder of files. The root contains:
dbt_project.yml: the project configuration;profiles.yml: the database connection settings.
Two targets are defined in profiles.yml:
- dev: for development. Data is written to the developer's personal schema, for example
dev_ivan. - prod: for production. Data is written to the
analyticsschema, which the dashboards read from.
This setup makes it possible to experiment without risking damage to the data that analysts rely on.
In real projects, profiles.yml files are stored not in the repository but on the local machine or in environment variables, because they contain passwords.
Data Sources
First, dbt specifies where the source data comes from. To do this, a sources file is created, in which the source is described: the orders table in its raw form.
Models
A model is the basic and most important concept in dbt. A model is a file containing a single SELECT query.
The stg_orders Model
The name stg_orders comes from the word staging, meaning an intermediate layer. The model performs three actions:
- Removes the time from the date.
- Converts kopecks to rubles.
- Excludes test orders.
Monetary amounts are stored using the decimal type rather than floating-point numbers. Floating-point numbers can cause rounding errors.
The line from {{ source(...) }} is a Jinja template. Before running, dbt substitutes the real table in its place.
CREATE TABLE or CREATE VIEW commands are not written in the model file. dbt adds them itself. A CREATE VIEW query with a nested SELECT is sent to the database.
Materialization
The way dbt saves a model in the database is called materialization. There are two main options:
| Materialization | What is saved | Behavior |
|---|---|---|
| View | The saved query | Data is not stored; the query runs on every access |
| Table | A table with data | Recreated on every run |
Intermediate models are defined as views. Marts are defined as tables so that dashboards load faster. Materialization is configured with a single line in the project configuration.
The daily_revenue Model
The daily_revenue model shows revenue by day. Its query uses the ref function with the model name instead of a table name.
The ref function performs two tasks:
- Building the dependency graph. dbt determines that revenue depends on the
stg_ordersmodel and automatically builds the execution order. The order does not need to be set manually, whether there are two models or two hundred. - Schema substitution. When running with the dev target, the query accesses
dev_ivan.stg_orders. When running with the prod target, the same model is written to theanalyticsschema. The code does not change.
Running
The entire project runs with a single command: dbt build.
The command output shows the execution order:
- The
stg_ordersview is created. - Tests are run.
- The
daily_revenuetable is built.
The order was not set explicitly. dbt determined it from the ref references.
Result for November 1: two paid orders totaling 2,389 rubles. The test order for 10 rubles and the canceled order were not included in revenue.
Tests
Tests in dbt are data quality checks. They are described in a YAML file next to the model. The training project defines the following rules:
- unique: the order number must be unique;
- not_null: the order number cannot be empty;
- accepted_values: the status must be one of the values in the list:
paidorcanceled; - relationships: each order must have a customer in the customers table.
The fourth built-in test is relationships. In addition to built-in tests, you can write your own check in standard SQL.
How tests work
dbt converts the rules into an SQL query that looks for incorrect rows. For example, a uniqueness test groups data by order number and selects groups with a count greater than one.
If the query returns at least one row, the test is considered failed.
Practical check
A duplicate of order number 6 was added to the raw data, and dbt build was run again. The result:
- the uniqueness test failed and found one incorrect row;
- the
daily_revenuemodel was skipped, and the output showsskip; - the table was not rebuilt and kept yesterday's correct data.
Managers will see data that is one day older, but they will not see double-counted revenue.
That is why tests are placed on intermediate models. They sit in the graph before the marts and prevent errors from reaching the production environment (prod).
Incremental model
The table in the table materialization is fully rebuilt on every run. For six orders, this is acceptable. For a billion rows, this approach is unacceptable.
For large tables, the third materialization is used: incremental:
- First run builds the table in full.
- Subsequent runs select only new rows, starting from the last date already present in the table.
Important details:
- The
>=condition, not>. Orders from the last day may arrive late, so the boundary date is included in the selection. - The unique key is the order number. It prevents row duplication if the same orders arrive again.
- Order of operations. dbt first deletes the old versions of rows with matching keys, then inserts the new ones.
The author verified the result: after a repeated run, the table contained six orders, all unique. In other databases, this operation is often performed with a single command.
Snapshots
Snapshots preserve the history of data changes. Example: customer Anna moved from Moscow to Kazan. In the source table, the city value was simply overwritten, and the old value was lost.
A snapshot solves this problem:
- Each time dbt runs, it compares the data with the previous state.
- If the city has changed, the old row is closed with an end date.
- A new row with the current city is added.
- The current row's end date column is empty.
This mechanism follows the SCD (Slowly Changing Dimensions) approach. You don't need to write it manually.
Documentation
The dbt docs generate command creates a documentation site. For each model, the following are available on it:
- columns and their types;
- model code;
- a dependency graph, including what the table is built from and what depends on it.
Schedule
dbt cannot run on a schedule on its own. For example, Airflow can run it every night.
dbt Core 2.0
Since September 2026, dbt Core 2.0 has been available. Its engine has been completely rewritten in Rust. The basic building blocks (models, ref, tests, and YAML) have remained the same. Everything shown in the material also works in version 2.0.
According to the author, production environments are not in a hurry to move to the second version. The choice between learning 2.0 and 1.x is left to the reader.
Summary
- dbt is not an ETL loader or a database. The tool is responsible only for data transformation.
- Tests catch errors before data reaches the data marts.
- Incremental models save time on large tables.
- Snapshots preserve the history of changes.
- The project is a set of files in Git with Code Review and version history.
The author has published the project from the article. Readers are invited to download it, run dbt build, and check how the tests work.
The Problem That dbt Solves
dbt solves the problem of chaos from hundreds of SQL scripts that depend on each other and are run manually or on a schedule.
- A typical scenario: an online store daily copies orders from its app into an analytics warehouse in raw form (amounts in kopecks, timestamp dates, test data).
- Without dbt, a chain of three scripts (cleaning, revenue calculation, report) quickly becomes unmanageable as it grows to 200 scripts.
- Typical problems: unclear dependencies, breakage of downstream scripts when a column is renamed, lack of checks, errors in dashboards (for example, revenue twice the actual amount).
- DBT stands for Data Build Tool: it turns a set of SQL scripts into an organized project.
What dbt Does and What It Does Not Do
dbt is responsible only for transformation (the T in ELT); extraction and loading are handled by other tools, such as Airbyte. dbt itself computes nothing — it generates SQL, and the database performs the calculations.
- dbt does not extract data from applications or copy it into the warehouse; example tools for this are Airbyte and dbt Cloud (mentioned in the video as external tools).
- In the ELT scheme, dbt is responsible only for the last letter, T — Transform.
- dbt assembles ready-to-run SQL from project files and sends it to the database, which performs the calculations.
- To connect to a specific database, an adapter is installed: Postgres, ClickHouse, Greenplum, Snowflake, Spark, and others.
- The example uses DuckDB — a small file-based database on a laptop that requires no setup.
Project Structure and Connection Profiles
A dbt project is an ordinary folder of files: the project configuration and the description of the database connection, along with the dev and prod targets.
- The root contains the dbt_project.yml file, the project configuration.
- The profiles.yml file describes which database to connect to.
- profiles.yml has two targets: dev writes to a personal schema (for example, Ivan), and prod writes to the analytics schema, which the dashboards read.
- In real work, profiles are usually stored not in the repository but on the local machine or in environment variables, because they contain passwords.
Sources and the first model
Sources describe raw tables, and a model is a file with a single SELECT query. The first model, stg_orders, cleans the data.
- The source (sources) describes the raw orders table.
- A model is the basic and most important concept of dbt: a file with a single select query.
- The stg_orders model (from staging — an intermediate layer) removes the time from the date, converts kopecks to rubles, and excludes test orders.
- Rubles are calculated with the decimal type rather than floating-point, because when working with money, float can produce rounding errors.
- The Jinja template with double curly braces substitutes real tables before running.
- dbt itself adds create view; the database receives create view with a nested select.
Materialization
Materialization determines how dbt stores the model's result: as a view (a saved query without data) or as a table (a physical table recreated on every run).
- View — a saved query that does not store data and executes each time it is accessed.
- Table — a physical table that is recreated on every dbt run.
- Intermediate models are built as views, and marts as tables, so dashboards load faster.
- Materialization is set with a single line in the project configuration.
Dependencies via ref and environments
The ref function links models, builds a dependency graph, determines the execution order, and automatically substitutes the correct schema.
- The daily_revenue model shows revenue by day and uses ref to reference the stg_orders model.
- ref builds the dependency graph and determines the execution order itself, regardless of the number of models.
- When running on dev, the query uses dev_ivan.stg_orders; when running with target prod, the same model goes into the analytics schema.
- The model code does not change when the environment changes.
Run and result
The command dbt build runs models and tests in the correct order. In the example, the mart contains revenue for November 1 from two paid orders.
- The only command to run: dbt build.
- First the view stg_orders is created, then tests are run, and only after that the table daily_revenue is built.
- The order is not set explicitly — dbt infers it from ref.
- For November 1: two paid orders totaling 2,389 rubles.
- The test order for 10 rubles and the cancelled order were not included in revenue.
Tests
Tests (verifications) check data against rules defined in a YAML file. There are four built-in tests: unique, not_null, accepted_values, relationships; you can also write your own in SQL.
- Uniqueness of the order number (unique) and absence of null values (not_null).
- The status must be in the list: paid or canceled (accepted_values).
- The relationships test checks that every order has a customer in the customer table.
- Any check can be written in standard SQL.
- dbt turns the rules into an SQL query that looks for "bad" rows; if the query returns at least one row, the test fails.
- For uniqueness, the query groups by order number and looks for a count greater than one.
Test Failure and Production Protection
When a test fails, the mart is not rebuilt and keeps yesterday's correct data, which protects production from duplicates. That is why tests are written for intermediate models upstream of the marts.
- A duplicate of order number six was added to the raw data, after which dbt build was run.
- The uniqueness test failed and found one bad row.
- The daily_revenue mart was skipped and was not rebuilt.
- Executives will see data for yesterday, but will not see duplicated revenue.
- Tests are placed on intermediate models so that they stop errors before they reach production.
Incremental Models
Incremental materialization updates only new rows, which saves time on large tables. The filter condition and removal of duplicates by order ID are important.
- The table is fully recreated on every run; for billions of rows this is inconvenient.
- An incremental model builds the table entirely on the first run, and on subsequent runs selects fresh rows starting from the last date in the table.
- The condition used is date greater than or equal to (>=), since orders from the last day may arrive late.
- To prevent duplicates, the order ID is specified: dbt first deletes old versions of rows, then inserts new ones.
- After a repeated run, six unique orders remain in the table.
Snapshots
Snapshots preserve the history of data changes: when a record changes, the old version is closed with an end date, and a new row is added.
- Example: client Anna moved from Moscow to Kazan; in the source table, the city was simply overwritten.
- A snapshot compares the data with the previous run each time it runs.
- When the city changes, the old row is closed with an end date, and a new one is added.
- The end-date column of the current row is empty.
- This corresponds to SCD (Slowly Changing Dimensions), but you don't need to write it manually.
Documentation
The command dbt docs generate creates a documentation site with columns, types, code, and a graph of model dependencies.
- Command: dbt docs generate.
- The documentation site shows, for each model, its columns, types, code, and graph.
- You can see what a table is built from and what depends on it.
Scheduling and dbt Core 2.0
dbt cannot run on a schedule by itself; an external orchestrator, such as Airflow, does this. The chapter mentions the release of dbt Core 2.0 and its relation to version 1.x.
- dbt cannot run on a schedule on its own; Airflow can run it every night.
- Since September 2026, dbt Core 2.0 has been released, with its engine completely rewritten in Rust.
- Core concepts (models, ref, tests, YAML) have remained the same.
- In the author's opinion, production environments are in no hurry to switch to the second version.