Cookies This site keeps only what it needs to work. There is no analytics and no advertising on it today, and nothing here follows you to other sites. Read the privacy policy

Choose what to allow
+92 312 8299940Free Quote
EN – Language: English
Free tool, nothing to install

SQL Formatter
One Tool To Format, Minify And Escape SQL

This SQL formatter turns one long line of SQL into clean, readable lines. It also minifies SQL and escapes text for strings. We upload nothing.

  • FreePrice
  • 4Modes
  • NoneSign-up
  • NoneUploads

SQL Formatter

Format, minify, escape and unescape SQL in your browser

Lines
0
Statements
0
Size
0 B

Ready. Paste SQL and pick what to do.

Your browser does all of the work here, in this tab. We upload nothing, store nothing and log nothing.

Run it on a test page before the live one

Keep a copy of your original code and try the result on a test page before you replace the live file. No automatic tool can see every way your site uses its code.

Add a link to this tool on your own site

Copy the code below and drop it into your page. It places a link to this free tool.

The short version

What An SQL Formatter Does For You

A query you copy from a log or a tool often sits on one long line. You cannot read it, and you cannot review it. So you need to break it into lines, add indents and put each clause in its place.

This tool does that in one box. First, paste your SQL. Then pick Format, and the result appears as you type. It puts SELECT, FROM and WHERE on their own lines, and it lines up joins and conditions.

SQL is minified as well. This can be very helpful if you need to paste your SQL query into your application’s source code or configuration files. Also, the tool allows you to escape and unescape text, preventing any single quotes within a column value from breaking your SQL statement.

Your SQL will remain in your browser on your computer. This is important since some of the info you may be querying includes table names, IDs and possibly even actual data.

Format SQL Into Readable Lines

First, the software will read your written material and divide it up into individual "tokens." Each token can be either a single word, number, string, comment, or other symbolic element. Since the software processes strings and comments before anything else, keywords that appear within quotation marks are always left untouched.

Drawing of one long line of SQL on the left and an arrow to the same query on the right in clean indented lines
One long line becomes readable SQL.

Then it lays the tokens out. Every main clause of the query begins on a fresh line. The select list gets one column per line when it has more than one. Joins start a new line, and the AND and OR in a condition line up under the WHERE.

Subqueries are enclosed within parentheses and indented one space to the right, which allows us to quickly find where each of the clauses begin and end.

For more detail, see: Wikipedia article on SQL.

Indents, Keywords And Comments

You choose the indent. Two spaces, four spaces and tabs are all there. You also choose the keyword case. Capital letters are the traditional style. Modern code often uses lowercase. A third choice leaves your keywords as you typed them.

SQL formatter showing a join query formatted with uppercase keywords and a two space indent
A join query, formatted.

Also, comments will remain in place. A single-line comment will remain at the end of the line on which it was created, while a block comment will remain exactly where you put it. If you wish to have a cleaner output, then simply disable comments.

The tool doesn’t change names, values, or strings. All it does is move white space around and alter the case of keywords.

Minify SQL

Then there is minify, which does the reverse. It strips comments and unneeded white space and keeps one space only where two words would otherwise touch.

This can be useful if you’re saving your query in a string. It can also be useful for saving queries in URLs or logs. The end product is the exact same query as before, but with less characters. The notes below the results indicate the number of characters that were saved.

Escape And Unescape SQL Text

In addition to being a bug (a programming error), a quote within a value will end a string too early. This could potentially be a security risk as well. So, when writing code, you should use an escape character to avoid this issue when putting the text into a statement.

SQL formatter in escape mode turning a name with a single quote into a safe SQL string
Escape a value so it can sit safely inside SQL.

SQL uses double single-quotes when you want to escape a quote (e.g., ’a’’b’). MySQL allows you to use a backslash before the quote (e.g., ’a\’b’). This tool supports both ways of escaping a quote, as well as adding the outer single-quotes around your string.

Then unescape does the opposite. It removes the outside quotes and changes every two quotes or backslashes into simple text.

Escaping Alone Will Not Protect You

Escaping helps for one off text, such as a value in a script. For an app, use prepared statements with bound parameters instead. They keep data and code apart, so no quote can change the query.

The formatter does not change colon names or question marks in the prepared statements. So, when you use the formatter to change the appearance of your prepared statements, they will still be functional.

Which SQL Dialects It Reads

First of all, this tool is based on the most basic parts of SQL. This includes SELECT, INSERT, UPDATE, DELETE, WITH, CREATE TABLE, joins, unions, window functions and CASE.

The tool can read strings with single quotes, names with double quotes, backticks, square brackets and comments from either format. It lays out queries for MySQL, PostgreSQL, SQL Server, SQLite and Oracle.

Some of the engine specific syntax that looks simple is actually correct and valid. Since the tool doesn’t change what you write (it just formats it), there isn’t any way for the tool to break what you wrote.

Stored procedures can also use this feature, as well as lengthy scripts. This is because the tool considers every semicolon to be the termination point for a statement, which means that a script that contains multiple statements will remain in sequence.

Safe By Design

This utility doesn’t run your sql or connect to a database. It simply reads text from your tab, and writes text to your tab.

Video guide

See the SQL Formatter at Work, Start to Finish

This brief video shows each step from start to finish. Choose your language below, and switch on captions if they help.

How it works

Turning A Messy Query Into Readable SQL

