1
0
Fork 0
mirror of https://github.com/treffynnon/sqlstyle.guide.git synced 2025-03-09 12:49:51 -05:00

Indentation rules

This commit is contained in:
apurvis@lumoslabs.com 2016-05-25 07:26:20 +08:00
parent b8d8ddb2cd
commit 8dfd10336b

View file

@ -163,7 +163,10 @@ Although not exhaustive always include spaces:
comma or semicolon. comma or semicolon.
```sql ```sql
SELECT a.title, a.release_date, a.recording_date SELECT
a.title,
a.release_date,
a.recording_date
FROM albums AS a FROM albums AS a
WHERE a.title = 'Charcoal Lane' WHERE a.title = 'Charcoal Lane'
OR a.title = 'The New Danger'; OR a.title = 'The New Danger';
@ -213,11 +216,21 @@ To ensure that SQL is readable it is important that standards of indentation
are followed. are followed.
**ONLY** the fundamental keywords - `SELECT`, `FROM`, `WHERE`, `GROUP BY`, `HAVING`, `LIMIT`, **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 and `ORDER BY`should be fully left justified.
that keyword.
For single `SELECT`s, you can use the single line form:
```sql ```sql
SELECT first_name, 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, last_name,
is_still_tippin_on_four_fours, is_still_tippin_on_four_fours,
is_still_wrapped_in_four_vogues is_still_wrapped_in_four_vogues
@ -228,6 +241,7 @@ WHERE first_name = 'Mike'
This allows the reader to quickly scan for the important building blocks of the query. This allows the reader to quickly scan for the important building blocks of the query.
#### Joins #### Joins
Joins should be indented 2 spaces right from the `FROM` keyword Joins should be indented 2 spaces right from the `FROM` keyword
@ -277,13 +291,16 @@ FROM my_tmp_table
#### Sub-queries #### 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 In MySQL or other query engines that do not support `WITH`, sub-queries should be left aligned 2
laid out using the same style as a `WITH` statement w/r/t parentheses. 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 ```sql
SELECT r.last_name, SELECT
r.last_name,
( (
SELECT MAX(YEAR(championship_date)) SELECT MAX(YEAR(championship_date))
FROM champions AS c FROM champions AS c