SQL minifier — put a query on one line without its comments

Paste a SQL query or script and get a compact one-line version with -- and /* */ comments removed, while strings, quoted identifiers and optimizer hints stay exactly as written.

Live demo · real result from this tool

Minified SQL

SELECT id,name FROM users WHERE note='keep -- this' AND created_at >='2026-01-01';
Saved45.5%
Original
154 B
Minified
84 B

Processed on your device — nothing is uploaded.

How to use the SQL minifier

  1. Paste your query or script into the SQL box. The sample has line comments, a block comment and a string containing --.
  2. Tick Treat # as a comment (MySQL) if your SQL is for MySQL or MariaDB.
  3. Copy the Minified SQL. Saved, Original and Minified show the size change in bytes.

What it does and when to use it

A query that reads well in an editor is often awkward elsewhere: inside a string in application code, in a JSON config, in a shell command, in a log line or in a URL parameter for a dashboard tool. Minifying puts it on one line and strips comments that might contain internal notes. It is also a quick way to compare two queries that differ only in layout.

To go back to a readable layout, use the SQL formatter. The XML minifier and CSS minifier do the same job for other formats.

How it works

The minifier reads the SQL one character at a time in your browser and tracks where it is:

  • -- to the end of the line is a comment, and so is # when the MySQL option is on.
  • /* … */ is removed unless it starts with /*+ or /*!.
  • Quoted text in '…', "…", `…` and […] is copied whole, including doubled quotes such as 'it''s'.
  • PostgreSQL dollar-quoted bodies, $$ … $$ or $tag$ … $tag$, are copied whole, comments and line breaks included.
  • Any run of whitespace becomes one space. That space is dropped after , ( ; = and before , ) ; =.

Worked examples

These are the tool’s own outputs.

The sample query. Seven lines with three comments become:

SELECT id,name FROM users WHERE note='keep  --  this' AND created_at >='2026-01-01';

Saved: 45.5%, from 154 bytes to 84. The -- inside the string survives because it is not a comment there.

MySQL with # comments. SELECT name # MySQL note, FROM `users` and WHERE id IN (1, 2, 3); on three lines, with the MySQL option ticked, give SELECT name FROM `users` WHERE id IN (1,2,3);.

A PostgreSQL function. In CREATE FUNCTION f() RETURNS int AS $$ … $$ LANGUAGE sql; the body between the $$ markers keeps its line breaks and its -- body comment, because it is a string to the database. Nothing outside it needed changing, so the saving is 0%.

Limits and tips

  • Keywords are not changed. Upper and lower case stay as you wrote them.
  • No renaming or shortening of tables, aliases or values.
  • Keep the readable version in your repository and minify copies for embedding.

Frequently asked questions

Can minifying change what my query does?
It should not. Only comments and whitespace outside strings are removed. Text inside single quotes, double quotes, backticks and square brackets is copied exactly, so 'keep -- this' keeps its double spaces and its dashes.
When should I tick "Treat # as a comment"?
Tick it for MySQL and MariaDB, where # starts a comment. Leave it off for other databases, where # can be part of a name or an operator. If you leave it off for a MySQL query that has # comments, the comment swallows the rest of the line once it is joined, so the result breaks.
Why does a space stay before >= but not after it?
The minifier removes a space only after a comma, opening bracket, semicolon or equals sign, or before a comma, closing bracket, semicolon or equals sign. Everywhere else it keeps one space, which is always safe. created_at >= '2026-01-01' therefore becomes created_at >='2026-01-01'. Both forms run the same.
Are optimizer hints kept?
Yes. Comments that start with /*+ (Oracle and MySQL hints) or /*! (MySQL version comments) change how the query runs, so they are kept. All other block comments are removed.
Does it check my SQL?
No. It does not parse or validate the query. A typo in the input is still there in the output.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close