Product Analytics/Style guide

Who should use this guide
This document is a guide for anyone who is working with SQL/HiveQL queries in the product analytics team and the foundation, and would like to write clean and clear code that is meant to be shared and reused. For product analysts, these style conventions are mandatory for any publicly-available query.

General principles

 * Use spaces(2 space indent), or tab characters.
 * All SQL keywords in all caps.
 * Variable names are lowercase, with spaces represented by underscores ( not  ).

statements
Align all columns to the first column on their own line:

goes on its own line:

Always rename aggregates and function-wrapped columns:

Always rename all columns when selecting with table aliases:

Always use  to rename columns:

statements
Only one table should be in the. Never use -joins:

statements
Explicitly use  not just , making multiple lines of  s easier to scan: The   keyword and condition goes on the   line:

statements
Multiple  clauses should go on different lines and begin with the SQL operator:

statements
statements aren't always easy to format but try to align and   together inside   and  :

clause subqueries
Multiple Common Table Expressions (CTEs) should be formatted accordingly: