← Back to all posts

How do I format SQL online? Uppercase keywords and indentation rules

Generic SQL layout: breaking SELECT fields onto new lines, indenting WHERE continuations, subqueries and CASE WHEN — with the TwoThink SQL formatter running entirely in the browser.

Paste select id,name from users where age>18 into a chat window or a document and the structure is hard to read at a glance. The value of online SQL formatting is not "changing the syntax" but making it readable.

The generic rules (MVP)

The TwoThink SQL formatter uses generic SQL rules and does not bind itself to the MySQL / PostgreSQL dialects (dialect differences are planned for phase 2):

  1. Uppercase keywords: SELECT, FROM, WHERE, JOIN, GROUP BY and friends
  2. Clauses on their own lines: each main clause gets a line
  3. AND / OR continuations: indented by two spaces relative to WHERE
  4. The SELECT field list: a new line after each comma
  5. Parenthesised subqueries: extra indentation after (, and back out at )
  6. CASE WHEN: WHEN / THEN / ELSE on their own indented lines

Formatting versus minifying

  • Format: multiple lines with indentation — good for code review and documentation
  • Minify: the extra whitespace is removed onto a single line — good for log fields or config values

Both happen locally in your browser, and nothing is uploaded.

What it will not do

  • It does not execute SQL and does not connect to a database
  • It does not do full syntax validation (incomplete statements are still laid out as far as possible)
  • It does not generate ORM code

How it pairs with the other tools

  • To compare two queries: format them first, then use the text diff
  • For an API response: use the JSON formatter to inspect the structure