SQL Formatter
Pretty-print SQL queries with consistent indentation.
Overview
The SQL formatter pretty-prints a SQL query with consistent indentation, keyword casing, and clause alignment. Paste a one-line ORM-generated monster and get back a readable, reviewable statement.
It's a daily-use tool for backend developers, database administrators, and anyone reading raw SQL in logs. A sql beautifier saves the eye-strain of decoding a 600-character query and makes diffs in code review meaningful.
How it works
The formatter tokenises the input into keywords, identifiers, literals, operators, and comments. It then re-emits the query with a layout style: major clauses (SELECT, FROM, WHERE, GROUP BY, ORDER BY, JOIN) on their own line, columns and conditions indented under their parent clause, and parentheses preserved with matched indentation.
Multiple SQL dialects are recognised — PostgreSQL, MySQL, SQL Server (T-SQL), Oracle PL/SQL, and SQLite — so dialect-specific keywords (LIMIT, TOP, OFFSET ... FETCH) are positioned correctly. Comments are preserved in place, both single-line (--) and block (/* */).
Examples
Input:
SELECT u.id, u.name, COUNT(o.id) AS order_count FROM users u LEFT JOIN orders o ON o.user_id = u.id WHERE u.active = TRUE GROUP BY u.id, u.name HAVING COUNT(o.id) > 5 ORDER BY order_count DESC LIMIT 10
Output:
SELECT
u.id,
u.name,
COUNT(o.id) AS order_count
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE u.active = TRUE
GROUP BY u.id, u.name
HAVING COUNT(o.id) > 5
ORDER BY order_count DESC
LIMIT 10;
FAQ
Does it work with CTEs and window functions?
Yes. Common Table Expressions get their own indented block, and window function clauses (OVER (PARTITION BY ... ORDER BY ...)) keep their internal structure readable.
Can I configure keyword case?
Uppercase keywords are the default (matches most style guides). Toggle to lowercase or PascalCase if your team's conventions differ — the formatter never changes identifier case, only reserved words.
Will it execute or validate the SQL?
No — formatting only. Syntax errors that don't break tokenisation are reformatted as-is. For execution, use a proper database client; for validation, use a dialect-aware linter like sqlfluff.