SQL Formatter
Format and reflow SQL queries to make them easier to read, review and debug.
Back to all tools on ToolForge
Formatted SQL
About SQL Formatter
This SQL formatter rewrites SQL queries into a cleaner, more readable layout, making it easier to understand complex joins, subqueries, and nested conditions. Whether you're debugging ORM-generated SQL, reviewing database migrations, or preparing queries for documentation, this tool helps you quickly improve readability.
Why Format SQL?
SQL formatting provides several benefits:
- Easier debugging: Spot syntax errors and logic issues more quickly
- Better code reviews: Clear structure helps teammates understand your queries
- Consistent style: Maintain uniform formatting across team SQL code
- Faster onboarding: New team members can understand existing queries more easily
How the Formatter Works
The formatter uses a token-based approach to parse and restructure SQL:
- Input SQL is tokenized into keywords, operators, identifiers, and literals
- String literals and quoted identifiers are preserved exactly
- SQL keywords (SELECT, FROM, WHERE, etc.) trigger new lines with appropriate indentation
- Parentheses increase/decrease indentation level for nested queries
- Commas trigger line breaks for column and value lists
Example: Before and After
Input (minified SQL):
SELECT u.id,u.name,o.total FROM users u JOIN orders o ON u.id=o.user_id WHERE o.total>100 ORDER BY o.total DESC;
Output (formatted SQL):
SELECT u.id, u.name, o.total FROM users u JOIN orders o ON u.id = o.user_id WHERE o.total > 100 ORDER BY o.total DESC;
Supported SQL Keywords
The formatter recognizes and formats around these common SQL keywords:
- Data manipulation: SELECT, INSERT, UPDATE, DELETE, MERGE
- Query clauses: FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, OFFSET
- Joins: JOIN, LEFT JOIN, RIGHT JOIN, INNER JOIN, OUTER JOIN, CROSS JOIN
- Set operations: UNION, INTERSECT, EXCEPT
- DDL: CREATE, ALTER, DROP, TRUNCATE
Formatting Notes
- Indentation: Uses 2 spaces per nesting level
- Keywords: Converted to uppercase for consistency
- String literals: Preserved exactly as written (including case)
- Comments: Basic support for SQL comments (-- and /* */)
- Parentheses: Subqueries in parentheses get indented blocks
Common Use Cases
- ORM debugging: Format generated SQL from Entity Framework, Hibernate, etc.
- Query optimization: Review execution plans with readable queries
- Code reviews: Present clean SQL in pull requests
- Documentation: Include formatted SQL in technical docs
- Learning SQL: Understand query structure when studying SQL
How to Format SQL
- Paste your SQL: Copy minified or poorly-formatted SQL into the input box above.
- Click Format: The tool will parse and restructure your SQL with consistent indentation.
- Review the output: Check that the formatted query structure looks correct and all clauses are properly indented.
- Copy the result: Click "Copy Result" to paste the formatted SQL into your editor or documentation.
Tips for Best Results
- Ensure your SQL is syntactically complete (matching parentheses, quotes)
- For dialect-specific SQL, verify the formatted output still works in your database
- Very long queries may take a moment to process
Frequently Asked Questions
- What is SQL formatting and why is it useful?
- SQL formatting (also called pretty-print) restructures SQL queries with consistent indentation and line breaks, making complex queries easier to read, review, and debug. This is especially helpful for minified SQL from ORMs, generated queries, or copied code that lacks proper formatting.
- What SQL statements does this formatter support?
- This formatter handles common SQL statements including SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, and DROP. It recognizes keywords, handles string literals, and formats JOIN clauses, subqueries, and WHERE conditions with appropriate indentation.
- Does the formatter validate SQL syntax?
- No. This formatter only improves readability by restructuring whitespace and indentation. It does not validate SQL syntax or guarantee the query will run correctly. Always test formatted queries in your database environment before using in production.
- What SQL dialects are supported?
- This formatter works with standard SQL syntax common to MySQL, PostgreSQL, SQLite, SQL Server, and Oracle. However, dialect-specific features (like procedural SQL or vendor-specific functions) may not format perfectly.
- Can this formatter handle stored procedures or functions?
- Basic formatting is supported, but complex procedural SQL (BEGIN...END blocks, DECLARE statements, etc.) may not format perfectly. For heavy procedural SQL, consider using your database's dedicated formatting tools.