01What is SQL Formatter?
A 400-character query pulled from a log file is hard to review. This tool lays it out properly. It runs your SQL through the open-source sql-formatter library, inside your browser, and puts each clause on its own line with indented column lists. Subqueries, CTEs and CASE expressions are nested so you can see the structure. Dialect is the first thing to set. The Dialect menu covers Standard SQL, PostgreSQL, MySQL, MariaDB, SQLite, SQL Server (T-SQL), Oracle PL/SQL, BigQuery, Snowflake, Amazon Redshift, DuckDB, ClickHouse, Spark SQL, Hive, Trino / Presto, IBM Db2, IBM Db2 for i, TiDB, SingleStoreDB and Couchbase N1QL. It matters because each database has its own operators, quoting and keywords. For example, select data->>'name' from docs where tags @> '{a}' fails on Standard SQL with a parse error at the @> operator. Switch to PostgreSQL and it formats cleanly, with data ->> 'name' under SELECT and tags @> '{a}' under WHERE. You can choose the indent (2 Spaces, 4 Spaces or Tab) and the case. UPPER, lower or As typed applies to keywords, function names and data types together. Identifiers such as table and column names are left exactly as you wrote them. So SELECT Id FROM T in lower mode becomes select, Id, from and T on separate lines. There is also a Minify checkbox, which goes the other way and produces one line. It removes -- and /* */ comments, but keeps optimizer hints written as /*+ ... */ and MySQL's /*! ... */ comments. Whitespace collapses to single spaces. String literals, quoted identifiers and PostgreSQL $$ bodies are left untouched. In MySQL, MariaDB, TiDB and SingleStoreDB, # also starts a comment. In other dialects it's kept as text. The output gets basic syntax colouring, and you can copy it or download it as formatted.sql. What it won't do: it formats, it doesn't validate. A query can format perfectly and still reference a missing column or fail on your server. Upper-case mode can also capitalise column names the dialect treats as keywords, like day or year. Your query text is handled by the library in the page and isn't sent to a server.
Backend developers and data engineers who read ORM-generated or logged SQL and want it laid out clause by clause for review. The tool runs entirely in your browser, so your data never leaves your device.
02How to use SQL Formatter
- Paste your query into the SQL Input panel, or load one of the examples
- Choose your database in the Dialect menu
- Set Indent (2 Spaces, 4 Spaces or Tab) and the case (UPPER, lower or As typed)
- Read the formatted output, or tick Minify to get a single line
- Click Copy, or Download to save it as formatted.sql
03Worked examples
- Input:
select id, name from users where active = 1 and age > 18 order by name (Standard SQL, 2 Spaces, UPPER) - Result:
SELECT id, name FROM users WHERE active = 1 AND age > 18 ORDER BY name - Each clause starts a new line and AND conditions line up under WHERE.
- Input:
select u.name, count(o.id) as n from users u left join orders o on o.user_id = u.id group by u.name - Result:
SELECT u.name, COUNT(o.id) AS n FROM users u LEFT JOIN orders o ON o.user_id = u.id GROUP BY u.name - With UPPER case, function names like COUNT are upper-cased too, while table aliases and column names stay as typed.
- Input:
SELECT Id FROM T (case: lower) - Result:
select Id from T - Keyword case only changes keywords; identifiers such as Id and T keep their case.
- Input:
SELECT a, -- note b FROM t WHERE x = 'a b' # hash (Minify, MySQL) - Result:
SELECT a,b FROM t WHERE x = 'a b' - Minify drops comments and extra whitespace but leaves the double space inside the string literal alone; # is only treated as a comment in MySQL-style dialects.
- Input:
SELECT /*+ INDEX(t i) */ a /* plain */ FROM t (Minify) - Result:
SELECT /*+ INDEX(t i) */ a FROM t - Optimizer hints in /*+ */ survive minification; ordinary block comments are removed.
- Input:
select data->>'name' from docs where tags @> '{a}' (PostgreSQL, 2 Spaces, UPPER) - Result:
SELECT data ->> 'name' FROM docs WHERE tags @> '{a}' - On Standard SQL the same query stops with a parse error at @>, because only the PostgreSQL dialect knows these JSON operators.
04Common use cases
- Laying out a long query copied from Django, SQLAlchemy or Prisma debug logs before reviewing it
- Formatting a migration or report query so a pull request diff is readable
- Making a query with several CTEs and window functions readable for a runbook or wiki page
- Collapsing a formatted query to one line with Minify for a log message, a psql -c argument or a JSON config
- Finding which dialect a vendor-specific snippet needs by switching Dialect until the parse error goes away
- Normalising keyword case across a set of saved queries before committing them
05Tips
- A parse error on Standard SQL usually means vendor syntax like @>, ->> or backtick quoting. Pick your real database in Dialect
- UPPER mode can turn columns named day, date or year into DAY, DATE or YEAR. Choose As typed if you want them untouched
- Minify treats # as a comment only for MySQL-style dialects, so pick the right one before collapsing MySQL queries
- Optimizer hints in /*+ ... */ survive Minify, so index hints are safe to collapse
- For identical formatting across a team, run the same sql-formatter library from the CLI or an editor plugin
06Related concepts
Common Table Expression, Window functions, Query optimizer, SQL dialect.
Frequently Asked Questions
What is SQL Formatter?
SQL Formatter is a free, browser-based developer tool on AIDToolStack. Beautify and format SQL queries with customizable indentation. Supports multiple SQL dialects. It requires no installation or sign-up and works entirely in your browser.
Is SQL Formatter free to use?
Yes. No sign-up, no fees and no usage cap.
Does SQL Formatter store my data?
No. It runs in your browser; nothing is uploaded.
Video
From our YouTube channel. The player loads from YouTube only when you press play.