SQL Formatter: Beautify and Indent SQL Queries Online
Paste a SQL query and get readable, indented code with keywords in capitals. Handles joins, subqueries, grouping and updates. Runs in your browser.
Paste your SQL and press Beautify. Clauses start on new lines, columns are listed one per line, joins and conditions are indented, CASE expressions are laid out line by line, CREATE TABLE puts one column on each line, and subqueries and WITH clauses are nested. Keywords can be turned to capitals. Stored procedures, functions and triggers (MySQL DELIMITER blocks and PostgreSQL $$ bodies) are laid out with IF, WHILE, LOOP and CASE indented.
What it does
- Starts SELECT, FROM, WHERE, GROUP BY and ORDER BY on new lines.
- Lists columns, SET values and INSERT value rows one per line.
- Puts each column of CREATE TABLE on its own line.
- Lays out long CASE WHEN ... END expressions over several lines.
- Keeps BETWEEN x AND y together and indents JOIN and AND/OR lines.
- Nests subqueries and WITH (CTE) blocks.
- Optionally writes keywords in capitals.
- Formats stored procedures, functions and triggers: parameters one per line, BEGIN ... END bodies, IF / ELSEIF / ELSE, WHILE, LOOP, REPEAT and CASE statements, DELIMITER lines and $$ bodies. Semicolons inside strings do not split statements.
Example
Before
select u.id,u.name,count(o.id) as orders from users u left join orders o on o.user_id=u.id where u.active=1 and u.age>18 group by u.id,u.name order by orders desc limit 10;After
SELECT
u.id,
u.name,
count(o.id) AS orders
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE u.active = 1
AND u.age > 18
GROUP BY
u.id,
u.name
ORDER BY orders DESC
LIMIT 10;Frequently asked questions
- Which SQL dialects does it support?
- Everyday MySQL, PostgreSQL, SQLite and SQL Server queries (SELECT, INSERT, UPDATE, DELETE, WITH, CASE, CREATE TABLE), plus MySQL stored procedures with DELIMITER and PostgreSQL functions with $$ bodies. It lays out the code but does not check it, and unusual vendor syntax (T-SQL TRY/CATCH, Oracle PL/SQL packages) is kept on single lines rather than restructured.
- Does it change what the query does?
- No. Only spacing, line breaks and keyword case change. Strings and quoted names are kept as written.
- Why are my table names not capitalised?
- Only SQL keywords are changed. Table and column names keep their case.
- Can it handle several statements?
- Yes. Statements separated by semicolons are formatted one after another.
More tools
Code formatters
CSS beautifier CSS minifier HTML beautifier HTML minifier JavaScript beautifier JavaScript minifier JSON formatter YAML formatter XML formatter Markdown preview
Converters
Base64 encoder and decoder URL encoder and decoder Unix timestamp converter CSV to JSON JSON to CSV HTML entity encoder and decoder Number base converter Time zone converter JSON to TypeScript XML to JSON HTML to Markdown Date difference calculator
Text tools
Regex tester Remove duplicate lines Lorem ipsum generator Word counter Case converter Text diff checker
Color and images
Color converter Contrast checker Image to Base64 Image resizer
Network
Subnet calculator Download time calculator Common ports list CIDR to IP range IPv6 address expander and compressor MAC address formatter curl command builder
Server and files
Cron expression explainer chmod calculator HTTP status codes
Encoding and security
Password generator UUID generator Hash generator JWT decoder QR code generator Password strength checker Wi-Fi QR code generator