An online SQL formatter that reads your dialect

Free SQL formatter with pages for T-SQL, PostgreSQL, MySQL, SQLite, Oracle, Snowflake, BigQuery and Redshift, plus minify, a change report and every app setting.

By

Most SQL arrives as one long line. It comes out of an ORM log, a monitoring tool, a colleague's chat message or a stored procedure someone wrote in 2011. Before you can reason about it you have to lay it out, and the usual online formatters will do that happily while quietly breaking it: a MySQL ->> turned into -> >, a PostgreSQL @> split in two, a GO folded into the line above it.

We rebuilt the free SQL formatter on sqlly.app around the rule that a formatter must never change what a query does. It now has a page for each dialect, every layout setting from the desktop app, a report of what it changed (or why it refused), and a Minify button. This post covers what it does and how.

A worked example

Here is a MySQL query with a JSON extraction, a null-safe comparison and a reserved word used as a table name, pasted as a single line:

select o.id, o.meta->>'$.status' as status, count(*) as n from `order` o where o.paid <=> 1 and o.created_at > now() - interval 7 day group by o.id, status having count(*) > 1 order by n desc

With the dialect set to MySQL and the Standard style, the formatter returns this:

SELECT
    o.id,
    o.meta ->> '$.status' AS status,
    COUNT(*) AS n
FROM
    `order` o
WHERE
    o.paid <=> 1
    AND o.created_at > NOW() - INTERVAL 7 day
GROUP BY
    o.id,
    status
HAVING
    COUNT(*) > 1
ORDER BY
    n DESC

That is genuine output, produced by running the query through the same Rust crate the page loads. The details worth checking: ->> and <=> are each still one operator, `order` keeps its backticks, and COUNT(*) has no space before the parenthesis. That last one matters, because MySQL rejects COUNT (*) unless the server runs with IGNORE_SPACE.

The status line under the result counts what changed besides whitespace. For this query it is 16 words recased and nothing else: no names requoted, no parentheses added, no comments reflowed. Switch the result pane to Changes and you get a line-by-line comparison with your input.

Nine dialect choices, eight landing pages

The dialect menu offers Auto-detect, T-SQL (SQL Server, Azure SQL), PostgreSQL, MySQL / MariaDB, SQLite, Oracle, Snowflake, BigQuery and Redshift. Each named dialect also has its own page that opens the tool with that dialect preselected and lists what the formatter does with its syntax:

  • SQL Server (T-SQL): [bracketed] names, @variables, @@ROWCOUNT, #temp and ##global tables, OUTPUT $action, and GO lines (including GO 5) that always stay on their own line.
  • PostgreSQL: operators read the way Postgres reads them, as one run of operator characters, so #>>, @>, ?| and custom operators survive. :: casts, arrays and slices, and $$ bodies are kept as written.
  • MySQL and MariaDB: backticks, # comments, /*! … */ executable comments, @rank and @@sql_mode variables, and prefixed literals like _utf8mb4'…'.
  • SQLite: all three quoting styles, every parameter form (?, ?1, :name, @name, $name), JSON arrows and x'ff' blobs.
  • Oracle: q'[…]' strings, :binds, table@dblink, %TYPE, and the SQL*Plus / line that ends a PL/SQL block.
  • Snowflake, BigQuery and Redshift, which the formatter reads with its PostgreSQL rules (Snowflake, Redshift) or its MySQL rules (BigQuery), plus their own syntax on top: src:path::type access, backtick project.dataset.table paths, @parameters, and QUALIFY laid out as a clause like HAVING.

Auto-detect picks T-SQL when it sees bracketed names, @ parameters or a GO line, MySQL when it sees backticks, and PostgreSQL otherwise. Each page also has a "Good to know" list where we say what is not handled. Snowflake stage references such as @my_stage/path/ are one example: SQL containing one comes back unchanged, with a note pointing at the @. T-SQL and PL/SQL blocks stay valid and their statements get laid out, but IF and LOOP structure is not re-indented.

When it refuses, it says where

