SQL Beautification & Formatting: Writing Maintainable Queries and Standardizing Indentation
By Evorenza Engineering • Category: Developer Guides
The Challenge of Unformatted SQL
In production debugging, developers frequently pull database queries from ORM logs or application monitors. These queries typically appear as dense, unformatted single lines with dozens of joins and subqueries.
Core Rules for Clean SQL Styling
- UPPERCASE Reserved Keywords: Capitalize SQL dialect keywords (SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY).
- One Major Clause Per Line: Start each primary SQL clause on a fresh line.
- Indent Column Lists and Projections: Place selected columns on individual indented lines.
- Explicit JOIN Conditions: Keep ON criteria indented directly under their associated JOIN statement.
- Format CTEs: Indent Common Table Expression bodies within parentheses.