SQL Style Guide
1 min readFeb 23, 2017
— 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, eitherLEFT 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