Tutorial · SQL Formatter · 3 min read

How to Format SQL Queries for Readability

Turn a one-line SQL query into a readable statement with uppercase keywords, one clause per line, indented conditions and properly nested subqueries.

A query pasted from a log or an ORM is one long line. Formatted, the same query reveals its shape: which tables join, what filters apply, where the subquery sits. That is the difference between a ten-second review and a ten-minute one.

What you'll learn

  • Break a query into clauses: SELECT, FROM, JOIN, WHERE, GROUP BY, ORDER BY
  • Indent AND / OR conditions and subqueries
  • Keep function calls and string literals intact

Step by step

  1. Paste the query

    Open the SQL Formatter and paste the statement. Any dialect works because formatting is keyword-based.

  2. Click Format

    Keywords are uppercased, each major clause starts a line, conditions are indented, and a subquery inside parentheses becomes its own indented block.

    select a, coalesce(b, 0) as b from t where x = 1
      and y in (select z from u) order by a
    
    →
    
    SELECT a,
      coalesce(b, 0) AS b
    FROM t
    WHERE x = 1
      AND y IN (
        SELECT z
        FROM u
    )
    ORDER BY a
  3. Copy it back

    Copy Output into your editor, migration file or ticket.

Open the tool with this example Runs in your browser. Nothing you paste is uploaded.

Common problems

An identifier was uppercased

A column or alias spelled like a keyword, such as a column named "in", is treated as one. Quote such identifiers in the source.

Commas inside a function split lines

They should not; commas are only broken at clause level. If you see it, check for an unbalanced parenthesis earlier in the query.

FAQ

Does it validate the SQL?

No. It reformats recognised keywords; a syntactically wrong query will format without complaint.