![rw-book-cover](https://opengraph.githubassets.com/e1eb19cfb296c1dca13d51d6bf23300339f1968865cd32607fb458072c3dbbd0/dbt-labs/corp) ## Metadata - Author: [[github|Github]] - Full Title:: dbt Style Guide - Category:: #🗞️Articles - Document Tags:: [[DBT|Dbt]], [[sql|Sql]], - URL:: https://github.com/dbt-labs/corp/blob/main/dbt_style_guide.md - Finished date:: [[2023-07-22]] ## Highlights > • Where performance permits, CTEs should perform a single, logical unit of work. > • CTE names should be as verbose as needed to convey what they do. > • CTEs with confusing or noteable logic should be commented with SQL comments as you would with any complex functions, and should be located above the CTE. > • CTEs that are duplicated across models should be pulled out and created as their own models. ([View Highlight](https://read.readwise.io/read/01h5yajbh55rb4wgh9qhfrgq7e)) > Where applicable, opt for filtering within import CTEs over filtering within logical CTEs. This allows a developer to easily see which data contributes to the end result ([View Highlight](https://read.readwise.io/read/01h5yajg881ts8rt2xc6v26d4h)) > Aggregations should be executed as early as possible before joining to another table. ([View Highlight](https://read.readwise.io/read/01h5yarcmwgvdf3g9vdv2nzt0c)) > Ordering and grouping by a number (eg. group by 1, 2) is preferred over listing the column names (see [this rant](https://blog.getdbt.com/write-better-sql-a-defense-of-group-by-1/) for why). Note that if you are grouping by more than a few columns, it may be worth revisiting your model design. If you really need to, the [dbt_utils.group_by](https://github.com/dbt-labs/dbt-utils/tree/0.8.6/macros/sql/groupby.sql) function may come in handy. ([View Highlight](https://read.readwise.io/read/01h5yasah99tq7n3fqy7dbtmq1)) > Joins should list the left table first (i.e., the table you're joining data to) ([View Highlight](https://read.readwise.io/read/01h5yasp1djzr39fwh86f4cajm))