Skip to content
MoneyDeck

SQL Formatter

Beautify messy SQL into clean, indented, readable queries

Updated · Free, no signup

Formatted SQL

SELECT
  u.id,
  u.name,
  COUNT(o.id) AS order_count,
  SUM(o.total) AS revenue
FROM
  users u
  LEFT JOIN orders o ON o.user_id = u.id
WHERE
  u.active = 1
  AND o.created_at BETWEEN '2026-01-01' AND '2026-06-30'
GROUP BY
  u.id,
  u.name
HAVING
  SUM(o.total) > 500
ORDER BY
  revenue DESC
LIMIT
  10;

Statements

1

Tables referenced

users, orders

Output lines

20

Tokens

77

About the SQL Formatter

This SQL formatter turns long, single-line or inconsistently written SQL into a clean, readable layout. Each clause — SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT — starts on its own line with its contents indented beneath it, every selected column gets its own line, JOINs line up under FROM, AND/OR conditions stack neatly, and subqueries are indented as their own nested block.

It is useful for developers and analysts reviewing queries copied from logs, ORMs or BI tools, preparing SQL for code review or documentation, and spotting mistakes such as a missing join condition. You can switch keywords to UPPER or lower case (identifiers and string literals are never changed), choose 2- or 4-space indentation, or minify a query back to a single line.

The formatter works with standard SQL as used by PostgreSQL, MySQL, SQLite, SQL Server, BigQuery and Snowflake. It only changes whitespace and keyword case, never the logic of the query; it also lists the tables referenced after FROM, JOIN, INTO and UPDATE. Everything runs locally in your browser. It is tuned for queries (SELECT, INSERT, UPDATE, DELETE and CTEs): DDL such as CREATE TABLE is kept valid but laid out simply, with the column list left on one line, and procedural blocks are formatted less precisely.

How to use the sql formatter

  1. 1Paste one or more SQL statements into the input.
  2. 2Choose Beautify to indent, or Minify to collapse to one line.
  3. 3Pick UPPERCASE, lowercase or preserved keywords and the indent size.
  4. 4Copy the formatted SQL; check the statement count and tables list.

Formula and method

The query is split into tokens — keywords, identifiers, numbers, quoted strings, comments, operators and punctuation — so text inside quotes or comments is never altered. Multi-word keywords such as GROUP BY, LEFT JOIN and UNION ALL are merged into single tokens.

Layout rules are then applied: a main clause keyword starts a new line at the current block level and its contents go on the next line, indented one level; top-level commas and AND/OR start new lines; JOINs are indented under FROM; BETWEEN … AND stays on one line; commas and conditions inside function calls, IN lists or CASE expressions stay inline; and a parenthesis that opens a SELECT becomes an indented sub-block closed on its own line. Semicolons end a statement and add a blank line.

Worked examples

Reporting query with JOIN, BETWEEN and HAVING

Each clause starts its own line and the four selected columns are listed one per line. The LEFT JOIN sits under FROM, the AND after WHERE starts a new line, but the AND inside BETWEEN stays inline. Keywords and functions are uppercased; the aliases and the date strings are unchanged.

Subquery, lowercase keywords, 4-space indent

The parenthesis that opens a SELECT becomes a nested block indented one level deeper than the line it starts on, and the closing parenthesis returns to the WHERE-content level. All keywords are lowercased.

Two statements: INSERT and UPDATE

Each VALUES row gets its own line while the commas inside each tuple stay inline. The escaped quote in O''Brien is preserved exactly, the semicolon ends the first statement and a blank line separates it from the UPDATE.

Minify to one line

Minify removes comments and collapses all whitespace to single spaces, keeping the unary minus attached to 1.

Frequently asked questions

Does formatting change what my SQL does?+

No. The formatter only changes whitespace, line breaks and the letter case of SQL keywords. Identifiers, string literals, numbers and comments are kept exactly as written, so the query returns the same results.

Should SQL keywords be uppercase?+

SQL keywords are case-insensitive, so it is purely a style choice. Uppercase keywords (SELECT, FROM) are the most common convention because they stand out from lowercase table and column names; many modern style guides also accept lowercase.

Which SQL dialects are supported?+

Standard SQL syntax shared by PostgreSQL, MySQL, MariaDB, SQLite, SQL Server, Oracle, BigQuery and Snowflake is supported, including CTEs, window functions, :: casts and backtick or bracket-quoted identifiers. Procedural blocks (PL/pgSQL, T-SQL BEGIN…END) are formatted less precisely.

Is my query sent to a server?+

No. Formatting runs entirely in your browser with JavaScript, so queries containing table names, customer data or credentials never leave your machine.

Why minify SQL?+

A single-line query is easier to embed in code strings, config files, log searches or command-line tools. Minifying also removes comments, which can break queries when newlines are stripped by other tools.

Related tools