SQL Beautifier
Format and beautify SQL queries online for better readability. Free SQL formatter with keyword highlighting and proper indentation.
About SQL Beautifier
This SQL Beautifier formats your SQL queries with proper indentation and keyword capitalization. It makes complex queries readable by placing major clauses on separate lines and indenting conditions.
How to Use SQL Beautifier
Paste your SQL
Paste messy or one-line SQL into the input editor on the left, or click Upload to pick a .sql file from disk. SELECT, INSERT, UPDATE, DELETE, DDL, and PL/SQL all work.
Choose options
The beautifier auto-capitalizes SQL keywords (SELECT, FROM, JOIN) for readability. Choose 2-space, 4-space, or tab indentation to match your team's style guide.
View formatted output
Properly formatted SQL appears instantly with each clause on its own line, JOINs aligned, conditions indented, and CTEs/subqueries clearly nested.
Copy or download
Click Copy to put the formatted SQL on your clipboard, or Download as a .sql file. Use it for code reviews, query documentation, debugging, or pasting into your IDE.
When to Use SQL Beautifier
Reviewing ORM-generated queries
ORMs (Sequelize, Prisma, SQLAlchemy, Hibernate, ActiveRecord) generate SQL that can balloon into hundreds of characters on a single line. Beautifying these reveals what your ORM actually executes — essential for spotting N+1 queries, missing JOINs, unnecessary subqueries, or accidentally cartesian products that hurt performance.
Debugging slow queries from logs
Database slow query logs (PostgreSQL pg_stat_statements, MySQL slow log, SQL Server Extended Events) capture queries verbatim — often dense and unformatted. Beautifying them lets you analyze structure, identify missing indexes, find inefficient WHERE clauses, and trace execution plans alongside the readable query.
Writing query documentation
Reports, dashboards, and analytics queries need documentation. Beautifying production queries before pasting into Confluence, Notion, or runbooks dramatically improves comprehension. Consistent capitalization (SELECT vs select) and indentation make the query's intent immediately clear to readers without database expertise.
Pre-commit query review
Before committing migration scripts, stored procedures, or query updates, beautify them. Reviewing aligned JOINs, properly nested CTEs, and capitalized keywords reveals logic errors that minified one-liners hide. Many DB-related bugs are caught simply by formatting code well enough to read.
SQL Beautifier Examples
Compact query to readable
select u.name, count(o.id) from users u left join orders o on u.id = o.user_id where u.created_at > '2024-01-01' group by u.id, u.name having count(o.id) > 5 order by count(o.id) descSELECT
u.name,
count(o.id)
FROM
users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE
u.created_at > '2024-01-01'
GROUP BY
u.id,
u.name
HAVING
count(o.id) > 5
ORDER BY
count(o.id) DESCA complex aggregate query becomes clearly structured. Each major clause (SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY) gets its own line. The LEFT JOIN is aligned with FROM. Column lists are indented. Keywords are uppercased. Now the query reads like a structured statement — much easier to verify logic and spot errors.
CTE (WITH clause) formatting
WITH active_users AS (SELECT id FROM users WHERE last_login > now() - interval '30 days') SELECT u.name FROM active_users a JOIN users u ON a.id = u.id ORDER BY u.nameWITH active_users AS (
SELECT
id
FROM
users
WHERE
last_login > now() - interval '30 days'
)
SELECT
u.name
FROM
active_users a
JOIN users u ON a.id = u.id
ORDER BY
u.nameCommon Table Expressions are formatted with the WITH clause body indented inside parentheses. Each CTE has the same internal structure as a top-level SELECT. The main query that follows uses the CTE as if it were a regular table. Multi-CTE queries follow the same pattern with each CTE separated by commas.
DDL: CREATE TABLE
create table users (id serial primary key, email varchar(255) not null unique, created_at timestamp default now())CREATE TABLE users (
id serial PRIMARY KEY,
email varchar(255) NOT NULL UNIQUE,
created_at timestamp DEFAULT now()
)Schema-defining statements get each column definition on its own line. Constraints (PRIMARY KEY, NOT NULL, UNIQUE, DEFAULT) are uppercased. The result is migration-ready: you can immediately see all columns, types, and constraints — useful for schema reviews, documentation, and detecting issues like missing indexes on foreign keys.
Tips & Best Practices for SQL Beautifier
- 1.Always beautify queries before code review or documentation. Reviewers can't catch logic errors in unformatted SQL — but spot them quickly when the structure is clear.
- 2.When debugging slow queries, beautify the query first, then run EXPLAIN ANALYZE alongside. Comparing the formatted query to its execution plan makes it easier to map plan nodes back to specific clauses.
- 3.Keep production migration files beautified in version control. Diffs are nearly useless on minified SQL but very readable on properly formatted CREATE TABLE statements with each column on its own line.
- 4.If your query has dynamic SQL (string concatenation, prepared statement placeholders like $1, ?, @param), beautify the static template first, then mentally substitute the values. The formatted shape stays correct even when parameters change.
- 5.For very long IN lists (WHERE id IN (1, 2, 3, ..., 1000)), beautifiers typically keep them on the same line. Manually break long lists into multiple lines or refactor to a JOIN with a temp table — improves readability AND often performance.
- 6.If your team uses lowercase keywords (a stylistic preference some teams have), revert the uppercase output manually after beautifying. Some beautifiers offer a 'preserve case' option, but the convention of uppercase keywords is much more widespread and improves readability for most readers.
Frequently Asked Questions
Related Tools
HTML Beautifier
Format and indent HTML code online for better readability. Free HTML beautifier with syntax highlighting and auto-indentation.
CSS Beautifier
Format and indent CSS code online for cleaner stylesheets. Free CSS beautifier with proper indentation and syntax organization.
JavaScript Beautifier
Format and indent JavaScript code online for improved readability. Free JS beautifier with auto-indentation and clean output.
JSON Beautifier
Format and indent JSON data online with proper syntax highlighting. Free JSON beautifier with validation and tree view support.
XML Beautifier
Format and indent XML data online with proper nesting and syntax highlighting. Free XML beautifier with validation support.
GraphQL Formatter
Format and beautify GraphQL queries, mutations, and schemas online. Free GraphQL formatter with syntax validation and configurable indentation.