How to format SQL
- Paste your query or script into Your SQL, or press Open a file to load a .sql file. You can also drag a file onto the box, or press Example to see a messy query tidied up.
- Choose your database under Dialect. Standard SQL works for most simple queries; the dialect matters for quotes, comments, parameters and keywords that only one database has.
- The result appears on the right while you type. Pick the case for keywords, functions, data types and names, the indentation and the layout until it looks the way your team writes SQL.
- Press Copy result or Download. Use result for my text moves the result into the left box so you can keep editing it.
Checked so the meaning never changes
A formatter should only move spaces and line breaks and change the case of keywords. In rare cases a formatter can get more than that wrong, such as splitting the shift operator << into < <, or an arrow -> into - >. Each of these changes what the query does, and neither is easy to spot by eye.
So every result here is checked. Your SQL and the result are both read with the rules of your dialect: strings, quoted names and comments must be exactly the same, and every other character must come back in the same order. Where the formatter split an operator or name, it is put back together; where two parts were joined, a space is kept. The notes tell you when that happened. When the result passes, the notes say: Checked: only spaces, line breaks and letter case changed.
Twenty SQL dialects
SQL is a standard, but every database adds its own pieces. The formatter knows the rules of each of these, so it does not mistake one database's syntax for an error:
- PostgreSQL and DuckDB:
::casts,$$function bodies,E'...'strings, JSON operators such as->>, and$1parameters. - MySQL, MariaDB and TiDB: backtick names,
#comments, backslash escapes in strings, and?parameters. - SQL Server (T-SQL): square bracket names,
@variables,TOP,GObatches andN'...'strings. - Oracle PL/SQL: blocks with
BEGIN ... END,q'[...]'strings and:namebind variables. - BigQuery, Snowflake, Spark SQL, Databricks, Hive, Trino, Presto, Athena and ClickHouse: the analytics databases, each with its own functions and syntax, such as triple-quoted strings, raw strings and lambdas in BigQuery.
- SQLite, Amazon Redshift, SingleStore, IBM Db2, Db2 for i and Couchbase N1QL, and Standard SQL for anything else.
The options
- Keywords, Functions, Data types, Table and column names: write each in upper case, lower case, or keep it the way you wrote it. Upper case keywords with names left unchanged is a common style.
- Layout: Standard puts each clause keyword on its own line with the rest indented under it. The two column layouts line up the keywords (SELECT, FROM, WHERE) in a column on the left or right, a style some teams use for long reports.
- AND and OR at the start of a line make conditions easy to scan and to comment out one at a time; at the end of a line suits teams who read conditions like a sentence.
- Keep brackets on one line up to sets how long a bracketed list, such as
IN (1, 2, 3)or a function call, may be before it is split over several lines. - No spaces around operators writes
a=b+1instead ofa = b + 1, for compact code.
The most common SQL mistakes
- A missing or extra comma, especially right before FROM:
SELECT a, b, FROM t. The notes warn about a comma right before FROM, WHERE or a closing bracket. - A quote that is never closed. In SQL a quote inside a string is written twice:
'don''t'. MySQL also accepts a backslash:'don\'t'. The message says when a quote is never closed. - Double quotes for text. In PostgreSQL, Oracle and Standard SQL,
"name"is a column name, not text; text needs single quotes. - Comparing with NULL using =. In standard SQL,
WHERE note = NULLfinds no rows, because any comparison with NULL gives unknown rather than true. WriteWHERE note IS NULLorIS NOT NULLinstead. - A keyword for a name, such as a column called
orderoruser. Put it in the quotes of your database:"order",`order`or[order]. - Brackets that do not match in nested subqueries and function calls. Formatting makes this easy to see, because each level is indented further.
- SQL from another database, such as backticks in PostgreSQL. When your text does not read in the chosen dialect, the notes list the dialects that do read it.
Tips
- Review changes in a pull request more easily by formatting both versions with the same settings and comparing them in the text compare tool, so only real changes show.
- Working with the data itself? The CSV to JSON converter turns a spreadsheet export into JSON, the JSON formatter checks and tidies JSON columns, and the JSON to YAML converter turns settings files from one format into the other.
- Minify before you paste a query into code, a log search or a URL, and format it again to read it.
Your queries stay on your device
SQL often shows table names, customer data and sometimes passwords, so everything happens in your browser: the reading, checking and formatting all run on your own computer or phone, and nothing is sent to this website or anyone else. The page remembers your latest text and settings in your browser on this device so they survive a reload. Clear removes the text, and clearing the site’s data in your browser removes everything.
Frequently asked questions
Is my SQL uploaded to a server?
No. The formatter runs in your browser, so the queries you paste or open are never sent anywhere. That makes it safe for SQL with table names, customer data or passwords in it. The page keeps your last text on this device so it is still there after a reload; press Clear to remove it.
Which SQL dialects does it format?
Twenty: Standard SQL, PostgreSQL, MySQL, MariaDB, SQL Server (T-SQL), Oracle PL/SQL, SQLite, Google BigQuery, Snowflake, Amazon Redshift, Spark SQL and Databricks, Apache Hive, Trino, Presto and Athena, DuckDB, ClickHouse, IBM Db2, Db2 for i, TiDB, SingleStore and Couchbase N1QL. Each one knows its own quotes, comments, parameters and keywords, such as backticks in MySQL, square brackets in SQL Server and $$ blocks in PostgreSQL.
Can formatting change what my query does?
It should not, and this tool checks it every time. After formatting, it reads your SQL and the result with the rules of your dialect and compares them: strings, quoted names and comments must be unchanged, and every other character must come back in the same order. Where a formatter would split an operator such as << into < <, the tool keeps it together and tells you. If anything else differs, the notes name the line to look at.
Why does it say my SQL is not valid?
Usually because of a missing comma, bracket or closing quote, or because the SQL is written for another database. The message gives the line and column, and the box on the right marks the spot. When another dialect can read your text, the notes name it: MySQL reads SQL with backticks, for example, but PostgreSQL does not.
Should SQL keywords be upper case?
Databases do not care: SELECT, select and Select all work. Many style guides write keywords in upper case so they stand out from table and column names, and that is the default here. Choose Unchanged or lower case under Keywords if your team does it differently. Names stay the way you wrote them unless you choose otherwise, because some databases, such as MySQL on Linux, keep two table names apart when they differ only in case.
How do I minify SQL to one line?
Choose One line (minified) under Result. Line breaks and extra spaces are removed, with one space kept only where two words or operators would otherwise run together. Comments are left out, or kept in /* */ form when you tick Keep comments, so a -- comment cannot swallow the rest of the line.
Does it format scripts with several statements?
Yes. Paste a whole script: each statement is formatted, a blank line is placed between statements (you can choose none or two), and the counts under the result show how many statements and lines there are. SQL Server GO lines stay on a line of their own, and the body of a PostgreSQL function between $$ marks is kept the way you wrote it.
How big a file can it handle?
A file you open can be up to 5 MB. Everyday queries format while you type; a script of many thousands of lines can take a few seconds, and the page says when it is working.