Formatieren and beautify SQL statements
SQL formatting improves readability und maintainability. Lernen indentation Regeln, keyword alignment, und consistent style für komplex queries.
- 1
SQL formatting conventions: (1) Keywords in UPPERCASE (SELECT, FROM, WHERE). (2) Each major clause on a new line (SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY). (3) Columns indented under SELECT. (4) Join conditions aligned. (5) Subqueries indented. (6) Comma-first or comma-last style consistently. (7) Long IN lists wrapped. Consistent formatting makes SQL easier to read, review, and debug.
- 2 SQL-Formatierer →
Formatieren a SQL query with standard conventions. Paste your unformatted SQL and get properly indented, keyword-aligned output. The formatter handles subqueries, CTEs, window functions, and CASE expressions. Use it before code review and commit.
- 3 sql-format → word-count
Use the SQL format workflow to format, validate, and document a SQL query. This produces clean SQL with consistent style, catches syntax errors, and can add comments explaining complex logic.
- 4
Team conventions: agree on a formatting style (e.g., sqlfluff, SQLFormat, pg_format) and enforce it in CI/CD. Pre-commit hooks can auto-format SQL files. For ORM-generated SQL, enable query logging and format the output for debugging. Consistent formatting across the team reduces review friction and makes diffs cleaner.
Häufige Fragen
- Should I use uppercase or lowercase for SQL keywords?
- It is a style choice. UPPERCASE keywords (SELECT, FROM) are traditional and help distinguish keywords from identifiers. lowercase is common in some frameworks (Django ORM). The key is consistency — pick one and enforce it. Most SQL formatters default to uppercase.
- How do I format very long queries?
- Break long SELECT lists into one column per line. Wrap long IN lists (more than 5 values) across multiple lines. Use CTEs to break complex queries into named steps. Add comments for non-obvious logic. For queries over 100 lines, consider refactoring into views or stored procedures.