SQL Style Guide

I recently had to learn the fundamentals of DBT and how to create a useful data pipeline. While reading up on DBT, I also decided to read up on some SQL best practices. This document compiles the rules that I’ve found to be the most useful, with some additional rules that fit my coding style.


Guiding Principle

Optimize primarily for readability, maintainability, and robustness rather than for fewer lines of code.

Newlines are cheap; people’s time is expensive.

People looove this quote.

General Guidelines

Naming conventions

Common Table Expressions (CTEs)

Sources:

  1. https://about.gitlab.com/handbook/business-technology/data-team/platform/sql-style-guide/
  2. https://github.com/brooklyn-data/co/blob/main/sql_style_guide.md#optimize-primarily-for-readability-maintainability-and-robustness-rather-than-for-fewer-lines-of-code
  3. https://handbook.meltano.com/data-team/sql-style-guide
  4. https://gist.github.com/fredbenenson/7bb92718e19138c20591
  5. https://docs.getdbt.com/guides/best-practices/how-we-style/0-how-we-style-our-dbt-projects
  6. https://github.com/mattm/sql-style-guide