Codepackr / formatters / SQL Formatter

SQL Formatter

Format SQL queries with standard indentation and uppercase keywords.

Client-Side Execution & Privacy Guarantee

This utility executes 100% locally in your web browser. Your inputs, tokens, queries, and data payloads are never transmitted to any external server or backend.

SQL written by ORMs, query builders, or hurried developers often appears as a single dense line. Consistent indentation and keyword casing make joins, subqueries, and predicate groups far easier to audit. This formatter rewrites the statement so major clauses sit on their own lines, keywords are uppercased, and nested expressions receive progressive indentation.

Processing stays inside the browser. You can paste queries that contain table names, column lists, or literal values from a production database without transmitting them. The result is ready for a code review, a documentation page, or a ticket comment.

The formatter is deliberately conservative: it improves presentation without attempting to repair broken markup or rewrite dialect-specific constructs.

Clause and keyword layout

Major clauses—SELECT, FROM, WHERE, GROUP BY, ORDER BY, LIMIT—each begin on a new line. Keywords are rendered in uppercase while identifiers and literals keep their original case. This visual distinction helps the eye separate structure from data.

Comma-separated lists (column lists, IN lists, VALUES rows) are broken so that each item occupies its own indented line when the list is longer than a few elements. Short lists may remain on one line to avoid unnecessary vertical growth.

Consistent visual structure also reduces the chance that a later automated diff or review tool will flag pure whitespace changes as meaningful edits.

Joins and subqueries

Each JOIN (INNER, LEFT, RIGHT, FULL) starts on a new line and is indented relative to the FROM clause. The ON condition that follows is indented one additional level so the join predicate is visually attached to its table.

Subqueries inside parentheses receive the same treatment as top-level statements, indented one level deeper. This makes correlated subqueries and derived tables easier to follow without losing the surrounding context.

What stays unchanged

String literals, numeric literals, and identifier quoting are left exactly as written. The formatter does not attempt to rewrite functions, cast syntax, or dialect-specific extensions; it only rearranges whitespace and normalizes keyword case.

Comments (both -- line comments and /* block comments */) are preserved and placed near the clause they originally annotated. The tool does not validate that the SQL is executable; it only improves presentation.

When to use this tool

  • Cleaning a query copied from an application log
  • Preparing SQL examples for documentation or a pull request
  • Making a long JOIN chain readable before optimization
  • Standardizing keyword case across a team’s query library
  • Inspecting a generated statement from an ORM

How to use the SQL Formatter

  1. Paste the SQL statement into the input area.
  2. Select the desired indent size (usually two or four spaces).
  3. Click Format. The indented, uppercased result appears at once.
  4. Scan for any clauses that still feel dense and adjust manually if needed.
  5. Copy the formatted query or download it as a .sql file.
  6. If the original contained dialect-specific syntax, verify the output still parses in your database.
  7. Clear the input when finished.

Worked examples

Simple SELECT with WHERE

Input
select id, name from users where active = 1 order by name
Output
SELECT
  id,
  name
FROM users
WHERE active = 1
ORDER BY name

Keywords uppercased; each major clause on its own line.

Join with condition

Input
select u.name, o.total from users u inner join orders o on u.id = o.user_id where o.total > 100
Output
SELECT
  u.name,
  o.total
FROM users u
INNER JOIN orders o
  ON u.id = o.user_id
WHERE o.total > 100

JOIN and ON indented to show the relationship clearly.

Common mistakes and tips

  • Uppercase keywords make structure pop; keep identifiers in their original case for clarity.
  • Break long IN lists and column lists so each item is easy to scan.
  • The formatter does not add or remove parentheses; verify operator precedence yourself.
  • Dialect-specific constructs (e.g., PostgreSQL :: casts or MySQL backticks) are preserved.
  • Always test the formatted query in your database if the original was machine-generated.

Frequently asked questions

Does it support multiple statements?

It formats one statement at a time. Separate statements with semicolons and process them individually if needed.

Will it uppercase table or column names?

No. Only SQL keywords are uppercased. Identifiers retain the case you supplied.

Can it format stored procedures or functions?

Basic formatting applies, but complex procedural blocks may need manual adjustment.

Are comments preserved?

Yes. Both line and block comments stay in place and are indented with the surrounding clause.

Does formatting change query results?

No. Only whitespace and keyword case change; the semantic meaning remains identical.