SQL Formatter & Beautifier
Format messy SQL into clean, readable queries — or minify for embedding.
Written by Anil Gautam · Updated
What does Format change in a SQL query?
Format squeezes all whitespace into single spaces, then uppercases a fixed list of keywords: SELECT, FROM, WHERE, the JOIN variants, ON, GROUP BY, ORDER BY, HAVING, LIMIT, OFFSET, INSERT INTO, VALUES, UPDATE, SET, DELETE FROM, CREATE, ALTER and DROP TABLE, WITH, UNION, AS, AND, OR, NOT, IN, EXISTS, BETWEEN, LIKE, IS NULL and the CASE words. Each main clause then starts a new line, and every comma-separated item moves to its own line with a two-space indent. Function names such as count() and words like desc or distinct keep the case you typed.
Paste select name, city from customers where status = 'paid' and total > 500 order by name desc limit 5 and the output puts SELECT on the first line, name and city indented below it, then FROM customers, WHERE status = 'paid' AND total > 500, ORDER BY name desc and LIMIT 5 on lines of their own. Conditions joined by AND or OR stay on the WHERE line. Commas inside function calls and IN lists also break, so IN (1, 2, 3) spreads over three lines.
Can formatting or minifying change what a query does?
Both modes apply pattern rules to the text without parsing SQL, so they cannot tell a quoted string from code. Each one collapses repeated spaces inside strings, turning 'a b' into 'a b'. In Format mode a comma in a string also gets a line break, so WHERE city = 'Delhi, India' comes out with a newline and two spaces after the comma, and the database then looks for different text. Keywords inside strings are uppercased and moved, so '%order from app%' splits into '%order and a new line starting FROM app%'. Quoted names change too: a column called "Order By" becomes "ORDER BY".
Comments are kept in both modes, and a comment that starts with two hyphens runs to the end of its line. Minify puts the whole query on one line, so everything after the first such comment is commented out: with a two-hyphen comment after select id on the first line, the minified query reaches the database as only select id. Format can swallow code the same way when the line after a comment does not start with a clause keyword. Delete those comments or rewrite them in /* */ form first. Outside strings, quoted names and comments, the tool changes only whitespace and keyword case, which SQL ignores.
When should you use Format or Minify?
Format suits a query copied from a log or pasted into a code review. Minify suits a query embedded in a code string or a single-line config value. Neither mode renames anything or checks syntax. To turn a JSON array into INSERT statements, use the SQL option in the JSON Formatter's Convert view.