Skip to content
PithLab

↑↓ to move · Enter to open · Esc to close

SQL Formatter and Beautifier

Beautify or minify SQL queries for many database dialects.

Use via API

Format or minify SQL. Send JSON, get JSON back.

POST https://pithlab.com/api/v1/tools/sql-formatter

curl https://pithlab.com/api/v1/tools/sql-formatter \
  -H "Authorization: Bearer $PITHLAB_KEY" \
  -H "Content-Type: application/json" \
  -d '{"input":"select id, name from users where active = true order by name"}'

Fields and responseOpenAPI documentGet an API key

About the SQL Formatter

Format messy SQL into readable, indented queries for PostgreSQL, MySQL, SQL Server, SQLite, BigQuery and more. Change keyword case, or minify SQL onto one line.

How to use the SQL Formatter

  1. Paste one or more SQL statements.
  2. Pick the dialect that matches your database so its syntax is recognised.
  3. Choose keyword case and indent width, then press Format.
  4. Press Minify to put the query on a single line with comments removed.
  5. Copy the result or download it as a .sql file.

Examples

Format a PostgreSQL query with uppercase keywords

Input

select id, name from users u join orders o on o.user_id = u.id where u.active = true and o.total > 100 order by o.created_at desc limit 10;

Output

SELECT
  id,
  name
FROM
  users u
  JOIN orders o ON o.user_id = u.id
WHERE
  u.active = TRUE
  AND o.total > 100
ORDER BY
  o.created_at DESC
LIMIT
  10;
Minify a query and remove comments

Input

SELECT  name ,  email   -- contact details
FROM   users
WHERE  bio = 'Loves  SQL' ;

Output

SELECT name,email FROM users WHERE bio = 'Loves  SQL';

Frequently asked questions

Why should I choose a dialect?

Databases differ in quoting, operators and keywords. For example MySQL uses backticks and PostgreSQL has :: casts and $$ strings. The right dialect avoids parse errors and odd formatting.

Does formatting change what my query does?

No. Only whitespace, line breaks and keyword case change. String literals, quoted identifiers and comments are kept exactly as written.

Is minifying safe?

Minify collapses whitespace outside strings, quoted identifiers and comments. Line comments are removed, or turned into block comments if you keep comments, so the query still works on one line.

Why do I get a parse error?

Usually the dialect does not match the SQL, or there is an unclosed quote or bracket. Try a more specific dialect, then check the reported line and column.

Is my SQL sent to a server?

No. Formatting runs in your browser using the open source sql-formatter library, so queries never leave your device.