Black Friday: 50% off every paid plan with code BF50. Ends 5 December.

See prices

Free tool

SQL formatter, readable queries.

Paste a SQL query or open a .sql file and it is laid out as you type, coloured, with SELECT, FROM, JOIN and WHERE on their own lines. Pick from 20 dialects, the case of keywords, functions and data types, a standard or tabular layout, and where AND and OR go. A query it cannot read is marked on its line. One line squeezes it without touching quoted text. Your queries stay in your browser.

Need a custom tool, app or website? Contact us 
Input
Result

The formatted query shows here.

Paste on the left, open a .sql file, or press Example.

Case

Layout

Keys

  • Cmd Enter Format
  • Cmd Shift Enter One line
  • Cmd S Download
  • Cmd Shift C Copy
  • / Search

Formatted in your browser with sql-formatter (MIT). Your queries are never uploaded or saved. Only your settings are remembered on this device.

Readable SQL

One clause per line.

01 / Layout

Clauses on their own lines

SELECT lists, JOINs, WHERE conditions and GROUP BY each get their own line and indent.

02 / Dialects

Twenty to pick from

Each knows its own keywords, quotes and functions, so formatting does not break the query.

03 / Private

Nothing leaves the page

Queries can hold table names and data; they are formatted in your browser.

Guide

How to format SQL, so a query reads top to bottom.

A SQL formatter rewrites a query so each clause starts on its own line, the parts inside are indented, and keywords share one case. It changes only spaces, line breaks and the case of keywords, never what the query does. This one uses the open source sql-formatter library in your browser and knows the quirks of thirteen dialects, from MySQL and PostgreSQL to SQL Server, BigQuery and Snowflake.

How to use the SQL formatter

  1. Paste your query into the Input box, drop a .sql file on it, or press Open file. It is formatted a moment after you stop typing. Press Example to try a longer query.
  2. Pick the Dialect your database uses. This matters most when the query has syntax only one database understands.
  3. Choose the case for Keywords, Function names and Data types: CAPITALS, lower case, or As typed.
  4. Set the Indent, the Style, where AND and OR go, and how long a bracketed part may get before it is split.
  5. Press One line if you want the whole query on a single line instead.
  6. Press Download .sql to save it under the name of the file you opened, or Copy. The search box above the result finds any word in it.

What each setting does

Dialect tells the formatter which words are keywords and how names and strings are quoted. MySQL wraps names in backticks, SQL Server uses square brackets, PostgreSQL has :: casts and dollar quoted function bodies. With the wrong dialect, a query that runs fine can fail to format, or a word can be read as a keyword when it is really a column name.

Keywords, Function names and Data types change only the case of those words, such as SELECT, count() and VARCHAR. Table names, column names and text inside quotes stay as you typed them. Capitals for keywords are the most common house style because they make the structure easy to scan. Pick whatever your team already uses.

Style sets the layout. Standard puts each clause keyword on its own line and indents what follows. The two tabular styles put the keyword and its first item on the same line, with the keywords lined up in a column on the left or the right, which some teams find faster to scan.

AND and OR chooses whether a condition line starts with AND or ends with it. Starting the line with it makes it easy to comment out one condition.

Keep short parts on one line up to controls bracketed parts, such as an IN ('US','CA') list, a function call or a short subquery. If the part fits within 30, 50, 80 or 120 characters, it stays on one line. If it is longer, it is broken over several lines and indented.

If you paste several statements separated by semicolons, each one is formatted and they are kept apart with a blank line.

Read a formatted query in the order the database runs it

Once each clause has its own line, it helps to know that the database does not run them in the order they are written. This is the logical order, which explains a lot of confusing errors:

StepClauseWhat happens
1FROM and JOINThe tables are combined into rows.
2WHERERows that do not match are dropped.
3GROUP BYThe remaining rows are put into groups.
4HAVINGGroups that do not match are dropped.
5SELECTThe columns and totals are worked out.
6ORDER BYThe result is sorted.
7LIMIT or TOPOnly the first rows are kept.

