# Transforming Data with dbt

> Writing modular SQL models with ref(), tests, documentation and incremental materialisations using dbt. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/data-analyst/dbt
Last updated: 2026-11-28

Writing modular SQL models with ref(), tests, documentation and incremental materialisations using dbt.

Intermediate · about 10 hours · difficulty 3/5

Learn first: [ETL, ELT and the Modern Data Stack](https://www.internhack.in/roadmaps/tracks/data-analyst/elt-pipelines), [Git and GitHub for Analysts](https://www.internhack.in/roadmaps/tracks/data-analyst/git-basics), [Subqueries and CTEs](https://www.internhack.in/roadmaps/tracks/data-analyst/sql-subqueries-ctes)

dbt (data build tool) lets analysts transform data with plain SQL SELECT statements while adding software practices: dependencies, testing, documentation and version control. It is the core skill of analytics engineering.

### Models and ref()

A model is a `.sql` file containing a SELECT. dbt turns it into a view or table. Models reference each other with `ref()`, which builds the dependency graph:

```
-- models/marts/fct_orders.sql
select o.order_id, o.customer_id, o.ordered_at, p.amount
from {{ ref('stg_orders') }} o
join {{ ref('stg_payments') }} p using (order_id)
```

Raw tables are declared as **sources** and referenced with `source('shop', 'orders')`.

### Materialisations

- **view**: default, always fresh, computed at query time.
- **table**: rebuilt each run, fast to query.
- **incremental**: only processes new rows, for large event tables.
- **ephemeral**: inlined as a CTE.

### Tests and docs

In YAML you declare tests such as `unique`, `not_null`, `accepted_values` and `relationships`, plus column descriptions. `dbt build` runs models and tests together; `dbt docs generate` produces a browsable site with lineage.

### Jinja and macros

Jinja templating adds loops and reusable macros, for example generating a pivot for every payment method. Use sparingly; readable SQL beats clever SQL.

**Pitfall:** putting business logic into staging models or copying the same CASE statement into many models. Centralise it in one intermediate model. You are ready to move on when you can build the dbt Jaffle Shop project with staging and mart models, tests and generated docs.

## Key concepts

- **Models, sources and ref()**
- **Project structure and naming**
- **Materialisations**
- **Generic and singular tests**
- **Documentation and lineage**
- **Incremental models**
- **Jinja and macros**
- **Semantic layer and metrics**

## Mini project

Complete the dbt Jaffle Shop tutorial on DuckDB or BigQuery, then add a customer lifetime value mart with unique and not_null tests and column descriptions.

## Check yourself

What does ref() give you that hard-coding a table name does not?

## Free resources

- [dbt documentation](https://docs.getdbt.com/) (dbt Labs)
- [dbt Learn free courses](https://learn.getdbt.com/) (dbt Labs)

Previous: [ETL, ELT and the Modern Data Stack](https://www.internhack.in/roadmaps/tracks/data-analyst/elt-pipelines) · Next: [Scheduling and Orchestration with Airflow](https://www.internhack.in/roadmaps/tracks/data-analyst/orchestration)

## More in Analytics Engineering

- [ETL, ELT and the Modern Data Stack](https://www.internhack.in/roadmaps/tracks/data-analyst/elt-pipelines)
- [Transforming Data with dbt](https://www.internhack.in/roadmaps/tracks/data-analyst/dbt)
- [Scheduling and Orchestration with Airflow](https://www.internhack.in/roadmaps/tracks/data-analyst/orchestration)
- [Data Quality Testing and Observability](https://www.internhack.in/roadmaps/tracks/data-analyst/data-quality-testing)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Data Analyst Roadmap](https://www.internhack.in/roadmaps/tracks/data-analyst)
