SQL Formatter API

Pretty-print SQL queries with consistent indentation and keyword case for MySQL, PostgreSQL, SQL Server, Oracle, BigQuery and more, or compact them.

POSTGEThttps://apibox.io.vn/v1/sql-format
Developer utilities▶ Try it online

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.

Parameters

NameInTypeRequiredDefault
sqlbody|querystringyesThe SQL (max 500,000 characters).
dialectbody|querystringnosqlsql, mysql, mariadb, postgresql, sqlite, tsql, plsql, bigquery, snowflake, redshift, spark, db2, trino, clickhouse.
keyword_casebody|querystringnoupperupper, lower or preserve.
indentbody|querynumberno2Spaces per level (1–8).
tabsbody|querybooleannofalseIndent with tabs.
expression_widthbody|querynumberno50Max width of a parenthesised expression on one line (20–200).
logical_operator_newlinebody|querystringnobeforeLine break before or after AND / OR.
lines_between_queriesbody|querynumberno1Blank lines between statements (0–5).
minifybody|querybooleannofalseCompact the SQL instead of formatting it.

Example

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"}'

Response

{
  "dialect": "postgresql",
  "formatted": "SELECT\n  u.id,\n  u.name,\n  COUNT(o.id) AS orders\nFROM\n  users u\n  LEFT JOIN orders o ON o.user_id = u.id\nWHERE\n  u.city = 'Hà Nội'\n  AND o.total > 100000\nGROUP BY\n  u.id,\n  u.name\nORDER BY\n  orders DESC\nLIMIT\n  10",
  "statements": 1,
  "length": {
    "input": 191,
    "output": 213
  }
}

Use cases

  • Readable SQL in code reviews and documentation
  • Normalise queries before storing them in a repository
  • Format queries captured from slow-query logs
  • Compact SQL for logs and config files

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.

Related APIs

Start with 1,000 free requests every month. Get free API key →