Skip to content
HandyKit

SQL Formatter

Paste a query to format it with one clause per line and consistent indentation. Choose your database’s dialect, keyword case and indent. Comments are kept.

Runs in your browser. Your input is never uploaded or stored.

What does a SQL formatter do?

A SQL formatter rewrites a query with consistent line breaks, indentation and keyword case, without changing what it does. Long one-line queries, often copied from logs, ORMs or query builders, become easy to read, review and compare.

This query:

select id, name from users where active = true and created_at > '2026-01-01' order by name

becomes:

SELECT
  id,
  name
FROM
  users
WHERE
  active = TRUE
  AND created_at > '2026-01-01'
ORDER BY
  name

How to format SQL

  1. Paste your query into SQL input, open a .sql file, or drop one onto the editor.
  2. Choose your database under Dialect.
  3. Set the keyword case, indent and where AND and OR go.
  4. Select Format or press Ctrl + Enter. Copy or download the result.

Changing a setting re-formats the query straight away. If the query cannot be parsed, the error shows the line and column, and Show in input moves the cursor there.

Supported SQL dialects

PostgreSQL, MySQL, MariaDB, SQLite, SQL Server (T-SQL), Oracle (PL/SQL), BigQuery, Snowflake, Amazon Redshift, Spark SQL, Trino and Presto, DuckDB, ClickHouse, IBM Db2, Hive and Standard SQL.

Dialects matter because databases differ in syntax. PostgreSQL uses $1 parameters and operators such as ->>, SQL Server quotes names with [brackets] and limits rows with TOP, and MySQL quotes names with `backticks`. Choosing the right dialect lets the formatter recognise these.

SQL style conventions

There is no single official SQL style, but most guides agree on a few points:

  • Put each major clause on its own line: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY.
  • Indent the contents of each clause so the structure is visible at a glance.
  • Use one case for keywords. Upper case is traditional; lower case is increasingly common. Consistency matters more than the choice.
  • Start lines with AND and OR in long conditions, so each condition can be commented out or reordered easily.
  • Give tables short, meaningful aliases (customers c) and qualify columns in joins (c.id).

When a query won’t format

The formatter has to understand a query’s structure before it can lay it out, so it stops at anything it can’t parse and shows where. The usual causes are:

  • The wrong dialect. Vendor syntax such as PostgreSQL’s :: casts or SQL Server’s [bracketed names] only parses in the dialects that support it. Choose the database the query runs on.
  • Query parameters. Placeholders are written differently in each database: ?, :name, $1 or @name. Standard SQL only accepts ?, so WHERE id = :id needs a dialect that supports named parameters.
  • Template tags. dbt models and other templated SQL contain tags such as {{ ref('orders') }} or {% if %} that are replaced before the query runs. They aren’t SQL, so swap them for real table names before formatting.
  • An unclosed quote or bracket. The error points at where the formatter gave up, which is usually just after the missing character.

Formatting SQL in your editor or CI

To format SQL files automatically, use a command-line tool. This page uses the open-source sql-formatter library, which also has a command line:

# Print the formatted query
npx sql-formatter --language postgresql query.sql

# Rewrite the file in place
npx sql-formatter --language postgresql --fix query.sql

SQLFluff goes further: it lints SQL against style rules and can fix many of them, which suits teams that want every query checked in CI. For PostgreSQL, pgFormatter is another long-standing option. Whichever you choose, run the same tool and settings in your editor and in CI, so formatting never shows up as noise in code review.

Privacy

Queries often contain table structures, customer data and sometimes credentials. This formatter runs entirely in your browser: the query is never uploaded, logged or stored.

Frequently asked questions

Which SQL dialect should I choose?

Choose the database the query runs on. Dialects differ in quoting, parameters and keywords: SQL Server uses [brackets] and TOP, PostgreSQL uses $1 parameters and the ->> operator, MySQL uses `backticks`. Standard SQL works for simple queries but rejects some vendor syntax.

Does formatting change what my query does?

No. Only whitespace, line breaks and the letter case of keywords change. Table names, column names, strings and comments are left exactly as written, and the result runs the same way.

Should SQL keywords be upper case?

It is a style choice. Upper-case keywords (SELECT, FROM, WHERE) are the long-standing convention and make keywords easy to tell apart from names. Many teams now prefer lower case for easier typing. Pick one and use it consistently.

Why do I get an error for a query that runs fine?

Usually the dialect is wrong. A query using vendor-specific syntax, such as :name parameters or T-SQL brackets, may not parse as Standard SQL. Choose your database’s dialect and format again. If it still fails, check for an unclosed quote or bracket at the position shown.

Can I format several statements at once?

Yes. Separate them with semicolons and each statement is formatted in turn, with a blank line between them.

Is my SQL sent to a server?

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

Guides

  • · 2 min read

    How to Format SQL: A Readable Style Guide

    A readable SQL layout explained with a before-and-after example: one clause per line, indented items, aligned conditions, keyword case and choosing the dialect.