Dev Tools Format SQL Formatter & Beautifier
100% client-side · No signup · Free forever

SQL Formatter & Beautifier

Paste a cramped one-line query and get clean, indented SQL with keywords aligned. Works with MySQL, PostgreSQL & ANSI SQL. 100% client-side.

1
Format tool
SQL Formatter & Beautifier
Live
SQL Input
Formatted SQL

A SQL formatter turns a cramped, single-line query into a readable, properly indented statement with keywords aligned and clauses on their own lines. Whether you have copied a query out of an ORM log, an application stack trace, a BI tool, or a colleague's Slack message, a tangled SELECT with five joins and a nested sub-query is hard to reason about until it is formatted. This SQL beautifier uppercases reserved keywords, places major clauses (SELECT, FROM, WHERE, GROUP BY, ORDER BY, JOIN) on fresh lines, indents the column list and join conditions, and breaks long comma-separated lists so each column or value sits on its own row. It works with MySQL, PostgreSQL, SQL Server, SQLite, and standard ANSI SQL. Everything happens in your browser — queries are never sent to a server, which matters when a statement contains real table names, schema details, or literal values.

How to Format a SQL Query

Three steps. (1) Paste your raw SQL — a single line, a multi-statement script, or a messy hand-edited query — into the input box. (2) Click Format SQL. (3) Copy the beautified result. The formatter tokenises the statement, recognises SQL keywords case-insensitively, and rebuilds the query: top-level clauses start a new line at zero indent, the columns after SELECT are indented one level and split on commas, each JOIN and its ON condition gets its own line, and boolean operators (AND, OR) in the WHERE clause are aligned for easy scanning. Parentheses for sub-queries and function calls increase the indent so nested logic is visually obvious. String literals and quoted identifiers are preserved verbatim, so your data and column names are never altered — only the surrounding whitespace and keyword casing change.

When to Use a SQL Beautifier

  • Reading ORM-generated SQL — Hibernate, Django ORM, ActiveRecord, Prisma, and Sequelize all emit dense one-line queries in their logs.
  • Code review — format both versions of a query and diff them with our text diff checker.
  • Debugging slow queries — a formatted EXPLAIN target is far easier to optimise.
  • Documentation — paste clean, indented SQL into a README, runbook, or wiki.
  • Learning SQL — see the structure of an unfamiliar query at a glance.
  • Migrations & reports — tidy up hand-written DDL or analytics queries before committing them.

SQL Formatting in Code

This tool ships a lightweight client-side formatter, but in your own projects you can use a dedicated library:

// JavaScript / Node.js — the 'sql-formatter' npm package
import { format } from 'sql-formatter';
format('select id,name from users u join orders o on o.uid=u.id where u.active=1', {
  language: 'postgresql',
  tabWidth: 2,
  keywordCase: 'upper',
});
/* =>
SELECT
  id,
  name
FROM
  users u
  JOIN orders o ON o.uid = u.id
WHERE
  u.active = 1
*/

# Python — the 'sqlparse' library
import sqlparse
print(sqlparse.format(raw_sql, reindent=True, keyword_case='upper'))

CLI: npx sql-formatter query.sql formats a file in place.

Dialects, Keyword Casing & Limits

SQL is not one language but a family of closely related dialects. ANSI SQL defines a common core, but MySQL, PostgreSQL, SQL Server (T-SQL), Oracle (PL/SQL), and SQLite each add their own keywords, functions, and quoting rules — backticks in MySQL, double quotes for identifiers in Postgres, square brackets in SQL Server. This formatter focuses on layout and standard keyword recognition, so it works across dialects for the vast majority of SELECT/INSERT/UPDATE/DELETE and DDL statements; it does not execute, validate, or rewrite your query, so it can never change the result of running it. Convention is to uppercase reserved keywords (SELECT, WHERE, JOIN) while leaving table and column names in their original case — this is exactly what the tool does. For extremely complex statements with deeply nested CTEs or vendor-specific procedural blocks, a server-side library such as sql-formatter with an explicit dialect gives the most precise result. After formatting, you can compare query versions with our diff checker.

Frequently Asked Questions

Everything you need to know about the SQL Formatter & Beautifier.

Is this SQL formatter free and private?

Yes, it is completely free with no signup. All formatting runs in your browser, so your queries — including real table names and literal values — are never sent to a server.

Which SQL dialects are supported?

The formatter focuses on layout and standard keyword recognition, so it works across MySQL, PostgreSQL, SQL Server, SQLite, and ANSI SQL for the vast majority of SELECT, INSERT, UPDATE, DELETE, and DDL statements.

Does formatting change what my query does?

No. The tool only adjusts whitespace and keyword casing. It never executes, validates, or rewrites the logic of your query, so the formatted version produces the exact same result when run.

Should SQL keywords be uppercase?

It is a widely followed convention to uppercase reserved keywords like SELECT, FROM, and JOIN while leaving table and column names in their original case. This tool follows that convention to improve readability.