SQL Formatter & Beautifier

Format and beautify SQL queries for readability. Properly indented, capitalized keywords, line-broken clauses.

What is SQL Formatting?

SQL Formatting transforms unformatted SQL queries (single-line blobs from logs, generated code, or copy-paste) into readable, properly-indented, multi-line format with capitalized keywords. Critical for: code reviews of database changes, debugging complex queries, documenting queries in tickets/blogs, sharing snippets with teammates, understanding generated SQL from ORMs (Hibernate, Sequelize, ActiveRecord), code maintainability, training/education materials. Properly formatted SQL highlights logic flow: SELECT clauses, JOINs, WHERE conditions, GROUP BY, ORDER BY each on their own lines makes query intent obvious at a glance.

How to use this tool

  1. Paste SQL query — Any SQL — SELECT, INSERT, UPDATE, DELETE, JOINs.
  2. Click Format SQL — Tool reformats with proper indentation and case.
  3. Review formatted output — Each major clause on its own line.
  4. Copy formatted SQL — Paste into your database client, code, or documentation.

Format conventions

Standard SQL formatting rules applied:

  • Major keywords UPPERCASE: SELECT, FROM, WHERE, GROUP BY, ORDER BY, etc.
  • Each major clause starts on a new line
  • Column list under SELECT indented
  • Each JOIN on its own line
  • WHERE conditions on separate lines when multiple AND/OR
  • Subqueries indented inside parentheses
  • Comma-separated columns: comma at end of line (Oracle/MySQL convention) or start of next line (PostgreSQL convention)

Example transformation:

BEFORE (unformatted):
select u.name,u.email,count(o.id) as total from users u left join orders o on o.user_id=u.id where u.created_at>'2024-01-01' group by u.id having count(o.id)>5 order by total desc limit 100

AFTER (formatted):
SELECT
  u.name,
  u.email,
  count(o.id) as total
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE u.created_at > '2024-01-01'
GROUP BY u.id
HAVING count(o.id) > 5
ORDER BY total DESC
LIMIT 100

Examples

  • ORM-generated query debugging: Hibernate produces long single-line query; format for readability
  • Log file SQL: Slow query log entries reformatted to understand bottlenecks
  • Code review: Pull request with database migration — format inline for reviewer
  • Stack Overflow question: Properly formatted SQL gets faster answers
  • Blog post code example: Format SQL examples for readers
  • Documentation: Database schema docs with formatted example queries
  • Training material: Teaching SQL with cleanly formatted examples

Tips & best practices

  • ALWAYS UPPERCASE SQL keywords for readability (most teams convention)
  • Use ANSI SQL style: explicit JOIN syntax instead of comma-separated tables
  • Indent subqueries inside parentheses
  • For very long WHERE clauses, break onto multiple lines with AND/OR at start of each line
  • Use consistent formatting across team — pick one style guide, stick with it
  • Most modern IDEs (DataGrip, DBeaver, VS Code SQL extensions) format on save
  • For PostgreSQL: use pgFormatter (more accurate than generic SQL formatters)

Limitations & notes

This tool uses pattern-based formatting — not a full SQL parser. Complex queries with subqueries, CTEs (WITH clauses), window functions, or unusual syntax may not format perfectly. For production-grade SQL formatting, use dedicated tools: SQLFluff (cross-database), pgFormatter (PostgreSQL), online dpriver formatter, or IDE-integrated formatters (DataGrip, DBeaver).

Frequently Asked Questions

Why format SQL queries?

Readability. Single-line SQL blobs are hard to debug, review, or maintain. Properly formatted SQL with clear indentation and clauses makes query logic visible at a glance.

Should keywords be UPPERCASE or lowercase?

Most teams use UPPERCASE keywords (SELECT, FROM, WHERE) and lowercase identifiers (table names, columns). This visual contrast aids readability. Some new conventions use lowercase keywords — team preference matters.

Does formatting affect query performance?

No — SQL parser treats 'SELECT *' and 'select *' identically. Formatting is purely for human readability. Same execution plan, same speed.

How do I format SQL in my editor?

VS Code: install SQL Formatter extension. DataGrip / DBeaver: built-in formatter. Most IDEs format on save or via keyboard shortcut. This tool for one-off cases without IDE.

What's the best SQL style guide?

Several options: SQL Style Guide by Simon Holywell, Mozilla's SQL style guide, dbt's SQL style guide. Pick one for your team and document it. Consistency matters more than which style.

Should I align column names in SELECT clauses?

Optional — some teams align with spaces for readability. Adds visual clarity but uses more horizontal space. Personal/team preference.

Does this tool handle CTEs and window functions?

Basic support — major keywords formatted. Complex nested CTEs may need manual tweaking. For PostgreSQL CTEs, pgFormatter is more accurate.

Related tools

CSS Beautifier · JSON Formatter · Code Minifier

Copied