Help

SQL Minifier: Compress a SQL Query to One Line

This SQL minifier turns a formatted, multi-line query into one compact line. Paste the query into the SQL query box and the Minified SQL box fills in as you type. Line breaks, tabs and repeated spaces collapse to single spaces, comments can be dropped, and anything inside quotes is left exactly as you wrote it. It works on the text of any dialect, and the query is minified in your browser, so nothing is uploaded.

How to Minify a SQL Query

  1. Paste or type your query into the SQL query box. Press Clear, which appears above the box once it has text, to start over.
  2. Leave Remove comments ticked to drop comments, or untick it to keep them.
  3. Read the one-line result in Minified SQL. It updates on every keystroke, so there is no Minify button.
  4. Press Copy. The button reads Copied for a moment, and the line under the result shows how much smaller it is, for example 34.3% smaller: 105 bytes to 69 bytes.

Before and After: Multiline SQL to a Single Line

The same query pasted with Remove comments on and then off. The string 'a b' keeps both of its spaces either way.

Before SELECT id, name -- display name
FROM users
/* active only */
WHERE status = 'active'
  AND note = 'a b';
After, Remove comments on SELECT id, name FROM users WHERE status = 'active' AND note = 'a b';
After, Remove comments off SELECT id, name /* display name */ FROM users /* active only */ WHERE status = 'active' AND note = 'a b';

What the Minifier Changes and What It Leaves Alone

In your queryWhat happens
Spaces, tabs and line breaks outside quotesEach run becomes one space; the start and end of the query are trimmed
Spaces around commas, operators and bracketsKept as one space where you had any, never removed
-- line and /* block */ commentsRemoved with Remove comments on, kept on one line with it off
/*+ hint */ and /*! conditional */Always kept, because the database reads them
Text in single quotes, double quotes and backticksKept byte for byte, including repeated spaces and line breaks
PostgreSQL $$ ... $$ and $tag$ ... $tag$ bodiesKept as they are
Keyword case, table and column namesNot changed

Remove Comments from a SQL Query

With Remove comments ticked, a -- comment is removed up to the end of its line and a /* ... */ comment is removed whole. Each removed comment leaves one space behind so the words around it never join together. An optimizer hint such as /*+ INDEX(e emp_idx) */ or a MySQL conditional comment such as /*! STRAIGHT_JOIN */ is not a comment to the database, so it stays in the result either way.

Keeping Comments Without Breaking the Query

Untick Remove comments and the block comments stay, collapsed onto one line. A -- comment cannot simply stay: once the query is on one line it would comment out everything after it. So the tool rewrites each one as a block comment with the same text, and -- display name comes out as /* display name */. A comment that is kept can make the result as long as the original, or longer.

Strings, Quotes and Dollar-Quoted Bodies Stay as Written

The tool reads each quote through to its closing mark before it looks at anything else, so a --, a /* or a run of spaces inside a string is part of the string, not a comment or extra whitespace. Doubled quotes such as 'it''s' and backslash escapes are followed correctly, and a PostgreSQL function body between $$ markers is copied untouched. If a quote is never closed, the rest of the text is kept as you typed it.

Where a One-Line SQL Query Helps

If the query sits inside a JavaScript object and needs strict, escaped JSON, the JSON Stringifier writes that. To shrink a stylesheet the same way, there is the CSS Minifier.

The tool does not parse or validate SQL, and it cannot tell whether the query will run. It only touches whitespace and comments, so a query that was wrong before is still wrong after. Keep your formatted query as the source, because the original layout and any removed comments cannot be recovered from the one-line copy.


Frequently asked questions

What does a SQL minifier do?

A SQL minifier turns a formatted, multi-line query into a single compact line. Runs of spaces, tabs and line breaks outside quotes become one space, and comments can be removed. It does not change the query's meaning, and it does not make the query run faster, because the database ignores that whitespace anyway. The saving is in the size of the text, which matters when the query has to fit inside a JSON file, a shell command, a log line or a URL.

How do I minify a SQL query?

Paste the query into the SQL query box. The Minified SQL box fills in as you type, and the line under it shows how much smaller it is, for example 34.3% smaller: 105 bytes to 69 bytes. Press Copy to put the one-line result on your clipboard. There is no Minify button to press.

How do I convert a multiline SQL query to a single line?

Paste the multiline query into the SQL query box and copy the Minified SQL result. Every line break and every run of spaces or tabs outside quotes becomes a single space, so the statement ends up on one line. A string that itself contains a line break keeps that line break, because the tool never edits text inside quotes.

How do I remove comments from a SQL query?

Leave Remove comments ticked, which is the default. Both -- line comments and /* block */ comments are removed and replaced by one space, so the words on either side do not run together. Untick Remove comments to keep them.

Which comments does it keep even when Remove comments is on?

Optimizer hints such as /*+ INDEX(e emp_idx) */ and MySQL conditional comments such as /*! STRAIGHT_JOIN */. The database reads those as instructions, so removing one could change how the query runs. They are always kept, collapsed onto one line.

Why did my -- comment turn into a /* */ comment?

That happens when Remove comments is unticked. A -- comment runs to the end of the line, so on a single line it would comment out the whole rest of the query. To keep the comment safely, the tool rewrites it as a /* ... */ block comment with the same text. With Remove comments ticked, the comment is simply dropped.

Does it change the text inside my quoted strings?

No. Text inside single quotes, double quotes and backticks is kept byte for byte, including doubled quotes, backslash escapes, repeated spaces and a -- or /* that sits inside the string. PostgreSQL dollar-quoted bodies, written $$ ... $$ or $tag$ ... $tag$, are kept as they are too.

Will the minified SQL run the same as the original?

It should, because only whitespace and comments change. The tool does not parse or validate SQL, so it will not tell you whether the query is correct, and it does not change keyword case or touch the spaces around commas, operators and brackets. Run the result against a test database before you rely on it.

Does it work with MySQL, PostgreSQL, SQL Server, SQLite and Oracle?

Yes, for the text of a query in any of them. It only touches whitespace and comments, so it does not need to know the dialect. It protects text in single quotes, double quotes and backticks, and PostgreSQL dollar-quoted bodies. SQL Server names in square brackets are not specially protected, so a repeated space inside one is collapsed.

Can I turn minified SQL back into readable SQL?

Not with this tool. Minifying throws the original line breaks and indentation away, and removed comments are gone for good, so keep your formatted query as the source and use the one-line copy only where it is needed. To read a minified query again, run it through a SQL formatter in your editor or database client.

Is my query sent anywhere?

No. The query is minified in your browser as you type, nothing is uploaded or saved, and no account is needed.

Recommended

Helps keep the tools free. ✨

Images are for illustration only. As an Amazon Associate I earn from qualifying purchases.