SQL · Guide
Format SQL Query — Online & in Code
Unformatted SQL is nearly impossible to debug. This guide shows you how to format SQL queries quickly — online in one click, or inside your favourite editor — with real before/after examples.
SQL Formatter
Paste any SQL query and get clean, formatted output instantly. Supports MySQL, PostgreSQL, and more.
Open SQL Formatter →Format SQL online in one click
The fastest way to format a SQL query is to paste it into the SQL formatter on tinybench.dev:
- Paste your raw or minified SQL into the input panel
- Choose your indent size (2 spaces, 4 spaces, or tab)
- Choose keyword casing (UPPERCASE or lowercase)
- Click Format — instantly get clean output with syntax highlighting
Private: Your SQL runs entirely in your browser. Sensitive queries, credentials in SQL, and proprietary schemas are never sent to any server.
Before and after example
Unformatted SQL
select u.id,u.name,u.email,count(o.id) as total_orders from users u left join orders o on u.id=o.user_id where u.active=1 and u.created_at>'2024-01-01' group by u.id,u.name,u.email having count(o.id)>0 order by total_orders desc limit 25
Formatted SQL
SELECT
u.id,
u.name,
u.email,
COUNT(o.id) AS total_orders
FROM users u
LEFT JOIN orders o
ON u.id = o.user_id
WHERE u.active = 1
AND u.created_at > '2024-01-01'
GROUP BY
u.id,
u.name,
u.email
HAVING COUNT(o.id) > 0
ORDER BY total_orders DESC
LIMIT 25SQL formatting rules to follow
- One clause per line — SELECT, FROM, WHERE, GROUP BY, ORDER BY each start on a new line
- Indent column lists — each column in SELECT gets its own indented line
- Uppercase keywords — SELECT, FROM, WHERE, JOIN, AND, OR stand out visually
- Qualify all columns — use
u.namenot justnamein multi-table queries - Explicit JOIN type — always write LEFT JOIN, INNER JOIN, not just JOIN
- ON clause indented — indent the ON condition under its JOIN
Format SQL in popular editors
| Editor / Tool | How to format |
|---|---|
| VS Code | Install "SQL Formatter" extension → right-click → Format Document |
| DataGrip | Ctrl+Alt+L (Windows/Linux) or Cmd+Alt+L (Mac) |
| DBeaver | Ctrl+Shift+F in SQL editor |
| pgAdmin | Query menu → Format SQL |
| MySQL Workbench | Edit menu → Format → Beautify Query |
Frequently asked questions
Does formatting SQL affect performance?
No. SQL formatting is purely cosmetic — whitespace, line breaks, and keyword casing have zero effect on how the database parses or executes the query. The database engine ignores all whitespace.
Should I use UPPERCASE or lowercase keywords?
Both are valid. UPPERCASE keywords are the traditional convention — they help keywords stand out visually from table/column names. Lowercase is popular in modern teams and ORM-generated SQL. Pick one and stick to it within a codebase.
How do I format a SQL query in Python?
Use the sqlparse library:
import sqlparse; formatted = sqlparse.format(sql, reindent=True, keyword_case='upper')Try it free — no sign-up needed
Runs entirely in your browser. Nothing uploaded, nothing stored.
Open SQL Formatter →