Four steps take you from a messy query to a clean one.

  • Paste your SQL

    Paste it, open a file, or load the sample.

  • Pick what to do

    Choose Format, Minify, Escape or Unescape from the buttons.

  • Set the options

    Choose the indent, the keyword case and whether to keep comments.

  • Copy or save

    Copy the tidied query, or keep it as a .sql file.

Why this one

Where This SQL Formatter Goes Further

It is quick, it is private, and it never changes what your query means.

  • Four tools in one

    Format, minify, escape and unescape SQL on a single page.

  • Real tokens

    Strings and comments are read first, so they are never altered.

  • Many dialects

    It works with queries from all of the following: MySQL, PostgreSQL, SQL Server, SQLite, and Oracle.

  • Your style

    Pick the indent and the keyword case.

  • Clear warnings

    Open quotes and unmatched brackets show a message.

  • Nothing leaves your tab

    The processing occurs in the user’s browser, so we cannot view the SQL code of the users.

Good habits

Habits For Writing Readable SQL

A few habits keep queries easy to review.

  • One clause per line. SELECT, FROM, WHERE and ORDER BY each start a new line.
  • One column per line. A long select list reads best when each column has its own line.
  • Name your aliases. Short but clear aliases such as o for orders keep joins readable.
  • Write joins in full. Use JOIN with ON, not a comma list with conditions in WHERE.
  • Pick one keyword case. Uppercase or lowercase works. Mixing both does not.
  • Comment the odd parts. A short note on a tricky filter helps the next reader.
  • Use parameters. Send values as parameters in apps, not as joined text.
  • Escape values in scripts. For one off scripts, double every single quote in a value.
  • Format before you review. A clean layout makes errors easy to spot in a code review.
  • Minify only for storage. Keep the formatted copy in your repo and minify just for transport.
  • Alias with AS. Write AS before an alias, so a missing comma cannot hide.
  • Test on a copy. Run a new query on test data before it touches real tables.
Worth avoiding

Common SQL Mistakes This Tool Helps You Spot

A clean layout makes these easy to see.

  • Unclosed quotes

    A string that never ends shows a clear message.

  • Unmatched brackets

    The tool warns when opening and closing brackets differ.

  • Lost AND or OR

    Each condition on its own line makes a missing one obvious.

  • Quotes in values

    Escape mode doubles the quote so a value cannot end the string.

  • Hidden comments

    Comments stay visible, or you can strip them with one switch.

Questions

SQL Formatter: Your Questions

Does this SQL formatter cost anything?

Yes. It’s free, you need no account, and there are no limits. Since your computer’s browser is doing most of the work for us, there are no costs associated with our servers that would need to be passed on to you.

Is the SQL I paste sent to a server?

The SQL is read by your browser and then written. There are no results that go to this site, and there is no data that we store.

Will it execute my query?

No. The tool doesn’t connect to any database and doesn’t run any query. It simply alters the way the text is formatted.

Which database dialects can the SQL formatter lay out correctly?

The common core of SQL is read by this tool. So, it will work with MySQL, PostgreSQL, SQL Server, SQLite and Oracle code.

Will it format more than one statement?

Yes, just separate them with semicolons. Each statement should be written on its own line and have a single blank space above and below it.

Does it change my keywords at all?

You may select the option to convert the case of your input (uppercase, lowercase) or leave it as it was originally typed.

Does it change names or values?

No, all table names, column names, string values and numeric values will remain in the exact format you given.

What happens to comments?

They stay by default. Switch off Keep comments to remove them from the result.

What exactly does the SQL minifier strip out?

The process removes comments and unneeded white spaces. It only allows spaces when there is an immediate contact between two words.

What is SQL escaping?

It changes a quote inside text so the text cannot end a string early. In standard SQL, a quote is escaped by doubling it.

What is the backslash style?

MySQL can also accept backslashes in front of quotes and newlines. If you are going to use MySQL, select the option which best fits your needs.

Should I escape values in my app?

No. You should use prepared statements to bind your variables, rather than concatenating strings. Prepared statements keep the data apart from the code.

What does unescape do?

It takes off the outer quotes and turns two double quotes, or a sequence of backslashes, into plain text.

Does it keep parameters?

Yes. Names such as :id, $1 and a question mark stay untouched.

Can it format a long file?

Yes, files of a few megabytes are fine. On a phone, very big files can take a little longer.

Why did the formatter show me an error?

A quoted string or a block comment never ends. Add the missing quote or the closing comment mark.

Is it possible to open a SQL file?

Yes. Use Open a file. You can also save the result as a SQL file.

Does the tool change the meaning of my query?

No. This process will only adjust whitespace and change the capitalization of keywords. The meaning of the query is not affected by these changes.

Are formatting and beautifying the same thing?

Yes, the terms "formatting" and "beautifying" refer to the same process. This tool uses line breaks and indentation to make the query more readable.

Does it reorder my columns or tables?

No. The order stays exactly as you wrote it. Only the spacing is altered.

Why does a long function call stay on one line?

Short calls like count, etc., read better on one line; whereas subqueries, joins and clauses should each be on separate lines.

Does it run on phones too, or only on desktops?

Yes. The panels stack and the boxes scroll. For long queries, a keyboard is still easier.

More free tools

Handy Companions For Developers

Formatting is one step. These three tools help with the data around it.

Ready To Format Some SQL?

Enter what you want to format, choose a format option, and then copy the formatted text. Your formatting tool will be for free, need no registration, and store all of your data locally.

Prefer Denwix on GoogleReview Denwix on Google