FIRST CH TOOLS / 29 SQL Formatter

SQL Query Formatter & Beautifier

Paste the kind of one-line SQL you pull out of a log or a code file on the left, and a query with uppercase keywords, one clause per line and real indentation comes out on the right. JOIN … ON, subqueries and CASE expressions each land at their own depth. Indent width, comma position and where AND goes are yours to choose.

Paste and it formats as you type

Paste (SQL)
Formatted
0
Lines
0
Statements
0
Subquery depth
0
Keywords
Preview
What we found

    Nothing you paste leaves the browser — the lexer and the formatter both run on this page. Loading a file only reads it from your device; nothing is uploaded. You can call the tool straight from a URL: /en/sql-format/?sql=select%20*%20from%20t%20where%20id%3D1 or /en/sql-format/?sql=…&case=lower&indent=2&comma=leading

    How to Use

    1. Paste the queryDrop the one-line query you pulled out of a log or a code file into the left box. You can also drag and drop a .sql file.
    2. Pick your styleKeyword case, indent width (2/4/8 spaces or a tab), comma position and where AND goes. Tick Collapse onto one line to go the other way.
    3. Copy the resultThe formatted query appears on the right. The preview below colour-codes keywords, strings and comments, and anything risky shows up in the findings list.

    About This Tool

    It turns one-line SQL into clauses on their own lines. Queries from application logs, from an ORM, or pasted into a chat almost always arrive with the newlines squashed out. This tool splits the query into tokens (keywords, identifiers, strings, operators, comments) and rebuilds it with SELECT, FROM, WHERE, GROUP BY and ORDER BY at the start of a line and their contents indented one level. The meaning does not change — SQL only sees whitespace as a separator.

    Keywords are cased; identifiers are not. Keywords and type names are matched against a keyword table and raised to uppercase (or lowered), but table, column and alias spellings are left exactly as typed. Identifier folding differs per database (PostgreSQL folds to lowercase, Oracle to uppercase) and quoted identifiers are case-sensitive, so changing them would be a real risk. Anything inside "…", `…`, […] or '…' is passed through byte for byte.

    Parentheses are treated by what is inside them. Only a ( followed by SELECT, WITH or VALUES becomes an indented subquery; everything else (SUM(…), COALESCE(…), IN (1, 2, 3), OVER (PARTITION BY …)) stays on one line, because stretching argument lists vertically makes them harder to read, not easier. CASE expressions are laid out with WHEN, ELSE and END stacked, and the AND in BETWEEN a AND b never becomes a line break.

    It flags the dangerous shapes while it formats. An UPDATE or DELETE with no WHERE (every row is affected), unbalanced parentheses or quotes, SELECT *, tables comma-joined in the FROM clause, and whether placeholders are used all show up under “What we found”. These are exactly the things you miss while squinting at a single long line.

    It bends to your team's style. Indent with 2, 4 or 8 spaces or a tab; put commas at the end or the start of a line; put AND / OR first or last. Break after every clause gives a fully expanded layout where even FROM and WHERE put their contents on the next line, and Collapse onto one line folds everything back into a single line for pasting elsewhere. Every setting has a URL parameter, so an internal doc can link to the exact style you use.

    To review what changed, pair this with the Diff Checker; to paste a schema or a result set into a document, use the Markdown Table Generator; to check when a batch job actually fires, use the Cron Explainer.

    From AI Agents

    This formatting logic is also available as the sql_format tool of the MCP (Model Context Protocol) server @first-ch/tools-mcp, so an AI agent can call it directly without a browser. It can read and write files by path too. See Using these tools from AI agents for setup details.

    Setup

    claude mcp add firstch-tools -- npx -y @first-ch/tools-mcp

    Examples

    # Format a one-line query (uppercase keywords, one clause per line)
    sql_format(text="select a,b from t where a=1 and b=2")
    
    # Read a .sql file, format it and write it back
    sql_format(path="/tmp/report.sql", outputPath="/tmp/report.sql")
    
    # Match a house style (2 spaces, leading commas, lowercase keywords)
    sql_format(text="select a, b from t", indent="2", commaStyle="leading", keywordCase="lower")
    
    # Collapse a formatted query back onto one line
    sql_format(text="SELECT\n    a\nFROM t", compact=true)

    Other Tools