0
0
dbtdata~5 mins

Creating your first model in dbt

Choose your learning style9 modes available
Introduction

Creating a model in dbt helps you turn raw data into clean, organized tables. This makes it easier to analyze and understand your data.

You want to clean and organize raw data from your database.
You need to create a reusable table for reports or dashboards.
You want to apply business rules to your data in a simple way.
You want to automate data transformations to save time.
You want to track changes and versions of your data transformations.
Syntax
dbt
select
  column1,
  column2,
  aggregate_function(column3) as new_column
from
  source_table
where
  condition

This is a basic SQL SELECT statement used inside a dbt model file.

Each model is a SQL file that creates a table or view in your data warehouse.

Examples
This model selects all columns from the raw customers table.
dbt
select * from raw.customers
This model counts completed orders per customer.
dbt
select
  customer_id,
  count(order_id) as total_orders
from raw.orders
where order_status = 'completed'
group by customer_id
This model converts customer names to uppercase.
dbt
select
  customer_id,
  upper(customer_name) as customer_name_upper
from raw.customers
Sample Program

This simple dbt model selects active users from the raw.users table.

Save this SQL in your dbt project's models folder as my_first_model.sql. When you run dbt run, it will create a table or view with this data.

dbt
-- File: models/my_first_model.sql
select
  id,
  name,
  created_at
from raw.users
where active = true
OutputSuccess
Important Notes

Model files must be saved in the models folder of your dbt project.

Run dbt run to build your models and create tables or views in your data warehouse.

Use simple SQL first, then add complexity as you learn.

Summary

dbt models are SQL files that transform raw data into clean tables.

Save your SQL code in the models folder and run dbt run to create the model.

Start with simple SELECT statements to build your first model.