0
0
dbtdata~5 mins

Why models are the core of dbt

Choose your learning style9 modes available
Introduction

Models in dbt are like recipes that tell the computer how to prepare your data. They help you organize and transform raw data into useful information.

When you want to clean and organize raw data from different sources.
When you need to create new tables or views that combine or summarize data.
When you want to make your data easy to understand and use for reports.
When you want to track how data changes step-by-step in your project.
When you want to reuse data transformations without repeating code.
Syntax
dbt
select
  column1,
  column2,
  aggregate_function(column3) as new_column
from source_table
where condition
group by column1, column2
A model in dbt is usually a SQL file that contains a select statement.
dbt runs these models to create tables or views in your database.
Examples
This simple model selects all data from the raw_customers table.
dbt
select * from raw_customers
This model counts how many orders each customer made.
dbt
select
  customer_id,
  count(order_id) as total_orders
from raw_orders
group by customer_id
This model sums payments made by each customer since the start of 2024.
dbt
select
  customer_id,
  sum(amount) as total_spent
from raw_payments
where payment_date >= '2024-01-01'
group by customer_id
Sample Program

This dbt model creates a table showing how many orders each customer has made.

dbt
-- models/customer_orders.sql
select
  customer_id,
  count(order_id) as order_count
from raw_orders
group by customer_id
OutputSuccess
Important Notes

Models help you build your data step-by-step, making it easier to understand and maintain.

dbt automatically manages the order of models based on dependencies.

Using models means you write SQL once and reuse it everywhere.

Summary

Models are the main way to transform raw data into useful tables or views.

They organize your data work clearly and make it easy to update.

dbt runs models to build your data project in the right order.