Free tool · Developer tools
SQL formatter
Paste a query and get it back with one clause per line, one column per line and keywords in the case you choose. Pick your database for the right syntax, then copy the result, download a .sql file or squeeze it onto one line.
- Free
- No sign-up
- Runs in your browser
28 lines2 statements, formatted
- Standard SQL
- 2-space indent
- Keywords upper case
- Size
- 282 B → 331 B
- Lines
- 1 → 28
- Statements
- 2
- Longest line
- 282 → 40 characters
How to use the SQL Formatter
- Paste the query into Your SQL. Several statements separated by semicolons are fine.
- Under Your database, choose the one you run the query on. The default is Standard SQL.
- Set the Indent, the case of Keywords and the Layout. Switch What to do to Minify to one line when you need the opposite.
- Press Copy or Download .sql. If the query cannot be read, the message names the line and column.
How do I format a SQL query online?
Paste the query into the box and the formatted version appears straight away. Each clause such as SELECT, FROM, WHERE, GROUP BY and ORDER BY starts a new line, every column and every grouped value gets its own line, and joins and brackets sit one indent step deeper than the clause they belong to. The words in your query stay the same; the whitespace between them and the case of the keywords change.
The example in the box is one line of 282 characters holding two statements: a report of users with their order counts, and a lookup of users who never signed up. Formatted with the defaults it becomes 28 lines and 331 bytes, and the longest line drops from 282 to 40 characters. The two statements are separated by one blank line, and the result panel counts them for you (2 here).
The indent changes the size more than people expect, because every indent step is real characters. A tab is one byte where four spaces are four. The table shows the same example under six settings, measured by this page.
| Setting | Lines | Size | Longest line |
|---|---|---|---|
| 2 spaces, keywords above their lists (default) | 28 | 331 bytes | 40 characters |
| 4 spaces | 28 | 365 bytes | 42 characters |
| Tab | 28 | 314 bytes | 39 characters |
| Keywords in a left column | 18 | 386 bytes | 38 characters |
| Keywords in a right column | 18 | 386 bytes | 38 characters |
| No spaces around operators | 28 | 323 bytes | 38 characters |
On the example, a tab gives 314 bytes against 365 for four spaces. Choose 2 spaces for a narrow window, 4 when your team style says so, and a tab when each reader should pick the width in their own editor. A left column of keywords packs the example into 18 lines, because short clauses share a line with their first item.
Does it matter which database I choose?
Yes, when the query uses syntax that only one database accepts. SQL is a family of dialects, and each one has its own quoting, placeholders and operators. The formatter reads your query with the rules of the database you pick. If it meets something those rules do not allow, it stops and says where.
Two cases show it. Square brackets around a name, as in select [Order Id] from [dbo].[orders], belong to SQL Server. With Standard SQL selected, the page reports an unexpected [Order Id] at line 1, column 8. With SQL Server selected, the same query formats. The other case is a numbered placeholder such as $1, which PostgreSQL uses for parameters: Standard SQL rejects where id=$1, and PostgreSQL formats it.
If you are not sure, start with Standard SQL. When the message points at a name, a placeholder or an operator that your database accepts, change the picker and look again. Backticks around names, as MySQL uses them, are read by both the Standard and the MySQL choice.
The picker also guards the minifier. Some databases treat a backslash inside a quoted string as an escape and others do not, so the minifier needs to know which rule applies before it decides where a string ends.
How do I change keyword case and layout?
Keywords can be upper case (the default), lower case or left as you typed them. Upper case is the long-standing habit in shared SQL because it separates keywords from column and table names at a glance. Lower case suits teams that find capitals noisy. As typed leaves each keyword alone, which helps when a query mixes styles on purpose and you only want the line breaks.
Layout decides where the clause keywords go. The default puts each keyword alone on a line with its items indented underneath. The two column layouts put the keyword and its first item on the same line and line the keywords up, either at the left edge or against a right-hand margin. They use fewer lines but more characters per line, so they suit wide screens.
Brackets have their own rule. A list inside brackets stays on one line while it fits within the width you set, and breaks into one item per line when it does not. For a three-string IN list the default width of 50 characters gives 10 lines, and a width of 120 keeps it at 6. A longer limit gives flatter queries and a shorter limit gives taller ones.
Two more options tune the look. Tick AND / OR at the end of the line to move the logical operator from the start of a condition to the end of the previous one, and No spaces around operators for a=1 instead of a = 1. Blank lines between statements can be none, one or two.
Can I minify SQL?
Yes. Choose Minify to one line and the query comes back on one line with the extra whitespace removed. Fed the formatted example back in (331 bytes), the minified result is 290 bytes. Spaces after commas and brackets and around semicolons go, and the spaces that separate words stay, so the query reads the same to the database.
Comments are dropped, because a line comment would swallow everything after it once the line breaks are gone. The query -- active users / select id , / name / from users /* main table */ / where active = 1 ; becomes select id,name from users where active = 1;. If a query relies on a hint written inside a comment, keep the formatted version.
Anything in quotes is copied exactly. In select 'a b' as label , "two spaces" from t the two spaces inside the string and inside the quoted name survive: the result is select 'a b' as label,"two spaces" from t. Dollar-quoted blocks, which PostgreSQL uses for function bodies, are left alone in the same way.
Minified SQL is for places where length matters, such as a config value, a log line or a one-line command. For anything a person will read or review, use the formatted version.
Is my query sent to a server?
No. The formatter is part of the page. When you open it, your browser downloads the formatting code once, and after that every query is processed on your device. Nothing you paste is sent anywhere, which matters because queries often contain table names, column names and sometimes literal values that you would not want to post.
The tool changes layout only. It does not run the query, connect to a database or check that a table exists, so a correctly formatted query can still fail when you run it. It reads the structure of the text: a missing bracket or an unclosed quote is reported with its line and column, as in the message for select from where ((, which stops at the end of the text.
Very large pastes are accepted up to one million characters. The result box shows the first 200,000 of a longer result, while Copy and Download carry all of it.
Use it with
- JSON Formatter: format and validate JSON.
- XML Formatter: pretty-print, minify and check XML.
- JavaScript Beautifier: format or minify JS.
Questions
How do I format a SQL query online?
Paste it into the box. Each clause moves onto its own line and columns are listed one per line. The example query goes from one line of 282 characters to 28 lines.
Can it format PostgreSQL, MySQL and SQL Server queries?
Yes. Choose the database under Your database. The list has 20 choices, including PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, BigQuery, Snowflake and Oracle.
How do I make SQL keywords uppercase?
Keywords are upper case by default. Use the Keywords box to switch to lower case or to keep them as you typed them.
Can I minify SQL?
Yes. Choose Minify to one line. Comments are removed and whitespace is collapsed, and quoted text is kept exactly. The formatted example drops from 331 to 290 bytes.
Why does it say my query cannot be read?
Either a bracket, quote or comma is missing, or the query uses syntax from a different database. The message gives the line and column. Check that spot, then try your own database in the picker.
Is my query sent to a server?
No. The formatter runs in your browser and the query stays on your device.
The tool's results are computed in your browser by the code on this page; the explanations are checked against the tool's own output.