Menu

SQL Formatter

Beautify SQL queries with syntax-aware indenting.

Last updated

Keywords
Indent
InputPaste SQL to begin
Output
Formatted SQL appears here — reindent, align, and copy in one click.

What is a SQL formatter?

A SQL formatter takes dense, single-line, or auto-generated SQL and rewrites it with consistent indentation and line breaks. Developers and analysts reach for it when reviewing joins, debugging filters, cleaning up ORM output, sharing queries in tickets or documentation, and stepping through query plans.

Formatting does not change what a query *does* — it changes how easily a human can read it. Once the layout is consistent, the relationship between SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, and ORDER BY becomes much easier to inspect, especially in queries that mix subqueries and CTEs.

SQL has many *dialects* — PostgreSQL, MySQL, SQLite, T-SQL (Microsoft SQL Server), Oracle, and BigQuery all extend the standard with their own syntax. A good formatter understands the dialect you choose so it doesn't break vendor-specific syntax.

What you'll learn while formatting SQL

  • SQL clauses have specific roles: SELECT chooses columns, FROM chooses tables, WHERE filters rows, GROUP BY aggregates, and ORDER BY sorts.
  • Readable indentation reveals nested queries, joins, and conditions that are otherwise invisible in a single-line query.
  • Dialects matter — RETURNING, LIMIT, TOP, and bracketed identifiers ([col] vs "col") are not portable across databases.

How to format a SQL query step by step

  1. Paste the query

    Drop your SQL into the input box. Single-line, ORM-generated, or partially formatted SQL all work.

  2. Pick the dialect

    Choose PostgreSQL, MySQL, SQLite, or T-SQL so the formatter respects vendor-specific keywords like LIMIT, TOP, or RETURNING.

  3. Configure style

    Pick keyword case (uppercase is standard) and indent width (2 or 4 spaces). Most teams pick uppercase keywords + 2-space indent.

  4. Read the formatted output

    Each clause sits on its own line, joins are aligned, and subqueries are indented. Comments are preserved.

  5. Copy back into your project

    Paste the formatted query into your migration, ORM raw query, or BI tool. Everything stays in your browser.

SQL clauses quick reference

The clauses you'll see in 95% of SELECT queries, in the order they're processed conceptually. Vendor references: PostgreSQL SELECT, MySQL SELECT.

ClausePurposeExample
SELECTPick which columns/expressions to returnSELECT id, name
FROMPick the source table(s)FROM users
JOIN ... ONCombine rows from another tableJOIN orders ON orders.user_id = users.id
WHEREFilter rows before groupingWHERE active = true
GROUP BYAggregate rows that share valuesGROUP BY country
HAVINGFilter aggregated groupsHAVING COUNT(*) > 10
ORDER BYSort the result setORDER BY created_at DESC
LIMIT / OFFSETPaginate the resultLIMIT 20 OFFSET 40
WITH ... ASCommon Table Expression (CTE) — name a subqueryWITH active AS (...)

SQL examples to try

Format a simple query

Input
select id,name from users where active=true order by created_at desc limit 10
Formatted
SELECT  id,  nameFROM usersWHERE active = trueORDER BY created_at DESCLIMIT 10;

Each clause is now on its own line and the column list is indented. Reviewing this query in code review just got 10× easier.

Inspect a join

Input
select u.name,o.total from users u join orders o on o.user_id=u.id where o.total>100
Formatted
SELECT  u.name,  o.totalFROM users uJOIN orders o ON o.user_id = u.idWHERE o.total > 100;

Aliasing tables (users u, orders o) keeps the column list short. The JOIN ... ON line makes the relationship between tables explicit.

Read a nested subquery

Input
select * from users where id in (select user_id from enrollments where course_id=5)
Formatted
SELECT *FROM usersWHERE id IN (  SELECT user_id  FROM enrollments  WHERE course_id = 5);

Indenting the inner query shows the dependency clearly. Many subqueries can be rewritten as JOINs or CTEs once the structure is visible.

Common SQL formatting mistakes

  • Assuming formatted SQL is *correct* SQL. Formatting changes layout, not semantics — a clean-looking query can still produce the wrong answer.
  • Editing a query while formatting and forgetting to re-test the result.
  • Ignoring the dialect — LIMIT 10 in PostgreSQL, TOP 10 in T-SQL, and FETCH FIRST 10 ROWS ONLY in standard SQL all do the same thing.

SQL Formatter FAQ

Does formatting SQL change the query result?
No. Formatting only changes whitespace and casing. The database parses both versions to the same query plan, so the result rows are identical.
Why format SQL?
Readable SQL is easier to debug, code-review, teach, and maintain — especially when the query has multiple joins, subqueries, CTEs, or window functions.
What is a SQL dialect?
A SQL dialect is a database-specific extension of standard SQL. PostgreSQL, MySQL, SQLite, T-SQL (SQL Server), Oracle, and BigQuery each have unique syntax beyond the SQL standard.
Can a SQL formatter fix broken queries?
A formatter can reveal syntax problems by re-emitting the query in a structured form, but it can't understand your schema or guarantee the query is logically correct.
Should I uppercase SQL keywords?
Most style guides recommend uppercase keywords (SELECT, FROM, WHERE) and lowercase identifiers, because the visual contrast makes the query structure pop. Pick one style and stay consistent.

Learn more

Other developer tools

Learn to code with Coddy

GET STARTED