From 8dfd10336ba369b8c76c1a0a4ca1aa48fa1d7e8d Mon Sep 17 00:00:00 2001 From: "apurvis@lumoslabs.com" Date: Wed, 25 May 2016 07:26:20 +0800 Subject: [PATCH 1/3] Indentation rules --- _includes/sqlstyle.guide.md | 69 +++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/_includes/sqlstyle.guide.md b/_includes/sqlstyle.guide.md index 971c2f4..3fa10ad 100644 --- a/_includes/sqlstyle.guide.md +++ b/_includes/sqlstyle.guide.md @@ -5,7 +5,7 @@ These are guidelines to help you write SQL queries that will be easier to read. Remember that even if you hate a given style at first, generally speaking it is -far more important that we have _any_ agreed upon style than that we all like it, +far more important that we have _any_ agreed upon style than that we all like it, and furthermore the odds are that you will eventually learn to like it once you start to follow it. @@ -163,7 +163,10 @@ Although not exhaustive always include spaces: comma or semicolon. ```sql -SELECT a.title, a.release_date, a.recording_date +SELECT + a.title, + a.release_date, + a.recording_date FROM albums AS a WHERE a.title = 'Charcoal Lane' OR a.title = 'The New Danger'; @@ -199,8 +202,8 @@ WHERE title = 'The New Danger'; ```sql SELECT a.title, - a.release_date, - a.recording_date, + a.release_date, + a.recording_date, a.production_date FROM albums AS a WHERE a.title = 'Charcoal Lane' @@ -213,14 +216,24 @@ To ensure that SQL is readable it is important that standards of indentation are followed. **ONLY** the fundamental keywords - `SELECT`, `FROM`, `WHERE`, `GROUP BY`, `HAVING`, `LIMIT`, -and `ORDER BY`should be fully left justified. Other clauses should be indented to the end of -that keyword. +and `ORDER BY`should be fully left justified. + +For single `SELECT`s, you can use the single line form: ```sql -SELECT first_name, - last_name, - is_still_tippin_on_four_fours, - is_still_wrapped_in_four_vogues +SELECT first_name +FROM rappers +``` + +If you are `SELECT`ing more than one column, place all selects on their own line indented 2 +spaces in a block after the `SELECT` keyword. + +```sql +SELECT + first_name, + last_name, + is_still_tippin_on_four_fours, + is_still_wrapped_in_four_vogues FROM rappers WHERE first_name = 'Mike' AND last_name = 'Jones' @@ -228,6 +241,7 @@ WHERE first_name = 'Mike' This allows the reader to quickly scan for the important building blocks of the query. + #### Joins Joins should be indented 2 spaces right from the `FROM` keyword @@ -277,27 +291,30 @@ FROM my_tmp_table #### Sub-queries -In PostgreSQL you should probably be doing subqueries with `WITH` clauses. +In PostgreSQL you should be doing subqueries with `WITH` clauses and avoiding the use of inline +subqueries. -Sub-queries should be left aligned 2 spaces to the right of the opening parentheses and then -laid out using the same style as a `WITH` statement w/r/t parentheses. +In MySQL or other query engines that do not support `WITH`, sub-queries should be left aligned 2 +spaces to the right of the opening parentheses and then laid out using the same style as a `WITH` +statement w/r/t parentheses. ```sql -SELECT r.last_name, - ( - SELECT MAX(YEAR(championship_date)) - FROM champions AS c - WHERE c.last_name = r.last_name - AND c.confirmed = 'Y' - ) AS last_championship_year +SELECT + r.last_name, + ( + SELECT MAX(YEAR(championship_date)) + FROM champions AS c + WHERE c.last_name = r.last_name + AND c.confirmed = 'Y' + ) AS last_championship_year FROM riders AS r WHERE r.last_name IN - ( - SELECT c.last_name - FROM champions AS c - WHERE YEAR(championship_date) > '2008' - AND c.confirmed = 'Y' - ) + ( + SELECT c.last_name + FROM champions AS c + WHERE YEAR(championship_date) > '2008' + AND c.confirmed = 'Y' + ) ``` #### Case statements (PostreSQL) From ed47fa6f6eae41f7dffc2c367054222e31e7ce3e Mon Sep 17 00:00:00 2001 From: "apurvis@lumoslabs.com" Date: Wed, 25 May 2016 07:28:18 +0800 Subject: [PATCH 2/3] Indentation rules --- _includes/sqlstyle.guide.md | 72 ++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/_includes/sqlstyle.guide.md b/_includes/sqlstyle.guide.md index 3fa10ad..80cffe1 100644 --- a/_includes/sqlstyle.guide.md +++ b/_includes/sqlstyle.guide.md @@ -153,6 +153,37 @@ WHERE p.release_date > '2014-09-30'; To make the code easier to read it is important that the correct amount of spacing is used. Do not crowd code or remove natural language spaces. +### Indentation + +To ensure that SQL is readable it is important that standards of indentation +are followed. + +**ONLY** the fundamental keywords - `SELECT`, `FROM`, `WHERE`, `GROUP BY`, `HAVING`, `LIMIT`, +and `ORDER BY`should be fully left justified. + +For single `SELECT`s, you can use the single line form: + +```sql +SELECT first_name +FROM rappers +``` + +If you are `SELECT`ing more than one column, place all selects on their own line indented 2 +spaces in a block after the `SELECT` keyword. + +```sql +SELECT + first_name, + last_name, + is_still_tippin_on_four_fours, + is_still_wrapped_in_four_vogues +FROM rappers +WHERE first_name = 'Mike' + AND last_name = 'Jones' +``` + +This allows the reader to quickly scan for the important building blocks of the query. + #### Spaces Although not exhaustive always include spaces: @@ -201,47 +232,16 @@ WHERE title = 'The New Danger'; ``` ```sql -SELECT a.title, - a.release_date, - a.recording_date, - a.production_date +SELECT + a.title, + a.release_date, + a.recording_date, + a.production_date FROM albums AS a WHERE a.title = 'Charcoal Lane' OR a.title = 'The New Danger'; ``` -### Indentation - -To ensure that SQL is readable it is important that standards of indentation -are followed. - -**ONLY** the fundamental keywords - `SELECT`, `FROM`, `WHERE`, `GROUP BY`, `HAVING`, `LIMIT`, -and `ORDER BY`should be fully left justified. - -For single `SELECT`s, you can use the single line form: - -```sql -SELECT first_name -FROM rappers -``` - -If you are `SELECT`ing more than one column, place all selects on their own line indented 2 -spaces in a block after the `SELECT` keyword. - -```sql -SELECT - first_name, - last_name, - is_still_tippin_on_four_fours, - is_still_wrapped_in_four_vogues -FROM rappers -WHERE first_name = 'Mike' - AND last_name = 'Jones' -``` - -This allows the reader to quickly scan for the important building blocks of the query. - - #### Joins Joins should be indented 2 spaces right from the `FROM` keyword From cf8a8572471a9620fe2da6f28cad2ddb4b829727 Mon Sep 17 00:00:00 2001 From: "apurvis@lumoslabs.com" Date: Sat, 28 May 2016 16:16:11 +0800 Subject: [PATCH 3/3] Probably use WITH --- _includes/sqlstyle.guide.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_includes/sqlstyle.guide.md b/_includes/sqlstyle.guide.md index 80cffe1..adb4350 100644 --- a/_includes/sqlstyle.guide.md +++ b/_includes/sqlstyle.guide.md @@ -291,8 +291,8 @@ FROM my_tmp_table #### Sub-queries -In PostgreSQL you should be doing subqueries with `WITH` clauses and avoiding the use of inline -subqueries. +In PostgreSQL you should probably be writing subqueries with `WITH` clauses and mostly avoiding the +use of inline subqueries. In MySQL or other query engines that do not support `WITH`, sub-queries should be left aligned 2 spaces to the right of the opening parentheses and then laid out using the same style as a `WITH`