That is why you cannot use a column alias from SELECT inside WHERE in most databases: WHERE runs first, before the alias exists. It is also why the sample query repeats sum(i.price*i.qty) in HAVING instead of writing total. Some databases, such as MySQL, do accept the alias in HAVING, so check yours.

When formatting fails

  • Wrong dialect. The error usually names the word or symbol it did not expect. Switch the Dialect to your database and try again.
  • Template placeholders. Code from an app can hold things like {{user_id}} or ${table} that are not SQL. Swap them for a plain value while you format, then put them back.
  • An unclosed quote or bracket. One missing ' makes the rest of the query look like a string. Look near the point the error names.

When the formatter cannot read a query

The result side says where it stopped, with the line and column, and shows the lines around it. The same line is marked in red in the input, and Show in the input puts the cursor there. The point marked is where the formatter gave up, so the real slip, such as a missing comma or bracket, is often just before it. If the query runs fine in your database, pick that database under Dialect. Put it on one line instead skips the layout and still gives you a tidy single line.

One line

One line squeezes every run of spaces and line breaks into a single space and removes comments. Text inside quotes, quoted names and dollar quoted bodies are left exactly as they are, so '--not a comment' and 'two spaces' survive. Optimizer hints like /*+ INDEX(t) */ and MySQL's /*! */ comments are kept. Tick Keep comments, as /* */ to keep every comment, with -- comments turned into /* */ ones so they cannot swallow the rest of the line.

Big files and shortcuts

Files over 150 KB, such as a schema dump, are formatted in the background so the page stays quick, and the result is drawn a screen at a time. Files over 2 MB are kept out of the input box. Cmd+Enter formats, Cmd+Shift+Enter puts it on one line, Cmd+S downloads, Cmd+Shift+C copies and / jumps to search. On Windows, use Ctrl for Cmd. Your settings are remembered on this device; your queries are not.

Tips for SQL that stays readable

  • Give tables short, clear aliases, such as o for orders and c for customers, and use them on every column.
  • Put each join condition in the ON clause and filters in WHERE, so a reader can tell joining from filtering.
  • Format before you paste a query into a code review or a ticket. A diff of formatted SQL shows the real change instead of moved whitespace.

To compare two versions of a query line by line, use the diff checker. If you share queries in slides or posts, the code to image tool turns the formatted result into a clean picture.

Questions

Good to know.

How do I format a SQL query?

Paste it. It is formatted as you type; pick your dialect for the best result.

Will formatting change what my query does?

No. Only spaces, line breaks and keyword case change.

Which databases are supported?

Standard SQL, MySQL, MariaDB, PostgreSQL, SQL Server, SQLite, Oracle PL/SQL, BigQuery, Snowflake, Redshift, DuckDB, ClickHouse, Spark, Hive, Trino and Presto, IBM Db2, Db2 for i, SingleStore, TiDB and Couchbase N1QL.

Why did it fail?

Usually the dialect: some syntax only exists in one database. The error shows the line and column and marks it in the input. Pick your database, or look just before that point for a missing comma, bracket or quote.

Is my SQL uploaded?

No. It is formatted in this tab and never stored. Only your settings are remembered on this device.

Can I line the keywords up in a column?

Yes. Set Style to one of the tabular layouts, with the keywords on the left or the right of the column.

Should SQL keywords be in capitals?

SQL does not care, so it is a style choice. Capitals make the clauses stand out from table and column names, which is why many style guides use them, but plenty of teams write lower case. Pick one and use it everywhere.

Can I format several SQL statements at once?

Yes. Paste them all, separated by semicolons, and each statement is formatted with a blank line between them.

Does it check whether my SQL is correct?

No. It only lays the query out. It can spot some broken syntax, like an unclosed quote, but it does not know your tables or run the query, so a query can format cleanly and still fail in the database.

Why can't I use a column alias in WHERE?

Because WHERE runs before SELECT, so the alias does not exist yet. Repeat the expression in WHERE, or wrap the query in a subquery and filter on the alias outside it.

Listed on

Listed on JustMacAppsMotionShine on Mac App IndexMotionShine on ScrollLaunchMotionShine featured on StartupBaseFeatured on FazierFeatured on Twelve ToolsMotionShine - Featured on Smol Launch