SQL Style Guide

Le Nguyen The Dat
Feb 23, 2017 · 1 min read

— by Dat Le on 23th Feb 2017

Consistent SQL style would help a lot in code review and development process, especially when SQL is the main language we use in the data team at honestbee.

Below are all the rules and conventions:

Formatting

  • All keywords need to be capitalized, it helps with readability, even if no syntax highlighting available.
  • Proper indentation is also important for code readability
  • Table references should always be used when more than one table is referred in a query
  • Avoid using alias unless absolutely necessary, naming should be underscore-separated

JOINS

  • Always specify which JOIN function to use, either LEFT JOIN, RIGHT JOIN,INNER JOIN, and so on…
  • LEFT JOIN should mainly be used when applicable
  • Starts with the deepest level of data (i.e with the most number of rows)

Sub Queries & Aggregations

  • Common Table Expressions (CTEs) or Temporary Tables should be used instead of Sub Queries
  • Aggregation functions should only be used on columns that are necessary

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade