SQL Minifier
Strip comments and extra whitespace from SQL to put a query on one line, without touching anything inside quoted strings or identifiers.
Was this tool helpful?
Thanks for the feedback!
This tool compresses a SQL query onto a single line by removing comments and extra whitespace. Text inside quoted strings and quoted identifiers is left exactly as it was, so a value like 'paid -- confirmed' survives untouched. It is useful when you need to put a query inside a code string, a JSON or YAML config, a log line or a command-line call.
The settings
Comments defaults to Remove. Choose Keep (as /* */) to hold on to them; line comments that start with -- are rewritten as /* … */ so they cannot swallow the rest of the line once everything is joined up.
Treat # as a comment (MySQL) defaults to No. MySQL also accepts # as a line comment. If your query uses that style, set this to Yes. If you leave it on No, the # comment stays in the output, and on a single line it would comment out everything after it.
Press Minify SQL. The cards show the length before and after in characters, and the percentage saved.
What the minifier does
- Quoted text is set aside untouched: single-quoted strings, double-quoted identifiers, MySQL backtick identifiers and SQL Server
[bracketed]names. Doubled quotes such as'O''Brien'and backslash escapes are respected. - Comments are removed:
--to the end of the line,/* … */blocks, and#lines if you turned that on. MySQL's special/*! … */comments are always kept, because MySQL runs the code inside them. - Every run of spaces, tabs and line breaks becomes one space.
- Spaces next to
( ) , ; = < > +are dropped, soc.id = o.customer_idbecomesc.id=o.customer_id. A space is put back after a semicolon between statements, and between keywords like IN, VALUES or EXISTS and an opening bracket. - The quoted text is put back exactly as it was.
Keywords are not changed to upper or lower case, and nothing is reordered. An empty box gives the message "Paste the SQL you want to minify."
Worked example
-- Monthly sales report
SELECT o.id,
c.name AS customer,
o.total
FROM orders o
JOIN customers c ON c.id = o.customer_id /* only paid orders */
WHERE o.status = 'paid -- confirmed'
AND o.city IN ('Pune', 'Nagpur')
ORDER BY o.total DESC;
With the default settings this 264-character query becomes 181 characters, a 31% saving:
SELECT o.id,c.name AS customer,o.total FROM orders o JOIN customers c ON c.id=o.customer_id WHERE o.status='paid -- confirmed' AND o.city IN ('Pune','Nagpur')ORDER BY o.total DESC;
Both comments are gone, but the double space and the -- inside the status string are kept because they are part of the value. The missing space in )ORDER BY is fine: SQL does not need a space after a closing bracket. With Comments set to Keep, the result is 230 characters (13% saved) and starts with /* Monthly sales report*/.
Edge cases to know
The tool treats every -- as the start of a comment, as standard SQL and PostgreSQL do. MySQL only treats -- as a comment when a space or control character follows it. So SELECT 5--3, which MySQL reads as 5 minus minus 3, is minified to SELECT 5. Write 5 - -3 and it is left as it is.
With Comments set to Keep, any */ inside a -- comment is changed to * /, so the rewritten block comment cannot end early. Keeping comments can make the output slightly longer than the input, which shows as a negative saving.
A minified query is hard to read. To go the other way, the SQL Formatter lays a one-line query back out over several lines. For long lists of values in a WHERE clause, the SQL IN Clause Generator builds the list from a column of IDs.
Frequently asked questions
How do I convert a multi-line SQL query to one line?
Will minifying change the text inside my strings?
Does the SQL minifier remove comments?
Why is my # comment still in the output?
Is a minified query faster to run?
What are /*! */ comments and why are they kept?
Related guides
- Free Developer Tools That Replace a Dozen Browser Tabs Free developer tools grouped by job: format, convert, encode, generate and test. What each one does, which run on the server, and the limits to know f...
- How to Beautify CSS, HTML and XML (Online, VS Code, Prettier) Beautifying adds line breaks and indentation so minified CSS, HTML or XML is readable again. How the free formatters work, their limits, and VS Code a...
- How to Format JSON: Pretty-Print, Validate and Read Errors Paste JSON into a formatter to indent it and catch errors. The exact syntax rules, how to read an error position, and how to do it in VS Code, Python...