Skip to content

dbt (Data Build Tool)

Key idea:

dbt — a tool for transforming data in a warehouse via SQL. Paradigm: define models as SQL select statements, dbt compiles the DAG, materialises into tables/views, runs tests, generates docs. The core of what is called the "modern data stack". Open-source dbt-core + SaaS dbt Cloud. Used by: Airbnb, Monzo, HelloFresh, thousands of startup data teams.

Below: details, example, related terms, FAQ.

Check your site →

Details

  • Models: .sql files, one per table/view
  • ref(): lineage-aware table references → auto DAG
  • Materialisations: view / table / incremental / snapshot
  • Tests: not_null, unique, accepted_values, custom
  • Docs: auto-generated with column descriptions + lineage graph
  • Adapters: Snowflake, BigQuery, Redshift, Postgres, DuckDB, ClickHouse

Example

-- models/orders_summary.sql
{{ config(materialized='table') }}

SELECT
  DATE_TRUNC('day', order_date) AS day,
  COUNT(*) AS orders,
  SUM(amount) AS revenue
FROM {{ ref('orders') }}
WHERE status = 'completed'
GROUP BY 1

-- schema.yml
models:
  - name: orders_summary
    columns:
      - name: day
        tests: [not_null, unique]

Related Terms

TL;DR

dbt (Data Build Tool) is an open-source command-line tool that enables data analysts and engineers to transform raw data into a structured format for analysis, leveraging SQL. It facilitates data transformation workflows through modular SQL files, version control, and a robust testing framework, making it essential for modern data analytics pipelines.

Understanding dbt: Core Concepts and Functionality

dbt (Data Build Tool) serves as a powerful framework for transforming and modeling data within a data warehouse. It operates primarily on SQL and integrates seamlessly with various data platforms such as Snowflake, BigQuery, and Redshift. The essence of dbt lies in its ability to manage data transformation workflows efficiently, allowing data practitioners to focus on analysis rather than data engineering.

At its core, dbt uses a straightforward command-line interface (CLI) that allows users to execute a series of SQL transformations defined in .sql files. These files are organized into models, which represent the transformed data tables or views. Each model is defined by a SQL query that can reference other models, facilitating a modular and reusable architecture.

Key concepts in dbt include:

  • Models: SQL files that define transformations. Each model can be materialized as a table or view in the data warehouse.
  • Seeds: CSV files that dbt can load into the warehouse as tables.
  • Snapshots: Mechanism to capture historical changes in your data over time.
  • Tests: Built-in testing framework to ensure data quality and integrity.

dbt's configuration allows for different materialization strategies, which dictate how and when data is written to the warehouse. For example, the table materialization creates a table every time the model is run, while the incremental materialization updates only new or changed records, thus optimizing performance and resource usage.

Practical Example: Setting Up a dbt Project

To illustrate the usage of dbt, let’s walk through the process of setting up a simple dbt project that transforms a raw sales dataset into a structured format suitable for reporting.

1. Installation: First, ensure you have Python and pip installed, then run:

pip install dbt

2. Create a new dbt project: Use the following command to initialize a new project:

dbt init my_dbt_project

This command creates a directory structure for your dbt project, including folders for models, seeds, and tests.

3. Configure your dbt profile: Edit the profiles.yml file to include connection details for your data warehouse. Here’s an example configuration for Snowflake:

my_profile:
  target: dev
  outputs:
    dev:
      type: snowflake
      account: your_account
      user: your_user
      password: your_password
      role: your_role
      database: your_database
      warehouse: your_warehouse
      schema: your_schema

4. Create a model: In the models directory, create a new SQL file named sales_summary.sql with the following content:

SELECT
  customer_id,
  SUM(amount) AS total_sales
FROM
  {{ ref('raw_sales') }}
GROUP BY
  customer_id

In this example, raw_sales represents another model or table in your data warehouse. The ref function creates a dependency, ensuring that dbt builds the raw_sales model before sales_summary.

5. Run your dbt project: Execute the following command to compile and run your dbt models:

dbt run

This command will execute all models in your project, creating the necessary tables or views in your data warehouse. You can also run tests using:

dbt test

By following these steps, you've successfully set up a basic dbt project that transforms raw sales data into a summary format, showcasing dbt's capabilities in managing data transformation workflows.

Learn more

Frequently Asked Questions

dbt Core vs Cloud?

Core: free, CLI, self-host. Cloud: SaaS + web IDE + scheduling + docs hosting + CI/CD, $100-200/dev/mo. For small teams — core + Airflow; enterprise — Cloud.

Alternatives?

SQLMesh (newer, Python-based), Apache Airflow tasks, Dataform (Google). For non-SQL ELT: Fivetran/Airbyte + Python.

Incremental models?

materialized="incremental" + unique_key — dbt detects changed rows, runs INSERT/UPDATE only for them. Huge cost savings vs full refresh.

Try the live tool that powered this guide

Free plan — 10 monitors, checks every 5 min, no card required. Upgrade for 1-minute interval and multi-region monitoring.