Format and beautify SQL queries
Format and beautify SQL queries instantly with our free online SQL formatter. This tool helps database developers and analysts transform messy SQL into readable, properly indented queries for better maintainability. Simply paste your SQL and get formatted output in real-time.
Format and beautify SQL queries for better readability. This formatter supports common SQL syntax and keywords.
Features:
Supported statements: SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, DROP
SQL formatting transforms messy, single-line queries into readable, properly indented SQL. This is essential for code reviews, documentation, debugging, and maintaining SQL codebases. Well-formatted SQL is easier to understand, modify, and catch errors in.
The formatter parses SQL syntax, identifies clauses (SELECT, FROM, WHERE, etc.), and applies consistent formatting rules: keywords uppercase/lowercase, proper indentation for nested queries, line breaks at logical points, and aligned columns. It preserves the query's functionality while improving readability.
SQL formatting involves: tokenization (identifying keywords, identifiers, literals), clause detection (SELECT, FROM, WHERE, JOIN, GROUP BY, etc.), nested query handling, and style application. Dialects (MySQL, PostgreSQL, SQL Server, Oracle) have syntax variations that formatters must handle.
Format SQL for readable pull request reviews.
SELECT * FROM users WHERE active=1 → properly indented multi-lineFormat complex queries to identify logical errors.
Format SQL examples in documentation and tutorials.
Standardize formatting across old SQL codebases.
Readable queries make optimization opportunities clearer.
-- Before (hard to read)
SELECT u.id,u.name,u.email,o.total,o.created_at FROM users u INNER JOIN orders o ON u.id=o.user_id WHERE u.active=1 AND o.total>100 ORDER BY o.created_at DESC LIMIT 10;
-- After (formatted)
SELECT
u.id,
u.name,
u.email,
o.total,
o.created_at
FROM users u
INNER JOIN orders o ON u.id = o.user_id
WHERE u.active = 1
AND o.total > 100
ORDER BY o.created_at DESC
LIMIT 10;Each clause on its own line, consistent indentation, aligned conditions. Much easier to scan and modify.
import { format } from 'sql-formatter';
const formatted = format(
'SELECT * FROM users WHERE id = 1',
{
language: 'postgresql',
tabWidth: 4,
keywordCase: 'upper',
linesBetweenQueries: 2,
}
);
// Output:
// SELECT
// *
// FROM
// users
// WHERE
// id = 1sql-formatter supports multiple SQL dialects and customizable style options.
# Using sqlformat (Python)
pip install sqlparse
sqlformat --reindent --keywords upper -o output.sql input.sql
# Using pgFormatter (PostgreSQL)
pg_format -u 1 input.sql > output.sql
# In-place formatting
sqlformat --reindent -a input.sqlsqlparse is a Python library with CLI. pgFormatter is specialized for PostgreSQL.
Use SQL formatter whenever writing, reviewing, or documenting SQL queries. Essential for team codebases, code reviews, and anywhere SQL readability matters.
Both are valid - SQL is case-insensitive for keywords. Uppercase (SELECT, FROM, WHERE) is traditional and makes keywords stand out from identifiers. Lowercase is more modern and easier to type. Choose one and be consistent.