SQL Formatter

Format and reflow SQL queries to make them easier to read, review and debug.

Back to all tools on ToolForge

More in Developer Tools



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:

How the Formatter Works

The formatter uses a token-based approach to parse and restructure SQL:

  1. Input SQL is tokenized into keywords, operators, identifiers, and literals
  2. String literals and quoted identifiers are preserved exactly
  3. SQL keywords (SELECT, FROM, WHERE, etc.) trigger new lines with appropriate indentation
  4. Parentheses increase/decrease indentation level for nested queries
  5. 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:

Formatting Notes

Common Use Cases

How to Format SQL

  1. Paste your SQL: Copy minified or poorly-formatted SQL into the input box above.
  2. Click Format: The tool will parse and restructure your SQL with consistent indentation.
  3. Review the output: Check that the formatted query structure looks correct and all clauses are properly indented.
  4. Copy the result: Click "Copy Result" to paste the formatted SQL into your editor or documentation.

Tips for Best Results

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.