Format and beautify SQL queries with keyword capitalization and indentation
LIMIT, AUTO_INCREMENT, ENGINE) and backtick identifier quoting; PostgreSQL — supports RETURNING, ILIKE, JSONB operators, and dollar-quoted strings; SQLite — the embedded database dialect with REPLACE and special pragma support; T-SQL (Transact-SQL) — Microsoft SQL Server's dialect with TOP, MERGE, bracket identifier quoting, and stored procedure syntax. Selecting the correct dialect ensures that dialect-specific keywords are properly capitalized and formatted.SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY, HAVING, INSERT INTO, CREATE TABLE) are uppercased for visual prominence; intelligent indentation — each clause starts on a new line, subqueries are indented one level deeper, JOIN conditions align under their ON clauses, CASE expressions indent WHEN/THEN/ELSE branches; column alignment — multi-column SELECT lists align each column on its own line; parenthesis matching — nested function calls and subexpressions maintain proper bracket pairing with readable indentation. The formatter handles complex patterns correctly: CTEs (WITH ... AS), window functions (ROW_NUMBER() OVER (PARTITION BY ... ORDER BY ...)), multi-table JOINs with nested conditions, UNION/INTERSECT/EXCEPT queries, and deeply nested subqueries. All formatting uses the sql-formatter library running entirely in your browser — your database queries, which may contain table schemas revealing business logic, never leave your device.The database engine indeed executes unformatted SQL identically to formatted SQL — the query optimizer doesn't care about whitespace. However, formatted SQL has substantial practical benefits: (1) <strong>Query optimization</strong> — a formatted query reveals its logical structure. You can see at a glance whether a JOIN is INNER or LEFT, which columns are indexed in the WHERE clause, and whether a subquery could be rewritten as a JOIN. Trying to optimize a 200-line single-line query is nearly impossible because the clause structure is invisible. (2) <strong>Debugging</strong> — when a query returns unexpected results or a syntax error, finding the problematic clause in formatted SQL takes seconds; in compressed SQL, it takes much longer. (3) <strong>SQL injection prevention</strong> — formatted SQL makes it easier to identify where user input is concatenated into queries (a security anti-pattern); dynamic table/column names and string concatenation stand out visually. (4) <strong>Code review</strong> — database schema changes and query modifications in pull requests are reviewable when formatted; single-line SQL diffs are essentially unreviewable. (5) <strong>Documentation and knowledge sharing</strong> — formatted queries in documentation, runbooks, and team wikis are readable by junior developers and non-SQL stakeholders. The PivaBox SQL Formatter provides instant formatting for all these benefits — entirely private and browser-side.
SQL identifier quoting is dialect-specific, and the formatter preserves the quoting style of the selected dialect correctly. <strong>MySQL</strong> uses backticks (<code>`table_name`</code>) for identifiers and single quotes for string literals — the formatter leaves backtick-quoted identifiers untouched while uppercasing keywords. <strong>PostgreSQL</strong> uses double quotes (<code>"Table Name"</code>) for identifiers (making them case-sensitive) and single quotes for literals; the formatter respects the SQL standard double-quote convention. <strong>T-SQL (SQL Server)</strong> uses square brackets (<code>[Table Name]</code>) as its primary identifier quoting mechanism, though double quotes work with <code>QUOTED_IDENTIFIER ON</code> — the formatter handles both. <strong>SQLite</strong> accepts backticks, double quotes, and square brackets interchangeably for identifiers (for compatibility), though double quotes are the SQL standard. The formatter also correctly handles: <strong>string literals</strong> — single quotes with escaped quotes (<code>'It''s'</code> for the string It's), <strong>dollar-quoted strings</strong> in PostgreSQL (<code>$SELECT * FROM users$</code> — used for function bodies and dynamic SQL), and <strong>escape sequences</strong> in string literals. The PivaBox SQL Formatter preserves your identifier casing and quoting while normalizing only the formatting structure.
There's no universally accepted SQL style guide (unlike languages like Python with PEP 8 or Go with gofmt), but several influential guides have converged on similar conventions. This formatter follows principles aligned with Joe Celko's SQL Programming Style and Simon Holywell's SQL Style Guide: (1) <strong>Keywords UPPERCASE</strong> — improves visual scanning; SQL keywords stand out from table/column identifiers (typically lowercase). (2) <strong>Each major clause on a new line</strong> — SELECT, FROM, WHERE, JOIN, GROUP BY, HAVING, ORDER BY, LIMIT each start a new line; makes clause structure immediately visible. (3) <strong>Indentation for logical hierarchy</strong> — subqueries, JOIN conditions, and CASE branches indent to show nesting. (4) <strong>One column per line in SELECT lists</strong> — especially for queries with 5+ columns or columns with expressions/aliases. (5) <strong>Align JOIN conditions</strong> — the ON clause of a JOIN is indented under the JOIN keyword. (6) <strong>Commas at the beginning of lines</strong> — some styles prefer leading commas (<code>, column2</code> rather than <code>column1,</code>) because they're easier to comment out and add/remove; this formatter uses the trailing-comma convention (more widely used) but you can manually adjust after formatting. For team consistency, pick a style guide, configure your formatter to match, and enforce it via CI/CD (SQLFluff for linting, SQLFormat for formatting). The PivaBox formatter provides a clean, readable baseline that you can further customize manually.