Skip to content

SQL Formatter

Paste a query and get it back laid out for reading: one clause per line, columns and conditions listed beneath, subqueries indented, keywords in the case you choose. Or switch to Minify for the shortest safe one-line version. Choose a dialect so quotes, comments and operators are read the way your database reads them. The formatter changes whitespace and keyword case only, it never runs your SQL, and nothing is uploaded. Free, no sign-up.

Action

Formatted in your browser as you type. Nothing is uploaded, and the SQL is never run.

Options

Standard SQL: single quotes for text, double quotes for identifiers, -- and /* */ comments.

Paste some SQL, or load the example, to format it.

How to Use the SQL Formatter

  1. 1. Choose Format or Minify. Format lays the query out for reading; Minify removes everything a database does not need.
  2. 2. Paste your SQL. One statement or many, separated by semicolons. A query copied from a log, an ORM or a single-line string works as well as one you typed. Load the example shows a typical report query.
  3. 3. Pick the dialect and layout. The dialect decides how quotes, comments and operators are read; the options set keyword case, indentation (2 spaces, 4 spaces or a tab), the width at which brackets break, comma position and blank lines between statements.
  4. 4. Check the notes, then copy or download. The tool reports unbalanced brackets and unterminated strings or comments, which would otherwise make the layout unreliable.

How the Layout Works

  • Every clause starts a line. SELECT, FROM, each JOIN, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, UNION, VALUES, SET and RETURNING all begin at the left edge of their block.
  • One item stays on its line; a list goes beneath. FROM users u and LIMIT 10 stay on the clause line. Several columns, several tables or several AND and OR conditions go one per line, indented under the clause word.
  • Joins keep their conditions with them. ON goes on its own indented line when it is long or has more than one condition, with each AND below it.
  • Subqueries and CTEs indent. A bracket that opens a SELECT, a WITH list or a VALUES list breaks across lines, and its closing bracket returns to the level where it opened.
  • Other brackets wait until they are too long. A function call or IN list stays on one line until it passes the break width, then breaks at its commas. A CASE with two or more WHEN branches always expands.
  • Comments stay put. A comment at the end of a line stays at the end of that line, and a comment on its own line stays on its own line.

A Worked Example

The example is one 289-character line. With the defaults it becomes 18 lines and 309 characters; the extra 20 are indentation and line breaks.

Before

select u.id, u.name, count(o.id) as orders, sum(o.total) as revenue from users u left join orders o on o.user_id = u.id and o.status <> 'cancelled' where u.created_at >= '2024-01-01' and u.country in ('US', 'CA') group by u.id, u.name having count(o.id) > 2 order by revenue desc limit 10;

After

SELECT
  u.id,
  u.name,
  COUNT(o.id) AS orders,
  SUM(o.total) AS revenue
FROM users u
LEFT JOIN orders o
  ON o.user_id = u.id
  AND o.status <> 'cancelled'
WHERE
  u.created_at >= '2024-01-01'
  AND u.country IN ('US', 'CA')
GROUP BY
  u.id,
  u.name
HAVING COUNT(o.id) > 2
ORDER BY revenue DESC
LIMIT 10;

Minify turns the same query into 275 characters, 4.8% shorter, by removing the spaces around commas, operators and brackets: u.id,u.name,count(o.id) and in('US','CA'). Both versions have exactly the same tokens in the same order.

Keyword Case: What Changes and What Never Does

Only words that are reserved in every mainstream dialect, such as SELECT, FROM, JOIN, AND and NULL, are changed by default. Built-in functions such as COUNT, SUM and COALESCE change when a bracket follows them, and types such as INT and VARCHAR change after ::, inside CAST and in a CREATE TABLE column list.

Words that are keywords in some databases and ordinary names in others — name, year, type, value, key, date, first, index — are left alone, because capitalising a column called year would misreport your schema. Identifiers, quoted names, strings and comments are never touched. Choose As typed to leave every word as written.

