What an SQL formatter does — and how this one works
A query that runs perfectly can still be a nightmare to read. Pasted from a log, an ORM, or a colleague's Slack message, SQL often arrives as a single unbroken line with random casing. A formatter parses that text and prints it back with consistent keyword casing and one clause per line, so the shape of the query — what it selects, where it joins, how it filters — becomes obvious at a glance. That readability is what lets you spot a missing join condition or an accidental cartesian product before it reaches production.
This tool is quote-aware. Rather than blindly running find-and-replace over
your text, it walks the query character by character and tracks when it is inside a single-quoted
string, a double-quoted identifier, or a backtick-quoted name. Only the keywords outside
those regions are touched — so a literal value like 'order by' stored in a column
is never mistaken for a clause and is left exactly as you wrote it.
A worked example
Paste a dense one-liner like this:
select id, email from users where status = 'active' order by created_at desc limit 10;
…and with both options enabled it becomes:
SELECT id, email
FROM users
WHERE status = 'active'
ORDER BY created_at desc
LIMIT 10;
Notice the value 'active' keeps its lower-case casing — it is data, not a keyword —
while only the recognised clauses are capitalised and broken onto their own lines.
Logical clause order vs. execution order
Formatting also makes it easier to reason about a query, because SQL does not run in the order
you write it. You write SELECT first, but the engine resolves the data sources and
filters long before it projects your columns:
| Clause | Writing order | Execution order | What it does |
|---|---|---|---|
FROM / JOIN | 2 | 1 | Picks the source tables and matches rows across them. |
WHERE | 3 | 2 | Filters individual rows before any grouping happens. |
GROUP BY | 4 | 3 | Collapses rows into groups by shared key columns. |
HAVING | 5 | 4 | Filters the grouped results (e.g. COUNT(*) > 5). |
SELECT | 1 | 5 | Projects and computes the final output columns. |
ORDER BY | 6 | 6 | Sorts the finished result set for display. |