SQL Minifier
Strip comments and squeeze whitespace out of SQL while keeping string literals, quoted identifiers and optimizer hints intact.
Tool guide
What this tool does
SQL Minifier compresses SQL by removing comments and collapsing unnecessary whitespace with a tokenizer that understands string literals, quoted identifiers, PostgreSQL dollar-quoted bodies and MySQL-style optimizer hint comments, so the statement’s meaning never changes. Choose whether to keep hint comments, treat # as a MySQL line comment, output one line or one statement per line, and see exactly how many bytes were saved.
Formatted SQL is easier for people to read, but the extra blank lines, indentation and inline comments add up when a query is embedded in application code, sent over a constrained connection, or stored in a column with a size limit. Stripping that padding by hand risks breaking a string that happens to contain -- or merging two tokens that needed a space between them.
This minifier tokenizes the SQL first rather than using find-and-replace, so it recognises single, double and backtick-quoted strings, bracketed identifiers, PostgreSQL’s $tag$ dollar-quoted bodies and the backslash escaping MySQL uses inside string literals, and leaves their contents untouched. Optimizer hint comments such as /*+ INDEX(...) */ and MySQL’s /*! ... */ conditional comments can be preserved separately from ordinary comments, since removing them can change how a database executes the query, and everything runs locally in the browser.
How to use SQL Minifier
- Paste a formatted query, or load the Sample to see minification applied to a realistic report query.
- Keep Remove comments on to strip line and block comments; most SQL does not need them at runtime.
- Leave Keep optimizer hints on so /*+ ... */ and /*! ... */ comments survive even when other comments are removed.
- Turn on Treat # as a line comment if you write MySQL, which treats # the same way as -- for single-line comments.
- Choose Output as everything on one line, or one statement per line if you still want some structure.
- Review the Saved percentage in the stats panel, then copy the minified query or download it as query.min.sql.
Tips for better results
- Keeping optimizer hints on by default avoids silently changing a query’s execution plan when hint comments are mixed in with ordinary ones.
- Backslash escaping inside strings is MySQL-specific; leave that option off for PostgreSQL and other databases where a backslash has no special meaning in a string.
- One statement per line keeps a minified batch readable for logs while still removing the comments and indentation that waste space.
- Dropping the final semicolon is useful when a query will be interpolated into a larger statement that already supplies one.
Common use cases
- Shrinking a SQL string embedded in application source code or a configuration file.
- Preparing a query for a system with a maximum statement length, such as some stored procedure or trigger definitions.
- Stripping internal comments from a query before sharing it externally without removing hints that affect performance.
- Comparing the size of a query before and after removing formatting to estimate storage or transfer savings.
Common mistakes to avoid
- Removing all comments including optimizer hints, which can make a previously tuned query fall back to a much slower execution plan.
- Minifying SQL that still has a syntax error; the tokenizer reports an unclosed string or comment instead of guessing at the intent.
- Treating # as a comment character for a database other than MySQL, where it has no special meaning and would be stripped incorrectly.
Frequently asked questions
Does minifying change what the query does?
No, as long as comments other than hints are the only thing being removed. The tokenizer preserves every string literal, quoted identifier and dollar-quoted body exactly as written.
Why keep optimizer hint comments by default?
Hints such as /*+ INDEX(...) */ in Oracle and MySQL and /*! ... */ conditional comments in MySQL change how the database plans or executes the query, so removing them is a behavioural change, not just a formatting one.
What happens to a comment that contains a semicolon?
It is removed along with the rest of the comment text before statements are split, so a semicolon inside a removed comment does not create an extra statement boundary.
Can I minify several statements at once?
Yes. Paste a semicolon-separated script and every statement is minified; choose one statement per line in Output if you want to keep them visually separated.
Feedback & suggestions
Help us improve SQL Minifier. We read every message.