SQL Formatter
Format a query in the dialect you actually run, choosing keyword casing and indentation.
What this tool does
SQL Formatter puts your query through a real parser for the dialect you pick, then prints it back with one clause to a line and every keyword cased the way you asked. The layout is rebuilt rather than patched, so a statement pulled out of a log, unwrapped from a string in application code, or emitted by an ORM as one enormous line all come out in the same shape as everything else you format here.
Keyword casing is the setting most people arrive for, and it is worth knowing what it touches. UPPERCASE, lowercase or Leave as typed applies to the words the chosen dialect recognises as keywords; table names, column aliases and the insides of string literals keep the case you typed. So, less obviously, do function names and data types: sum, count, int and varchar are never touched by this setting, which is why the sample query comes back with SELECT, FROM and GROUP BY shouted around a lowercase sum(). The edge case in the other direction is a column named after a keyword, which gets recased along with the rest — select year, name, value comes back with YEAR and VALUE shouted while name is left alone — so quote those identifiers, or set keywords to Leave as typed, if that bothers you.
What it will not do is tell you whether the query is correct. This is a formatter, not a linter and not a database connection: there is no schema, nothing is executed, and select * form t is laid out without a word of complaint because form parses perfectly well as an alias. It stops only when the text will not parse at all, and what you get then is the parser library's own complaint, passed through unedited. An unclosed quote, or a placeholder the chosen dialect has never heard of, comes back as one readable line naming the position it gave up at. An unclosed bracket is the ugly case: the library answers with tens of thousands of characters of its internal grammar rules, and the line and column you actually wanted sit at the very front of that wall of text. All of this happens inside this browser tab, so the query, its table names and any values sitting in the WHERE clause are never uploaded; only the formatting library itself is fetched from this site the first time you run the tool.
How to use it
- Paste the query. Drop it into the input box, or press Load sample for a joined and grouped query that shows the shape of the output. Several statements separated by semicolons can go in together.
- Choose the dialect. Sixteen are offered, from Standard SQL through PostgreSQL, MySQL, MariaDB, SQLite, SQL Server, Oracle, BigQuery, Snowflake, Redshift, Spark, Hive, Db2, Trino, N1QL and TiDB. The choice decides which words count as keywords and which placeholder and quoting styles will parse.
- Set the casing and the indentation. Keywords uppercase, lowercase or left exactly as typed; two spaces, four spaces or a tab for each level of nesting.
- Space the statements out, then copy. Blank lines between statements can be set anywhere from none to five. The summary under the output counts the statements it formatted and names the dialect it used.
Processing & privacy
Runs on your device. Your file is processed directly in this browser.
Common use cases
Reading a query out of a slow-query log
Logged SQL arrives as a single flattened line. Indenting it is the quickest way to see which join or correlated subquery is doing the damage.
Reviewing SQL that a query builder wrote
An ORM emits output that is correct and close to unreadable. One pass gives it the same shape as hand-written SQL, which is what makes a code review of it possible.
Putting a repository's queries into one style
Migrations and reports written by different people drift into different casing and indentation. Formatting each with the same settings means diffs show real changes rather than whitespace.
Pasting a query into a ticket or a document
Two-space indentation and uppercase keywords survive a paste into an issue tracker far better than a wall of unbroken text does.
Questions
Is my SQL sent anywhere?
Why does it reject a query that runs fine on my database?
Will it tell me my query is wrong?
Does it change my query?
Why is one of my keywords still lowercase?
How large a query can I paste?
Can it turn the statements into rows I can open in a spreadsheet?
Related tools
- JSON Formatter — Pretty-print, collapse and inspect JSON instantly.
- Base64 Encoder Decoder — Encode and decode Base64 text and files.
- JWT Decoder — Inspect the header, payload and claims of a JWT.
- Hash Generator — Compute MD5, SHA-1, SHA-256 and SHA-512 digests.
- Regex Tester — Test patterns against sample text with live matches.
- Unix Timestamp Converter — Convert between epoch seconds and readable dates.