SQL
4 min read
BEGINNER
Query readability
Quick AnswerReadable SQL makes the intended source, selected values and ordering easy to inspect. Formatting is a convention, not a substitute for correct syntax.
Core idea
Readable SQL makes the intended source, selected values and ordering easy to inspect. Formatting is a convention, not a substitute for correct syntax.
Minimal example
sql
SELECT p.name,p.price_cents AS unit_price_cents FROM products AS p ORDER BY p.id;Expected rows (in order):
text
Notebook | 350Pen | 150Bag | 2400Pencil | 100How it works
Put major clauses on separate lines and use descriptive aliases. Keep expressions and comments focused on intent. Whitespace inside string literals is data, so formatting must not alter it. The compact and formatted queries below return the same values.
Practical example
sql
SELECT p.name, p.price_cents AS unit_price_centsFROM products AS pORDER BY p.id;Expected rows (in order):
text
Notebook | 350Pen | 150Bag | 2400Pencil | 100Common mistake and practice
Do not change quoted text or identifiers just to enforce a style. Reformat the compact query and compare its results to the original.