Overview
Introduction
A SQL query written on one line, or pasted from a compact source, is hard to scan for its clauses and conditions at a glance.
This tool tokenizes your SQL and reflows it so SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY, HAVING, and LIMIT each start their own line, entirely in your browser.
What Is SQL Formatter?
A hand-written tokenize-then-reflow formatter: a scanner splits the query into keywords, identifiers, string literals, numbers, and punctuation (respecting quote boundaries and comments), then a reflow pass rebuilds the query with clause keywords on their own lines and indented continuation lines underneath.
It recognizes a broad set of standard SQL keywords, common data types, and common aggregate/scalar functions, uppercasing (or preserving) only those tokens; everything else, including string literals and identifiers, passes through untouched.
How SQL Formatter Works
The scanner walks the input character by character, treating single-quoted strings, double- or backtick-quoted identifiers, and `--`/`/* */` comments as opaque spans so nothing inside them is ever recased or split.
Adjacent keyword tokens matching known phrases (like GROUP BY, LEFT OUTER JOIN, or IS NOT NULL) merge into a single compound keyword before formatting, so multi-word clauses stay together.
During reflow, each top-level clause or JOIN keyword starts a new line at the base indent; its contents start an indented continuation line, further broken at top-level commas (in SELECT/GROUP BY/ORDER BY/SET) or AND/OR, while anything inside parentheses stays on one line.
When To Use SQL Formatter
Use it when a query pasted from a log, ORM debug output, or a single-line migration file needs to be readable during review.
It's also handy for normalizing keyword casing across a codebase that mixes SELECT and select inconsistently.
Often used alongside SQL to NoSQL Query Converter, JavaScript Formatter and Code Minifier.
Features
Advantages
- Runs entirely client-side, so queries never leave your browser.
- Recognizes a broad set of standard keywords, data types, and common functions rather than a handful of clauses.
- Never touches string literals or quoted identifiers, so values like `'select this'` keep their exact casing.
- Configurable indent width (2 or 4 spaces, or a tab) and an optional keyword-casing toggle.
Limitations
- This is a tokenizer and line-reflow pass, not a real SQL parser: it doesn't validate query correctness, and it has no notion of nested statement structure beyond top-level clause keywords.
- Content inside parentheses (subqueries, function call arguments, column-definition lists) is left on one line rather than recursively reformatted.
- DDL statements (CREATE TABLE and similar) get keyword casing but not the same clause-per-line treatment as SELECT/INSERT/UPDATE/DELETE, since their structure lives inside a single parenthesized list.
Examples
Best Practices & Notes
Best Practices
- Format a query before pasting it into a code review or documentation so clauses are easy to scan.
- Use the preserve-case toggle if your team's style guide lowercases keywords instead of uppercasing them.
- Re-run a query through this tool after editing it by hand to keep clause indentation consistent.
Developer Notes
Tokenization is a hand-written character scanner (no external SQL parser library) that tracks quote and comment boundaries; a second pass merges adjacent keyword tokens matching known multi-word phrases (longest match first), and a third pass reflows the token stream into indented lines based on a fixed set of clause- and JOIN-keyword line breaks plus top-level comma/AND/OR breaks.
SQL Formatter Use Cases
- Cleaning up a single-line query copied from a database log or ORM debug output
- Normalizing SELECT/select keyword casing across a team before committing a migration
- Making a deeply nested WHERE clause with several AND/OR conditions easier to read during a code review
Common Mistakes
- Expecting subquery or function-argument content inside parentheses to be recursively reformatted; it's left on one line since this is a reflow pass, not a full parser.
- Assuming the tool validates query correctness; it only re-formats tokens it recognizes and won't catch a missing FROM clause or a typo in a keyword.
Tips
- If a query still looks cramped after formatting, it's likely because most of its content sits inside parentheses (a subquery or large IN list), which this tool intentionally leaves on one line.
- Switch to 4-space or tab indentation if your team's SQL style guide doesn't use 2 spaces.