Dialect Notes

DialectWhat is read differently
Standard SQLSingle-quoted strings, double-quoted identifiers, -- and block comments. The safe default.
PostgreSQLDollar-quoted strings ($$ … $$), nested block comments, E'…' strings with backslash escapes, :: casts, the JSON operators ->, ->> and @>, $1 parameters. A ? is an operator here, not a placeholder.
MySQL / MariaDBBacktick identifiers, # comments, backslash escapes in strings, ? placeholders, ON DUPLICATE KEY UPDATE.
SQL Server (T-SQL)[bracketed] identifiers, TOP, #temp tables, @variables, WITH (NOLOCK) hints and GO between batches.
SQLite[bracketed] and backtick identifiers and INSERT OR REPLACE.

The layout rules are the same for every dialect. The choice only affects how the text is split into tokens and which extra words count as keywords, so a wrong choice shows up as a misread string or comment, not as a different layout.

What Minify Keeps

Minify removes whitespace and comments. It keeps comments that a database acts on: optimizer hints written as /*+ … */ and MySQL's versioned comments written as /*! … */. It keeps a space wherever removing one would change the meaning, such as between two names, or between two minus signs, where -- would start a comment. Minified SQL is for embedding in a string, a URL or a log line; keep the formatted version for code review. With Keep comments ticked, line comments become block comments so that the query still fits on one line.

What It Does Not Do

  • It does not check or run SQL. It reads tokens, not a grammar, so a query with a typo still gets laid out. Missing brackets and unterminated strings or comments are flagged because they make the layout unreliable.
  • It does not restructure procedures. Stored procedures, triggers and BEGIN … END blocks are laid out clause by clause but not re-indented as code. A dollar-quoted function body is kept whole as one string.
  • It does not rewrite queries. No reordering, no alias changes and no removed parentheses. The tests check that every token that goes in comes out, in the same order.
  • It does not wrap a single long expression that has no commas, brackets or AND and OR to break at.

Frequently Asked Questions

How do I format SQL online?
Paste the query into the box with the action set to Format. The result appears as you type: one clause per line, lists indented beneath, subqueries nested. Copy it, or download it as a .sql file. Pick your database under Options so strings, quotes and comments are read correctly.
Does formatting change what a query does?
No. Only whitespace and the case of recognised keywords change; strings, quoted names, numbers and comments are kept character for character, and the order of tokens never changes. SQL is case-insensitive for keywords and unquoted names in the main dialects, so the meaning is the same. Quoted identifiers are case-sensitive in some databases, which is why they are never altered.
Should SQL keywords be uppercase?
It is a convention, not a rule. Many style guides use uppercase keywords so they stand apart from table and column names; others prefer lowercase and rely on syntax colouring. Choose the style your team uses. The formatter applies it consistently, or leaves your own case alone with As typed.
Why is my column called name, year or type still lowercase?
Those words are keywords in some databases and ordinary column names in most, so capitalising them would misreport your schema. Only words reserved everywhere, known functions followed by a bracket, and types in clear type positions are capitalised. If you want a word in capitals, type it that way and choose As typed.
Does it format stored procedures and T-SQL scripts?
Partly. Ordinary statements inside a procedure are laid out clause by clause, and SQL Server's GO separator is understood, but control flow such as IF, WHILE and BEGIN … END is not re-indented, and a PostgreSQL function body in dollar quotes is kept as one string.
What does minifying SQL keep?
Everything that is not whitespace or a comment, plus optimizer hints (/*+ … */) and MySQL versioned comments (/*! … */), because the database reads those. A space stays wherever two tokens would otherwise run together. The result runs exactly as the original did.
Is my SQL uploaded or executed?
Neither. The formatting is plain JavaScript in your browser, so the query is not sent to CitationEasy or anywhere else, and it is never run against any database.

Related Tools

More in Data, Markup & Code

See all Data, Markup & Code