SQL Formatter Online: Format and Beautify SQL Queries
Paste an SQL query, choose the dialect, and the formatted query appears as you type. Everything happens in your browser: the query is not sent anywhere and not saved. The page remembers only your choices.
Options
SQL Formatter Online: Format and Beautify SQL Queries
This SQL formatter makes any query readable in one click: it adds indents and line breaks, puts every clause and every column on its own line and aligns the nested queries. Paste a query, choose the dialect and the result appears at once. The formatter and beautifier works in your browser, so the query is not sent to a server, and it knows the syntax of MySQL, PostgreSQL, SQL Server (T-SQL), Oracle, SQLite, BigQuery, ClickHouse and other databases.
- 16 dialects, from Standard SQL to MySQL, MariaDB, PostgreSQL, SQL Server, Oracle PL/SQL, SQLite, BigQuery, Snowflake, Redshift, ClickHouse, Db2, DuckDB, Trino, Spark SQL and Hive.
- The look of the query is yours: indent of 2 or 4 spaces or a tab, keywords in upper case, lower case or as you wrote them, the layout with aligned columns, AND and OR at the start or at the end of a line, the width of short expressions.
- Make one line to put a query into a log, a config, a code string or a chat message: the spaces are collapsed and the comments are removed, while strings, quoted names and the executable comments of MySQL stay as they are.
- Copy in one click, drop a
.sqlfile into the box, and a message that tells you when the syntax is not understood.
How to format an SQL query
- Paste the query into the left box, or press one of the examples.
- Choose the dialect of your database in the options. This matters:
[name]in SQL Server, backticks in MySQL,$$bodies in PostgreSQL andFORMATin ClickHouse are different things. - Take the result from the right box with the Copy button. The page remembers your options for the next time.
Why format SQL
- Reading and debugging. A query copied from a log, from the debug output of an ORM or from a ticket often comes as one long line. After the formatting you see the joins, the conditions and the nesting.
- Code review and diff. One column or one condition on a line gives a clean diff: a change of one condition is a change of one line.
- A shared style. The same upper case keywords and the same indents in the whole team, without arguments about the settings of the editor.
- Learning. A well formatted query shows the order of the clauses: SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY.
SQL Server (T-SQL) and the Poor Man's T-SQL Formatter
Choose "SQL Server (T-SQL)" for a query with square brackets, TOP, CROSS APPLY, FORMAT() and the other forms of Microsoft SQL Server. Poor Man's T-SQL Formatter is a free open-source formatter with a plug-in for SSMS and Visual Studio, a command-line version and an online service; this page does a similar job for a single query, in your browser, with the SQL Server dialect.
How to format a date, a time, a number and a string in SQL
In SQL the word "format" also means the formatting of a value, not of a query. The functions are different in every database:
- SQL Server (MS SQL):
CONVERT(varchar(10), GETDATE(), 23)gives a date asyyyy-mm-dd. Other common styles: 120 isyyyy-mm-dd hh:mi:ss(24-hour), 112 isyyyymmdd, 104 isdd.mm.yyyy, 101 ismm/dd/yyyy.FORMAT(GETDATE(), 'yyyy-MM-dd')takes a .NET format string and a culture, andFORMAT(1234567.89, 'N2')gives a number with thousands separators and two decimals (1,234,567.89in the en-US culture). Microsoft recommends CAST or CONVERT for general data type conversions andFORMATfor locale-aware formatting;FORMATneeds the .NET CLR. - MySQL and MariaDB:
DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s')for a date and a time,TIME_FORMAT(NOW(), '%H:%i')for a time,FORMAT(1234567.891, 2)for a number. - PostgreSQL:
TO_CHAR(now(), 'YYYY-MM-DD HH24:MI:SS')for a date and a time,TO_CHAR(1234567.891, 'FM9,999,999.00')for a number (9is a digit that can be dropped,0a digit that is always shown, the comma is the group separator,FMremoves the padding). A string with values is made by the functionformat('Hello %s', 'World'). - SQLite:
strftime('%Y-%m-%d', 'now'). - ClickHouse:
formatDateTime(now(), '%Y-%m-%d')for a date. The commandFORMATat the end of a query sets the format of the whole answer:SELECT * FROM t FORMAT JSON,FORMAT CSVorFORMAT Pretty.
Good to know
- The formatting changes only the layout of a query (spaces and line breaks, and the case of the keywords if you choose it). The strings and the names are not touched.
- The tool does not run the query and does not check it against your database. If it says that the syntax is not understood, the query may have a mistake (a bracket or a quote is not closed) or use a rare feature of another dialect: choose the right dialect and try again.
- "Make one line" removes the comments, because a comment with
--would swallow the rest of a one-line query. - The limit is 500 000 characters. The query is formatted in your browser and is not saved.
Related tools
For other formats use the JSON beautifier and validator and the HTML and CSS minifier. To change the case of names, for example from userName to user_name, use the case converter, and to sort a list of values or remove the duplicates the sort and shuffle lines tool.
Similar tools
Beautify, minify and validate JSON online. Open a file, see the exact line and column of a syntax error, download the result. Works in your browser, nothing is uploaded.
HTML and CSS minifier online: remove comments, spaces and line breaks, and see how many bytes you save. Safe for strings, URLs and pre blocks. In your browser.
Quickly transform your text formatting. Instantly switch between lower, upper, camel, Kebab, dash and snake case with the DIEG Case Converter. Free online tool.
Sort lines A-Z, Z-A or by number, reverse text, shuffle a list, pick random lines, remove duplicates, and make lists from file names. In your browser.
Popular tools
Check who hosts a website: the IP address, the hosting company (ASN), the country and the city of the server. Free hosting checker for any domain.
Free IP lookup: find the country, city, coordinates and timezone of an IP address and its host name with a reverse IP (reverse DNS) lookup. IPv4 and IPv6.
Free DNS lookup online: check A, AAAA, CNAME, MX, NS, TXT, SOA and CAA records of any domain, plus its SSL certificate. Find out why a DNS lookup fails.
Check from the United States (New York) whether a website is online: HTTP status code and response time in milliseconds. A free US ping test for any URL.