SQL Reference ยท Dialects and Concepts
SQL style guide
A practical SQL style guide: keyword case, layout, indentation, naming of tables and columns, aliases, joins, comments and habits that keep queries readable.
SQL ignores whitespace and keyword case, so the computer does not care how you format a query. People do. A query is read many more times than it is written, and a consistent style makes the logic visible, reduces mistakes and makes version-control diffs small. This guide describes the conventions used throughout this reference; the exact choices matter less than applying them consistently across a team.
Layout
- Keywords in uppercase, names in lowercase:
SELECT name FROM products. - One clause per line for anything beyond a trivial query, with each major clause (
SELECT,FROM,WHERE,GROUP BY,HAVING,ORDER BY) starting at the left margin. - One column per line in long select lists, or a few per line when short.
- Indent by two spaces inside parentheses and below a clause.
- Join conditions on the join line or indented directly below it.
- Put
ANDandORat the start of continuation lines, and use parentheses whenever you mix them. - End statements with a semicolon.
Press Run (or Ctrl + Enter) to see the result here.
Naming
| Object | Convention | Example |
|---|---|---|
| Tables | Lowercase snake_case; one consistent choice of singular or plural |
customers or customer |
| Columns | Lowercase snake_case, descriptive |
signed_up, unit_price |
| Primary key | id |
id |
| Foreign key | referenced table (singular) + _id |
customer_id |
| Booleans | Prefix is_ or has_ |
is_active |
| Dates and times | Suffix _on or _at |
ordered_at, hired_on |
| Indexes and constraints | Pattern that names table and columns | idx_orders_customer_id |
| Aliases | Short but meaningful | c for customers, not t1 |
Further rules:
- Avoid reserved words as names so quoting is never needed (reserved keywords).
- No type or prefix noise:
tbl_customersorvarchar_nameadd nothing. - Don't abbreviate unless the abbreviation is universal (
id,url). - Use the same name for the same thing across tables (
customer_id, notcustin one place andclient_idin another).
Aliases, joins and columns
Press Run (or Ctrl + Enter) to see the result here.
- Use
ASfor aliases, and always for column aliases. - Qualify columns with the table alias when a query has more than one table, even where not required.
- Avoid
SELECT *in application code and views. List the columns you need, so schema changes do not break callers and less data moves. - Use
JOIN ... ON, not comma joins with conditions inWHERE, and write the join type out (INNER JOIN,LEFT JOIN). - Prefer
COALESCE,CASEandCASTover vendor-only forms when equivalent. - Prefer
<>over!=for portability; both work in most databases but<>is standard. - Always specify
ORDER BYwhen order matters, and addLIMITfor ad hoc exploration. - Name your
GROUP BYcolumns instead of numbers, except in quick ad hoc work.
Comments and structure
Press Run (or Ctrl + Enter) to see the result here.
- Comment the why, not the what: business rules, odd filters and workarounds.
-- exclude test accountshelps;-- select the namedoes not. - Use CTEs to name steps instead of deeply nested subqueries; each CTE should do one thing.
- Keep statements short. A query longer than a page is usually two queries or a view.
- Format
INSERTwith column lists (INSERT INTO t (a, b) VALUES ...) so it survives schema changes. - Trailing vs leading commas: pick one. Leading commas make a removed column a one-line diff but look unusual; trailing commas are more common.
Dialect differences
- Keyword case and identifiers are style, not syntax, in all dialects. Oracle and PostgreSQL fold unquoted names (upper and lower respectively), so lowercase
snake_casewithout quotes works in both; mixed-case quoted names do not. - Line comments with
--work everywhere;#only in MySQL. Block comments/* ... */work in all five. - Identifier quoting and
LIMITversusTOPchange how a query is laid out; see quoting and row limiting. - Formatting tools exist for most dialects (sqlfluff, pgFormatter, SQL Prompt, and IDE formatters); configure them to your conventions and run them in review or CI.
Usage notes
- Consistency beats perfection. Adopt a style once, write it down and let a formatter enforce it.
- Format for diffs. One column or condition per line makes code review precise.
- Review for logic, not layout, by automating layout checks.
- Match the existing style when editing an old file, unless the team agrees to reformat everything in a separate commit.