SQL Formatter & Beautifier
Format and beautify SQL queries with proper indentation, keyword uppercasing, and line breaks.
Why Formatted SQL Matters
SQL is one of the most forgiving languages when it comes to whitespace. A query that works perfectly as one enormous line also works formatted across 20 lines. But the person reading that query — you, six months from now — will strongly prefer the formatted version.
Formatted SQL makes the structure visible. You can see the SELECT clause at a glance, spot which tables are JOINed, understand the WHERE conditions, and verify the GROUP BY columns. With everything on one line, you're scrolling horizontally and hoping for the best.
This formatter takes messy SQL (pasted from logs, generated by ORMs, or squished into a single line) and produces clean, indented, readable SQL. It handles SELECT, INSERT, UPDATE, DELETE, CREATE, and complex subqueries. Paste, format, understand.
SQL Formatting Conventions
| Element | Convention | Example |
|---|---|---|
| Keywords | UPPERCASE | SELECT, FROM, WHERE, JOIN |
| Table/column names | lowercase or snake_case | users, first_name, order_id |
| Major clauses | New line each | SELECT ... FROM ... WHERE ... ORDER BY |
| Column lists | One per line (for readability) | id,\n name,\n email |
| JOINs | New line, indented ON clause | JOIN orders\n ON users.id = orders.user_id |
| Subqueries | Indented, parenthesised on own lines | WHERE id IN (\n SELECT ...\n) |
What this means for you: There's no one "correct" SQL formatting standard, but the conventions above are the most widely used. The key is consistency within your project. Pick a style and apply it everywhere.
SQL Writing Best Practices
Never use SELECT *
SELECT * fetches every column, including ones you don't need. It slows queries, breaks when columns are added, and makes code harder to understand. Always list the specific columns you need.
Use table aliases consistently
In multi-table queries, alias every table (users u, orders o) and qualify every column (u.name, o.total). This prevents ambiguous column errors and makes it clear where each column comes from.
Comment complex logic
A complex WHERE clause or subquery deserves a comment explaining the business logic. SQL comments use -- for single-line or /* */ for multi-line. Future you will appreciate it.
Use parameterised queries
Never concatenate user input into SQL strings. Use parameterised queries (prepared statements) to prevent SQL injection. Every ORM and database driver supports them natively.
SQL Dialect Differences
| Feature | MySQL | PostgreSQL | SQL Server |
|---|---|---|---|
| Limit rows | LIMIT 10 | LIMIT 10 | TOP 10 |
| String concat | CONCAT(a, b) | a || b | a + b |
| Auto-increment | AUTO_INCREMENT | SERIAL | IDENTITY(1,1) |
| Upsert | ON DUPLICATE KEY | ON CONFLICT | MERGE |
| Identifier quotes | `backticks` | "double quotes" | [brackets] |
This formatter handles standard SQL syntax. Dialect-specific keywords format correctly, but always test formatted SQL against your actual database before running in production.
Related Tools
How to use this tool
Paste your SQL query into the input field
Click Format SQL to beautify with proper indentation
Copy the formatted output for your codebase
Common uses
- Formatting ORM-generated SQL for debugging
- Cleaning up single-line SQL for code reviews
- Beautifying log-extracted queries for analysis
- Standardising SQL style across a development team
Share this tool