How to Format SQL Online — Beautify, Indent & Clean SQL Queries Instantly
Unformatted SQL is hard to read, harder to debug, and nearly impossible to review in a pull request. A SQL formatter instantly restructures messy queries with consistent indentation, keyword casing, and clause separation — without changing any logic. This guide covers how SQL formatting works, what good SQL style looks like, how to format SQL IN clause lists and subqueries, and the best online tools for doing it in one click.
1 click
Paste messy SQL → click Format → get clean, readable SQL instantly
UPPERCASE
SQL keywords in uppercase is the most widely adopted convention
100%
Formatting never changes query logic — only whitespace and casing
Why Format SQL? The Real Reasons Developers Do It
Readable queries debug faster
When every clause starts on its own line with consistent indentation, you can scan a 50-line query in seconds. A wall of unseparated SQL hides bugs — a missing JOIN condition, an accidental cross join, or a WHERE clause that applies to the wrong subquery.
Cleaner pull request diffs
Formatting SQL before committing means git diff shows logic changes, not whitespace noise. Reviewers can focus on whether the JOIN is correct, not whether a keyword is on the right line.
ORM output is unreadable
Django ORM, Hibernate, SQLAlchemy, and Prisma all emit SQL as a single line with minimal whitespace. Paste that into a formatter to understand what query your ORM is actually running — essential for N+1 debugging and performance optimization.
Consistent team style
Formatter enforces a single style across the team without discussion. Uppercase keywords, 2-space or 4-space indentation, one clause per line — these are configured once in the formatter and applied consistently to every query.
SQL Formatting Rules — What Good SQL Style Looks Like
Unformatted SQL vs properly formatted SQL
Unformatted — single line, lowercase, no indentation
-- Hard to read — everything on one line or inconsistently spaced
select u.id,u.name,u.email,o.total from users u inner join orders o on u.id=o.user_id where o.status in ('shipped','delivered') and o.total>100 order by o.total desc limit 50Formatted — one clause per line, indented columns, UPPERCASE keywords
-- Formatted — clear clause structure, readable at a glance
SELECT
u.id,
u.name,
u.email,
o.total
FROM users u
INNER JOIN orders o
ON u.id = o.user_id
WHERE
o.status IN ('shipped', 'delivered')
AND o.total > 100
ORDER BY o.total DESC
LIMIT 50;Keywords in UPPERCASE
SQL keywords (SELECT, FROM, WHERE, JOIN, GROUP BY, HAVING, ORDER BY, LIMIT) in UPPERCASE is the most common convention. It visually separates SQL structure from table and column names.
One clause per line
Each major clause (SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY, LIMIT) starts on its own line. This makes it easy to comment out a clause, add a clause, or scan the query structure at a glance.
Indented column lists
Columns in SELECT are indented and one per line for long SELECT lists. This makes it easy to add or remove a column, and shows the exact shape of the result set.
JOIN conditions indented under JOIN
ON conditions are indented under the JOIN clause that introduces them. This makes it clear which ON belongs to which JOIN in multi-join queries.
Formatting SQL IN Clause Lists
Long IN clause lists are one of the most common formatting challenges. A list of 100 IDs on a single line is unreadable. The two accepted conventions:
-- ---- Horizontal (short lists — 5 items or fewer) ----
SELECT * FROM users
WHERE id IN (1, 2, 3, 4, 5);
-- ---- Vertical (long lists — easier to diff and modify) ----
SELECT * FROM orders
WHERE status IN (
'pending',
'processing',
'shipped',
'delivered',
'return_requested',
'refunded'
);
-- ---- Very long ID lists — use a comment to describe the source ----
SELECT * FROM users
WHERE id IN (
-- User IDs from bug report JIRA-4521 (affected batch, 2026-04-10)
1001, 1002, 1003, 1007, 1012,
1015, 1018, 1022, 1031, 1044
);Formatting Subqueries and CTEs
-- ---- Correlated subquery — indent the subquery body ----
SELECT *
FROM orders o
WHERE o.customer_id IN (
SELECT id
FROM customers
WHERE country = 'US'
AND created_at > '2026-01-01'
);
-- ---- CTE (Common Table Expression) — align WITH and SELECT ----
WITH monthly_revenue AS (
SELECT
DATE_FORMAT(created_at, '%Y-%m') AS month,
SUM(total) AS revenue
FROM orders
WHERE status = 'completed'
GROUP BY DATE_FORMAT(created_at, '%Y-%m')
),
top_months AS (
SELECT month
FROM monthly_revenue
WHERE revenue > 100000
)
SELECT *
FROM monthly_revenue
WHERE month IN (SELECT month FROM top_months)
ORDER BY month DESC;How to Format SQL Online — Step by Step
Go to unblockdevs.com/sql-formatter
The SQL IN Clause Generator and formatter is at unblockdevs.com/sql-formatter. No account, no install, runs entirely in your browser.
Paste your raw SQL or ID list
Paste a messy SQL query, a list of IDs, a JSON array, or anything copied from a log file. The tool auto-detects the input format.
Select your database dialect
Choose MySQL, PostgreSQL, SQL Server, Oracle, or SQLite. The formatter applies the correct quoting conventions and syntax for your database.
Choose output format
For SQL IN clause: choose SQL IN (with or without the full SELECT). For ID lists: choose JSON array, CSV, GraphQL filter, or MongoDB query format.
Click Format (or press ⌘+Enter)
The formatted output appears instantly. Copy to clipboard or download as .sql, .csv, or .json.
SQL Formatting in Your Code Editor (VS Code, JetBrains)
# ---- VS Code ----
# Install the "SQLTools" extension or "SQL Formatter" extension
# Open a .sql file, then:
# Format Document: Shift+Alt+F (Windows/Linux) or Shift+Option+F (Mac)
# ---- JetBrains (DataGrip, IntelliJ with DB plugin) ----
# Format SQL: Ctrl+Alt+L (Windows/Linux) or Cmd+Option+L (Mac)
# Configure style: Settings → Editor → Code Style → SQL
# Options: indent size, keyword case, alignment
# ---- pgFormatter (command-line, PostgreSQL) ----
npm install -g pgformatter # or brew install pgformatter
pg_format messy_query.sql -o formatted_query.sql
# ---- sqlformat (Python, any dialect) ----
pip install sqlparse
python -m sqlparse --reindent --keywords upper messy_query.sql
# ---- prettier-plugin-sql (Node.js project integration) ----
npm install --save-dev prettier prettier-plugin-sql
# .prettierrc:
# { "plugins": ["prettier-plugin-sql"], "language": "mysql" }Format SQL on save — automate style enforcement in your project
Configure your editor to auto-format SQL files on save, the same way you format JavaScript or Python. In VS Code with a SQL formatter extension, add [sql]: {editor.formatOnSave: true} to your workspace settings. This ensures every committed SQL file is already formatted consistently without a manual step.
Common SQL Formatting Mistakes to Avoid
Don't mix casing inconsistently
Pick one convention and stick to it. Mixing SELECT with from and Where in the same query is harder to read than a consistently lowercase or consistently uppercase query.
Align ON with JOIN, not with WHERE
The ON condition belongs to its JOIN, not the WHERE clause. Indenting ON two spaces under the JOIN keyword makes multi-join queries much easier to parse.
Don't omit semicolons in scripts
In multi-statement SQL scripts and stored procedures, semicolons separate statements. Some databases require them; all benefit from them. Add a semicolon at the end of each statement.
Qualify all column names in joins
In any query with more than one table, prefix every column with the table alias: u.id, o.total, p.name. This prevents ambiguous column errors and makes the query self-documenting.