Skip to main content

SQL Formatter

Format SQL queries with clause-level line breaks and keyword case control.

Written by Golam Rabbani, Founder & Lead Engineer

How to use this sql formatter

  1. Paste your SQL query into the input area.
  2. Pick the indent size — 2 spaces, 4 spaces, or tab.
  3. Choose keyword case: UPPERCASE, lowercase, or preserve as-typed.
  4. Press Format to rewrite the query with line breaks at every clause.
  5. Use Copy to grab the result or Reset to clear the form.

About this sql formatter

The SQL formatter tokenises your query, identifies reserved words, then re-emits it with a new line and consistent indentation in front of each top-level clause (SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING, LIMIT, JOIN variants, UNION) and each Boolean joiner (ON, AND, OR). Strings, identifiers, numbers, comments, and operators are preserved exactly. Sub-queries inside parentheses are indented one additional level so the structure is obvious at a glance.

Because everything runs locally in your browser, you can paste production queries with embedded constants, secrets, or customer identifiers without worrying about leaks. The keyword case toggle is useful when joining a codebase that has chosen UPPERCASE for keywords (the most common SQL house style) or one that prefers lowercase for consistency with column names.

For example, the messy input "select id, name from users where active=1 and role='admin' order by name" with 2-space indent and UPPERCASE keywords becomes: SELECT id, name FROM users WHERE active = 1 AND role = 'admin' ORDER BY name

That is much easier to scan in a pull request than the original single-line form.

FAQ

Which SQL dialects are supported?
The formatter targets the common ANSI SQL keyword set shared by Postgres, MySQL, SQLite, SQL Server, and Oracle. Dialect-specific identifiers and operators pass through unchanged because they look like regular tokens to the parser.
Will it change my string literals or identifiers?
No. Quoted strings, backtick-quoted identifiers, and numeric literals are preserved character-for-character. The keyword case option only affects reserved words.
Are SQL comments kept?
Yes. Both -- single-line comments and /* */ block comments are detected during tokenising and re-emitted in place so they continue to document the surrounding clauses.
Does it validate the query?
No. The formatter is a structural rewriter, not a parser for a specific database. An invalid query still gets formatted; whether it runs is up to your database.
Why does each AND/OR start on a new line?
Putting Boolean joiners at the start of their own line is the standard style used by most teams — it makes it easy to comment out a single condition or read down the column to see the predicate chain.
Is the query sent to a server?
No. The SQL formatter runs entirely client-side. Your query never leaves your browser.