DBS Course Coding Style Guide
This guide is an outline of the coding style to be used in dbs211, dbs311, dbs610, and dbs710 courses. Please speak with your professor regarding this requirement if it was not made clear.
SQL Style Guide
The following is a standard SQL style guide that will allow SQL to be consistent, easy to read, and comply with industry standards. This guide was developed from the base Mozilla online standards found at: SQL Style Guide - Mozilla Data Documentation and are therefore a good standard for students to follow and be ready for a variety of style guides found with their future work places.
Capitalization
- All reserved keywords, excluding functions, are to be written in ALL CAPS.
(Examples: SELECT, UPDATE, INSERT, DELETE, FROM, WHERE, JOIN, INNER, OUTER, LEFT, RIGHT, GROUP BY, ORDER BY, INTO, AS, LIMIT, TOP, OFFSET, BEGIN, END, GO, CREATE, ALTER, VIEW, TABLE, PROCEDURE, etc.)
- All built-in functions should be Pascal Case (starts with a capital)
- All user defined objects, such as tables, columns, views, aliases, etc., should be either all lower case using _ (underscore) to fill in spaces or camel case (starts with a lower-case letter).
SELECT
field_name1 AS alias1,
field_name2,
field_name3
FROM tablename
WHERE
field_name2 > somevalue
ORDER BY field_name2 DESC;
Line Breaks and Indenting
- All Root Keywords are to begin on a new line (SELECT, FROM, WHERE, ORDER BY, JOIN, GROUP BY)
- If there is only a single parameter, then it can follow on the same line, otherwise, parameters are each placed on their own line and indented.
- All indents are to be exactly 4 spaces
- Always include a semi-colon at the end of each statement
Joins
- Always include the join type rather than relying on the default join.
- Multi-table joins should be indented
- all JOIN keywords, past the first one, should start a new line
SELECT
table1.field_name1 AS alias1,
table1.field_name2,
table2.field_name3
FROM
table1 t1 INNER JOIN table2 t2 ON t1.field_name1 = t2.field_name3
INNER JOIN table3 t3 ON t2.field_name3 = t3.field_name4
WHERE
table1.field_name2 > somevalue
ORDER BY table1.field_name2 DESC;
Parentheses
If the content of parentheses span multiple lines, then:
- the opening parenthesis should terminate the previous line
- the closing parenthesis should be on its own line aligned with the line that starting the code block
- the contents of the parentheses should all be indented relative to the parent code block
CREATE VIEW view_name AS (
SELECT *
FROM table_name
);
Booleans and Conditional Statements
Boolean operators should always be placed at the beginning of a new line:
SELECT
fieldlist
FROM table_name
WHERE
condition1
AND condition2
OR condition3
ORDER BY fieldname;