SQL Formatter Online
Pretty-print SQL queries with consistent indentation and keyword case for MySQL, PostgreSQL, SQL Server, Oracle, BigQuery and more, or compact them.
Result
Press “Run” to see the result.
The SQL formatter rewrites a query with one clause per line, indented column lists and joins, and a consistent case for keywords, data types and functions (keyword_case upper, lower or preserve). Choose the dialect so its syntax is understood: mysql, mariadb, postgresql, sqlite, tsql (SQL Server), plsql (Oracle), bigquery, snowflake, redshift, spark, db2, trino, clickhouse or standard sql.
Tune the layout with indent (1–8 spaces) or tabs=true, expression_width (how long a parenthesised expression may stay on one line), logical_operator_newline (before or after AND / OR) and lines_between_queries for scripts with several statements. String literals and comments are kept.
minify=true does the opposite: comments are removed and whitespace outside quotes collapsed, for logs or query strings. A query that cannot be parsed returns invalid_sql with the position of the problem.
Use this in your code
curl -X POST "https://apibox.io.vn/v1/sql-format" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"sql":"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.city = '\''Hà Nội'\'' and o.total > 100000 group by u.id, u.name order by orders desc limit 10","dialect":"postgresql"}'const res = await fetch('https://apibox.io.vn/v1/sql-format', {
method: 'POST',
headers: {
Authorization: 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"sql": "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.city = 'Hà Nội' and o.total > 100000 group by u.id, u.name order by orders desc limit 10",
"dialect": "postgresql"
}),
});
const data = await res.json();
console.log(data);import requests
res = requests.post(
"https://apibox.io.vn/v1/sql-format",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"sql": "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.city = 'Hà Nội' and o.total > 100000 group by u.id, u.name order by orders desc limit 10", "dialect": "postgresql"},
timeout=30,
)
print(res.json())<?php
$ch = curl_init('https://apibox.io.vn/v1/sql-format');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => ['Authorization: Bearer YOUR_API_KEY', 'Content-Type: application/json'],
CURLOPT_POSTFIELDS => '{"sql":"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.city = \'Hà Nội\' and o.total > 100000 group by u.id, u.name order by orders desc limit 10","dialect":"postgresql"}',
]);
$response = curl_exec($ch);
print_r(json_decode($response, true));API documentation → Get a free API key
FAQ
Does it check that my SQL is valid?
Only its syntax as far as formatting needs; table and column names are not checked against a database.
Are comments kept?
Yes when formatting. minify=true removes them.
Which dialect should I choose?
The one of your database; it matters for quoting (backticks, [brackets]) and specific keywords. sql works for standard queries.