The following document is currently work in progress.
- Language keywords MUST be written in UPPER CASE format.
- Table and column names MUST be written as they are defined in the database.
- Line length MUST not exceed 80 characters in length, everything longer than that MUST be split into multiple lines.
- To ensure better readability main keywords MUST be right aligned and MUST follow the RIVER pattern (https://www.red-gate.com/simple-talk/sql/t-sql-programming/formatting-sql-code-part-second/).
- Indentation MUST be performed using spaces and not tabs.
- Indent size MUST be 8 spaces.
- Separators MUST be placed at the beginning of the line and not at the end.
Note: This is highly opiniated so we might decide later to switch to the oposite.
- MUST use the AS keyword.
- MUST be camelCase.
- MUST be surrounded with parenthesis.
- MUST be aliased.
-
Column name MUST be specified inline with the SELECT keyword when selecting a single column.
Example:
SELECT birthDate
-
Column name MUST be specified on a separate line below the SELECT keyword, indented, when selecting multiple columns.
Example:
SELECT firstName , lastName , birthDate
- Table name MUST be specified inline with the FROM keyword.
- Table name MUST have an alias when JOIN clauses are defined.
- From clause MUST consist of only one table. Usage of implicit JOIN statements is prohibited.
Example:
SELECT
person.firstName
, person.lastName
, account.balance
FROM people AS person
INNER
JOIN accounting AS account
ON account.person_id = person.id
- Inner join MUST be defined using INNER JOIN keyword, the usage of short JOIN keyword variant is prohibited.
- Left/Right outer joins MUST be defined using LEFT/RIGHT JOIN keyword, the usage of long oboslete LEFT/RIGHT OUTER JOIN keyword is prohibited.
- Joined tables MUST have an alias.
-
Short conditions SHOULD be specified inline.
Example:
WHERE person.id = 5
-
The AND/OR operator MUST be positioned at the beginning of the line.
-
Long conditions MUST be split over multiple lines.
Example:
WHERE account.balance IS NULL AND person.id = 5
-
Conditions which have complex logic must be surrounded by parenthesis.
Example:
SELECT MAX(account.balance) FROM people AS person INNER JOIN acconting AS account ON account.person_id = person.id WHERE ( YEAR(person.birthDate) >= 2000 AND person.firstName = "Nikola" ) OR ( MONTH(person.birthDate) = 5 AND ( account.column_d = 1 AND account.column_e IS NULL ) )
- Column name MUST be the same as defined in the SELECT clause.
- Column name MUST be specified inline with the GROUP BY keyword when grouping by a single column.
- Column name MUST be specified separately on a new line, indented below the GROUP BY keyword, when grouping by multiple columns.
- Column separator - COMMA - MUST be positioned after the column and not before.
- Order direction keyword - ASC / DESC - MUST be specified inline with the column name.
- Order direction keyword MUST not be omitted.
- Column name MUST be specified inline with the ORDER BY keyword when ordered by a single column.
- Column name MUST be specified separately on a new line, indented below the ORDER BY keyword, when ordered by multiple columns.
- Column separator - COMMA - MUST be positioned after the column and not before.
- Limit value MUST be defined inline with the LIMIT keyword.
- LIMIT clause MUST not contain an OFSET, use OFSET clause for defining one.
- Ofset value MUST be defined inline with the OFSET keyword.
Example:
INSERT
INTO people (
firstName
, lastName
, birthDate
)
Example:
VALUES (
'Nikola'
, 'Tesla'
, '1856-07-10'
)
- Table name MUST be specified inline with the UPDATE keyword when updating a single table.
- Table name MUST be specified on a separate line, indented, when updating multiple tables.
- MUST follow the same guidelines as in SELECT statement
- Column name MUST be specified inline after the SET keyword when updating a single column.
- Column name MUST be specified in a new row, below the SET keyword, indented, when updating multiple columns.
- MUST follow the same guidelines as in SELECT statement
Example:
UPDATE accounting AS account
INNER
JOIN people AS person
ON person.id = account.person_id
SET account.balance = 0
WHERE person.deteted = 1
- Table name MUST be ommited when deleting from a single table
- Table name MUST be specified on a new line, indented, when deleting from multiple tables.
- Table separator - COMMA - MUST be placed after the table name.
- MUST follow the same guidelines as in SELECT statement
- MUST follow the same guidelines as in SELECT statement
- MUST follow the same guidelines as in SELECT statement