The formatter re-lexes its own output and compares it with your input, token by token. If anything other than whitespace and the casing you asked for would differ, you get your SQL back untouched. SQL that does not lex cleanly is also left alone. Paste select 'O''Brien, name from t and the report the page receives looks like this:

{"status":"declined",
 "reason":{"code":"lexError","kind":"unterminatedString","line":1,"column":8}, ...}

The page turns that into a sentence and a Show me where button that puts the cursor on line 1, column 8, where the unterminated string starts. Silently reformatting a broken query and handing it back looking tidy would be worse than doing nothing.

The same report carries the output's highlight tokens, taken from the formatter's own lexer. The colors you see in the result pane are the formatter's reading of your SQL, so if a string is colored as a string, the formatter treated it as one.

Every setting from the app

The quick controls cover the choices most people want: a style (Standard, Compact, Expanded, or Keep my layout), keyword casing, 4 spaces, 2 spaces or tabs, trailing or leading commas, and whether short queries stay on one line. Here is the example above with lowercase keywords and leading commas:

select
    o.id
    , o.meta ->> '$.status' as status
    , COUNT(*) as n
from
    `order` o
where
    o.paid <=> 1
    and o.created_at > NOW() - interval 7 day
...

Function names have their own casing setting, which is why COUNT and NOW stayed uppercase. That setting lives under Advanced settings, which exposes every layout option the desktop app's Editor › Formatting pane has, in the same 13 groups: keyword casing (with optional per-category casing for data types, functions, clauses, joins, DML, DDL and more), identifiers and quoting, clause layout, the SELECT list, conditions, JOINs, functions, subqueries and CTEs, INSERT/UPDATE/DELETE/MERGE, DDL, comments, indentation, and line length.

The panel is not a hand-copied list. The wasm module exports a schema built from the formatter's own enums, so the page can only offer values the formatter knows, and a test fails if a new formatter setting is added without appearing in the panel. Quoting options respect each dialect's case rules: Remove unnecessary quotes leaves "Users" quoted in PostgreSQL, because Postgres folds unquoted names to lowercase and removing the quotes would point at a different table.

Your settings are remembered in the browser and also kept in the page address after the #. Copy settings link gives you a URL your team can open to get the same style. The link holds settings only, never your SQL.

Minify

Sometimes you need the opposite: one line for a log message, a JSON payload or a shell command. Minify collapses the query onto as few lines as possible. Our example becomes:

SELECT o.id, o.meta ->> '$.status' AS status, COUNT(*) AS n FROM `order` o WHERE o.paid <=> 1 AND o.created_at > NOW() - INTERVAL 7 day GROUP BY o.id, status HAVING COUNT(*) > 1 ORDER BY n DESC

It drops ordinary comments but keeps the ones that carry meaning: optimizer hints (/*+ … */ and Oracle's --+), MySQL and MariaDB executable comments, whose contents the server runs, and -- sqlly directive comments. A GO or an Oracle / keeps its own line, because the tools that read them require it. Minify also never adds a space where you wrote none, and like Format it checks that its result holds exactly the same SQL before handing it over.

The same formatter as the app, running in your tab

The page does not call a server to format anything. The formatter is the sqlformat module of our sqlly-lang Rust crate, the code behind Format SQL and Minify SQL in the SQLly desktop app and behind formatting in its language server. A thin wasm-bindgen wrapper compiles it to WebAssembly and exposes formatSql, minifySql, their report variants and the settings schema to the page's JavaScript. Options travel in the app's own sql-formatting.json shape, so a style you build on the page is the style the app applies.

Because it runs in your tab, nothing you paste or open is uploaded, logged or stored. Dropped .sql files are read by the browser. The examples on each dialect page are generated output too: a test runs every example through the formatter and fails if the stored "after" no longer matches, so the page cannot drift from what the tool actually does.

If you want a section left exactly as written, wrap it in -- formatter:off and -- formatter:on.

Try it

Paste something ugly into the SQL formatter, or start from your dialect's page. If you would rather have it in your editor, with Format on save and Format on paste, it ships in the SQLly desktop app for macOS, Windows and Linux, along with the rest of the free tools.