Always format your SQL and stay consistent.

The “SQL” language is pretty flexible as to what it will accept in terms of formatting so as you can imagine you can have queries that are formatted completely differently but are the same.

Formatting your SQL is important because it makes it easier for you to read and others that may need to read your queries. By sticking consistent your brain can make less decisions like “should I capitalize this” or “should this go on a new line”.

There is a pretty good style guide that was created recently that I agree with for the most part www.sqlstyle.guide, however, I will share with you my general guidelines. You can refer to all the examples in this guide as well.

Simple Guidelines

  • 2 spaces for indentation
  • SELECT columns should be on newlines
  • Keywords should be in CAPS, e.g., SELECT, FROM, AS
  • Wrap and indent anything over 80 characters