Reformatting a one-line query into a readable structure
'select id, name from users where active = 1 and role = \'admin\' order by name limit 20' reformats into a multi-line structure with each major clause (SELECT, FROM, WHERE, AND, LIMIT) starting its own line, and keywords capitalized -- turning a dense one-liner into a much more scannable query.
Notice 'ORDER BY' splits across two separate lines here ('ORDER' then 'BY') -- a byproduct of this formatter treating each keyword token independently rather than recognizing multi-word clause keywords as a single unit, which is a real limitation worth watching for in more complex queries using other multi-word clauses.
Readable formatting like this matters most for debugging and code review -- a query with clauses clearly separated onto their own lines is much faster to scan for a missing WHERE condition or an unintended JOIN than the same logic packed onto one line.
Why this is a naive formatter, not a real SQL parser
Naive keyword split. Nested queries and strings are not parsed. This works by recognizing and breaking on SQL keywords in the raw text -- it doesn't build an actual parse tree of the query's structure, which means nested subqueries, complex CASE expressions, or unusual formatting inside string literals can confuse a purely keyword-based approach.
String literals containing text that happens to match a SQL keyword (like a name field containing the word 'from') could, in principle, get incorrectly treated as a keyword by a naive splitter rather than recognized as literal text inside quotes -- a risk inherent to keyword-based (rather than full-parse) formatting.
This reformats structure and capitalization only -- it doesn't validate that the SQL is syntactically correct or would actually execute against a real database; a formatter and a query validator are different tools solving different problems.
Related SQL and data-format tools
If the goal is converting query results or a single INSERT statement into JSON instead of reformatting the query text itself, the SQL to JSON Converter handles that different task.
For visualizing how multiple tables JOIN together rather than formatting the query text, a dedicated SQL Join Visualizer addresses that separate need.
Frequently Asked Questions (FAQ)
Why does 'ORDER BY' split onto two separate lines in the output?
This formatter breaks the query wherever it recognizes an individual keyword, and it treats ORDER and BY as separate tokens rather than as one combined clause keyword -- a known limitation of this naive keyword-splitting approach rather than intended formatting.
Does this validate whether my SQL is syntactically correct?
No. Naive keyword split. Nested queries and strings are not parsed. It only reformats the text structure and capitalization of recognized keywords. It doesn't parse the query into a real syntax tree or check whether it would actually execute successfully against a database.
What happens with nested subqueries?
Since this formatter works by splitting on keywords in the raw text rather than understanding query structure, a nested subquery's own SELECT/FROM/WHERE keywords get treated the same as the outer query's keywords, which can produce a confusing or incorrectly indented result for complex nested queries.
Could a string literal's contents get mistakenly treated as a keyword?
In principle, yes -- since this is a keyword-based text splitter rather than a full SQL parser, a string value that happens to contain a word matching a SQL keyword (like 'from' inside a name field) could theoretically be split incorrectly, a risk with any non-parsing, keyword-matching approach.
Is this the same as a SQL linter or syntax checker?
No. This only reformats layout and capitalization. A linter or syntax checker would validate the query's actual correctness, which is a different function this formatter doesn't perform.