From the course: Data Engineering: dbt for SQL
Unlock this course with a free trial
Join today to access over 24,200 courses taught by industry experts.
CTEs in dbt
From the course: Data Engineering: dbt for SQL
CTEs in dbt
- [Instructor] Let's get hands-on using CTEs in dbt. In order to use a CTE, you'll need to define it with your dbt model. CTEs are defined using the WITH keyword, followed by the CTE name and the query that generates the results set. The syntax looks as you can see on screen. WITH the name of your CTE AS SELECT column1, column2, so on and so forth, FROM a source_table WHERE condition. You can see that this looks very similar to a normal SELECT statement in SQL, just with the additional WITH syntax. This is why I think it's helpful to think of CTEs as functions. By designing the CTE and assigning it a name, you can treat it as a function that generates a table. Once you've defined the CTE, you'll want to use it. To use a CTE, you can reference it in the main query of your dbt model and simply include its name in the main query as if it were a regular table. Let's make this more concrete. Take a look at the following…