Manage accounts via cloud phones and web profiles. Automate posts and engage with your audience in one dashboard with Multilogin. See Details

SQL Formatter Online: Format and Beautify SQL Queries

5 of 1 ratings

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.

Examples:

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 .sql file into the box, and a message that tells you when the syntax is not understood.

How to format an SQL query

  1. Paste the query into the left box, or press one of the examples.
  2. Choose the dialect of your database in the options. This matters: [name] in SQL Server, backticks in MySQL, $$ bodies in PostgreSQL and FORMAT in ClickHouse are different things.
  3. 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 as yyyy-mm-dd. Other common styles: 120 is yyyy-mm-dd hh:mi:ss (24-hour), 112 is yyyymmdd, 104 is dd.mm.yyyy, 101 is mm/dd/yyyy. FORMAT(GETDATE(), 'yyyy-MM-dd') takes a .NET format string and a culture, and FORMAT(1234567.89, 'N2') gives a number with thousands separators and two decimals (1,234,567.89 in the en-US culture). Microsoft recommends CAST or CONVERT for general data type conversions and FORMAT for locale-aware formatting; FORMAT needs 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 (9 is a digit that can be dropped, 0 a digit that is always shown, the comma is the group separator, FM removes the padding). A string with values is made by the function format('Hello %s', 'World').
  • SQLite: strftime('%Y-%m-%d', 'now').
  • ClickHouse: formatDateTime(now(), '%Y-%m-%d') for a date. The command FORMAT at the end of a query sets the format of the whole answer: SELECT * FROM t FORMAT JSON, FORMAT CSV or FORMAT 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

JSON Beautifier & Validator

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.

19
HTML & CSS Minifier Online: Compress HTML and CSS Code

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.

10
Free Case Converter: Change Text Case Online

Quickly transform your text formatting. Instantly switch between lower, upper, camel, Kebab, dash and snake case with the DIEG Case Converter. Free online tool.

11
Sort and Shuffle Lines Online: Alphabetize, Reverse and Randomize a List

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.

14

Popular tools