Resell services with the EthicalSMM API

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

  1. 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.
  2. 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.
  3. Every run of spaces, tabs and line breaks becomes one space.
  4. Spaces next to ( ) , ; = < > + are dropped, so c.id = o.customer_id becomes c.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.
  5. 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?
Paste the query and press Minify SQL. Line breaks and extra spaces become single spaces, comments are removed, and the whole query comes out on one line.
Will minifying change the text inside my strings?
No. Single-quoted strings, double-quoted identifiers, backtick names and [bracketed] names are set aside before anything else happens and put back exactly as they were.
Does the SQL minifier remove comments?
Yes by default. Choose Keep (as /* */) to keep them; line comments are rewritten as block comments so they do not hide the rest of the one-line query.
Why is my # comment still in the output?
The # comment style is MySQL-specific, so it is only treated as a comment when Treat # as a comment (MySQL) is set to Yes. Leave it on No for databases where # means something else.
Is a minified query faster to run?
Not noticeably. The database ignores whitespace and comments when it parses a query, so minifying mainly saves space and makes the query easy to embed in code or config.
What are /*! */ comments and why are they kept?
In MySQL, code inside /*! ... */ is executed by MySQL and ignored by other databases. Removing it could change what the query does, so the tool always keeps it.

Related guides

Related tools

People also used

Embed this tool on your site

Paste it into any blog or website. Free, with a credit link.