Docs
Writing SQL queries

Writing SQL queries

Understand how to write SQL queries for your dbt models.

Reference a source

my_model.sql

_10
select * from {{ source('<source_name>', '<table_name>') }}

Reference a model

my_model.sql

_10
select * from {{ ref('<table_name>') }}

Organize your SQL code with CTEs

To improve the readability of your SQL code, consider using Common Table Expressions (CTEs). CTEs are temporary query results that can be re-used within the broader scope of the entire model.

my_model.sql

_10
with my_cte as (
_10
select * from {{ ref('<table_name>') }}
_10
)
_10
select * from my_cte

FAQ

What SQL dialect should I use for my models?