Skip to content
ToolkitHub

SQL Formatter

Format SQL queries

Guide

Writing SQL that people can review

By Logappradeep · ServiceNow developer

SQL is often written or logged as one long line: generated by an ORM, copied from a slow-query log, or pasted from a dashboard. A query with three joins and a dozen columns on a single line is hard to reason about and even harder to review. Formatting it so each clause starts on its own line makes the structure obvious: what is selected, from which tables, with which filters, in which order.

What this formatter does

The tool applies three transformations. First, it converts recognized keywords such as select, from, where, inner join, left join, group by, order by, having, limit, and, or, as, asc and desc to uppercase, a long-standing convention that separates SQL syntax from your table and column names. Second, it starts a new line before major clauses. Third, it puts each item after a comma on its own indented line. A query like:

select id, name from users where active = 1 order by name

becomes a SELECT list with one column per line, followed by separate FROM, WHERE and ORDER BY lines. That layout makes it easy to comment out a column, spot a missing join condition, or see that a filter belongs in HAVING rather than WHERE.

Typical uses

  • Reading queries captured from application logs or database monitoring tools.
  • Preparing a query for a code review, a ticket or documentation.
  • Comparing two versions of a query in a diff tool after formatting both the same way.
  • Cleaning up SQL written quickly in a database console before saving it.

Things to be careful with

The formatter works on text patterns, not a full SQL grammar, so it does not know where string literals begin and end. A value such as 'order by date' inside quotes will be uppercased and split just like real SQL, which changes your data. Commas inside function calls like COALESCE(a, b) or IN (1, 2, 3) are also broken onto new lines. Review literals after formatting, or format the query structure and paste string values back.

Dialects differ too. PostgreSQL, MySQL, SQLite and SQL Server share core syntax, but features such as RETURNING, TOP, window functions or backtick-quoted identifiers are not specially handled. Uppercasing keywords is purely cosmetic: SQL keywords are case-insensitive in every major database, although identifiers may be case-sensitive depending on quoting and the database.

Formatting never executes anything. Your query stays in the browser, it is not sent to a database or a server, and the last input is remembered locally so you can come back to it.

Frequently asked questions

Does uppercasing keywords change what the query does?

No. SQL keywords are case-insensitive in all major databases. Only text inside quotes can be affected, so check string literals after formatting.

Why were commas in my function call split onto new lines?

The formatter breaks after every comma, including those inside functions and IN lists. You can rejoin those lines by hand if you prefer them compact.

Does it support stored procedures or multiple statements?

It will format them, but it has no special handling for procedural blocks like BEGIN and END. It works best on individual queries.

Is my query executed or sent anywhere?

No. The query is only transformed as text in your browser.