Skip to content

SQL Formatter

Put major SQL words on their own lines and write them in upper case. select a from b where c = 1 becomes a three-line statement. The page does not run the statement.

Result

Paste SQL

How to Use

  1. 1 Paste one statement of up to 20000 characters.
  2. 2 Choose Format SQL.
  3. 3 select a from b where c = 1 is the sample.

Example Output

  • select a from b where c = 1

About This Tool

Words moved onto a new line are select, from, where, and, or, group by, order by, insert, into, values, update, set, join, left join, right join, inner join, on, and limit. Text inside quotes stays as typed.

Using the SQL Formatter

This page lays one SQL statement out by moving major words onto their own lines and writing those words in upper case. The sample select a from b where c = 1 becomes three lines. The first line is SELECT a. The second line is FROM b. The third line is WHERE c = 1. The page does not run the statement. It does not open a connection, and it does not tell you whether a table named b exists. Text inside quotes stays as typed, including the letters and the spaces you put between the quote marks.

The headline is Formatted SQL. The text under the headline is the formatted statement, and that text is what you copy. A line under the result counts the characters in the formatted statement. The field accepts from 1 to 20000 characters. A blank field, or a paste longer than 20000 characters, is rejected. The message says to enter from 1 to 20000 characters. The sample is short, so that limit is not what the sample is demonstrating.

The words that start a new line are select, from, where, and, or, group by, order by, insert, into, values, update, set, join, left join, right join, inner join, on, and limit. Each of those words is written in upper case. The pairs group by, order by, left join, right join, and inner join stay together on one line. The words and and or each take a line of their own. A comma, a period, and parentheses stay on the line with the tokens beside them. Identifiers such as a, b, and c stay in the case you typed.

Copy the text under the headline when you want the statement itself. The character count is a separate line, so the words Formatted SQL are not the statement. Reset returns the field to select a from b where c = 1. The formatted lines stay on screen until you format again. Read the first word of each line before you keep the result. For the sample those words are SELECT, FROM, and WHERE, and the page still has not run the statement.

What the Three Lines Contain

The sample has three lines because it has three major words and nothing else that starts a clause. SELECT is the first major word, and the identifier a stays on that same line because a is not one of the words that starts a line. FROM is the next major word, so b moves with it onto the second line. WHERE is the third major word, so c = 1 stays with it on the third line. The equals sign and the digit remain on the WHERE line. There is a space before c, a space around the equals sign, and a space before 1. The page does not run the statement, so those three lines are a layout of the text, not a result set.

Text inside quotes stays as typed. Paste select a from b where c = 'from here'. The word from inside the quotes stays lower case and stays on the WHERE line. The clause word from, the one outside the quotes, still becomes FROM on its own line. A double-quoted string is kept the same way. The formatter reads a quoted stretch as one token and copies that token onto the current line. It does not search inside the quotes for select, from, or where. That is why a sentence stored in a quote can contain those words without being split into extra lines.

How the Sample Is Split

The page trims the ends of the paste, then reads quotes, words, and other characters as tokens. Spaces between tokens are not kept as their own tokens. The sample tokens are select, a, from, b, where, c, =, and 1. select, from, and where are major words, so each one opens a line and is written in upper case. a follows SELECT on the first line. b follows FROM on the second line. c, the equals sign, and 1 follow WHERE on the third line. Run the sample twice. Both runs produce the same three lines, in the same order, with the same spacing. The page does not run the statement on either run.

A JSON value is a different kind of text to lay out. The JSON Prettifier formats one JSON value with line breaks and four-space indentation. {"a":1} becomes a formatted object whose key a is set to the number 1. The headline is Formatted JSON. Invalid JSON is rejected with Enter valid JSON. That page does not fetch JSON, and it does not turn select a from b where c = 1 into three SQL lines. Use it when the paste is JSON. Use this page when the paste is a statement whose major words should each start a line.

What the Layout Does Not Run

The page does not run the statement. SELECT a, FROM b, and WHERE c = 1 are three lines of text. They are not rows from a table, and they are not an error from a database. The page cannot tell you that b exists, that a is a column, or that c can be compared with 1. Those questions stay outside this tool. What the tool tells you is how the major words were lined up and that quoted text was copied as typed. For the sample, the three lines are the whole result. The headline Formatted SQL names that result. The character count is the length of those lines, including the line breaks.

The formatter also does not insert a missing word, a missing comma, or a missing quote. If you paste select a b, the page still formats the tokens it sees. It does not decide that a comma belonged between a and b. If a quote is never closed, the rest of the paste is read as ordinary tokens, because the quote rule needs a closing quote to keep a stretch intact. Fix the statement yourself, then format it again. The second run still does not execute anything.

The Sample and a Quoted Value

Paste select a from b where c = 1. Confirm the result is three lines: SELECT a, then FROM b, then WHERE c = 1. Confirm the headline is Formatted SQL. Confirm a, b, c, and 1 stayed in the case you typed, while select, from, and where became upper case. Paste select a from b where c = 1 and d = 2. The word and takes a line of its own, so AND d = 2 follows the WHERE line. Paste select a from b group by a. The pair group by stays together, so the last line is GROUP BY a. The page does not run any of these statements.

A style rule is a different cleanup. The CSS Minifier removes CSS comments and extra spaces. A rule a { color: red; } becomes a{color:red;}. That page does not check that the CSS is valid, and it does not write SELECT, FROM, and WHERE on three lines. Use it when you want a compact style rule. Use this page when you want the sample statement broken onto SELECT a, FROM b, and WHERE c = 1.

Words the Formatter Knows

The major words are a fixed list. select, from, where, insert, into, values, update, set, join, on, and limit each open a line when they appear outside quotes. group by, order by, left join, right join, and inner join open a line as a pair, so the two words are not split. and and or open a line as well, and they are written AND and OR. A word that is not on that list stays in the case you typed and stays on the current line. The sample identifiers a, b, and c are in that second group. The page does not learn new major words from your paste, and it does not treat every capital letter as a clause.

Quotes are the other hard boundary. A single-quoted stretch and a double-quoted stretch are copied as one token, including spaces and including words that would have been major words outside the quotes. The page does not change the quote marks into the other kind of quote. A paste from 1 to 20000 characters is accepted. Longer text is rejected before formatting. The page does not run the statement that fit, and it does not run the statement that was rejected.

Mistakes That Misread the Three Lines

The usual miss is to treat the three lines as proof the statement ran. The page does not run the statement. SELECT a, FROM b, and WHERE c = 1 are a layout. Another miss is to expect text inside quotes to be uppercased. 'from here' stays 'from here'. Another miss is to expect the page to add a comma or a missing FROM. It lines up the words it recognizes and leaves the other tokens in place. A fourth miss is to paste two statements and expect a separator the page does not know. Each known word still opens a line, and unknown words stay as typed. Read the lines as a layout of one paste.

An HTML fragment is a different kind of space to remove. The HTML Minifier turns a p element with Hi on its own line into <p>Hi</p>. The headline is Minified HTML. Comments are removed. A pre block stays as typed. A script is rejected. That page does not write SELECT a, FROM b, and WHERE c = 1, and it does not run a SQL statement. Use it when you want a shorter HTML fragment. Use this page when you want the sample statement on three lines.

Keeping the Formatted Lines

Copy the three lines and keep the original paste beside them until you have checked the clauses. For the sample, the original is select a from b where c = 1 and the result is SELECT a, FROM b, and WHERE c = 1. Confirm that every identifier you still need is present, that the equals sign and the 1 stayed on the WHERE line, and that nothing inside a quote changed case. Write down the headline Formatted SQL next to the character count if you are comparing two pastes. A second run of the same sample returns the same three lines. The page does not keep a history, so the copy you saved is the record.

When the paste includes and, or, or a two-word clause, read that line on its own before you replace the original. AND and OR are easy to miss if you only glance at the first line. GROUP BY, ORDER BY, LEFT JOIN, RIGHT JOIN, and INNER JOIN should each occupy one line, with both words in upper case. If a quote was split across lines, the quote was not closed in the paste. Close it, format again, and compare the quoted token with the original characters.

Formatting the Sample Again

Enter select a from b where c = 1. Confirm the three lines and the headline Formatted SQL. Add and d = 2 and confirm AND starts the next line. Replace 1 with 'from here' and confirm the quoted words stay lower case on the WHERE line. Clear the field and confirm the rejection that asks for 1 to 20000 characters. Paste a statement that uses join and on, and confirm JOIN and ON each start a line in upper case. The page does not run the statement while you do these checks. The identifiers stay in the case you typed on every successful run.

A script is a different text to compact. The JavaScript Minifier removes comments and extra spaces outside strings. function a() { return 1; } on several lines becomes function a(){ return 1;}. Note the space after the opening brace. That page does not run the script. A comment mark inside a regular expression can be left as typed because that page is not a full parser. It does not produce SELECT a, FROM b, and WHERE c = 1. Use it when you want a shorter script. Use this page when you want the SQL sample on three lines.

What the Three Lines Mean

The three lines mean the major words were found and written in upper case, each at the start of a line, with the following tokens kept beside them until the next major word. In the sample, that produces SELECT a, FROM b, and WHERE c = 1. Quoted text means the characters between the quote marks were copied as typed. The character count means the length of the formatted text. The headline Formatted SQL means the layout step finished. None of those readings means the statement was executed.

Checking a Formatted Statement

Start with the sample. select a from b where c = 1 should become SELECT a, FROM b, and WHERE c = 1, with the headline Formatted SQL. Then put a known word inside quotes and confirm it stays as typed. Then use and, and confirm AND is on its own line. Then use group by, and confirm the pair shares one upper-case line. Then submit an empty field and confirm the length message. The page does not run the statement, so the check is the text of the lines, not a table of rows. If from inside quotes became FROM, the quote was not kept as one token. If the sample is still one line, the major words were not moved.

An XML document is a different indentation. The XML Formatter indents an XML document. <a><b>Hi</b></a> is formatted with each element on its own line. The headline is Formatted XML. That page does not fetch a file, and it does not accept a document type. It does not turn the SQL sample into SELECT a, FROM b, and WHERE c = 1. Use it when you want indented elements. Use this page when you want the statement lined up by its major words.

Frequently Asked Questions

What does select a from b where c = 1 become?

Three lines: SELECT a, then FROM b, then WHERE c = 1. The headline is Formatted SQL. The page does not run the statement.

Does text inside quotes change?

No. Text inside quotes stays as typed. A word such as from inside quotes stays on the current line and keeps its case.

Which words start a new line?

select, from, where, and, or, group by, order by, insert, into, values, update, set, join, left join, right join, inner join, on, and limit. They are written in upper case.

Do and and or share the previous line?

No. and and or each take a line of their own, written AND and OR.

Does the page run the statement?

No. The three lines are a layout of the paste. The page does not open a connection and does not return rows.

How long can the paste be?

From 1 to 20000 characters. A blank field or a longer paste is rejected before formatting.

What is the headline?

Formatted SQL. The formatted statement is the text under that headline, and the character count is the length of that text.

Will a second run of the sample differ?

No. select a from b where c = 1 becomes the same three lines again. Quoted text still stays as typed.