How to use the sql minify
- Paste your SQL query into the text area.
- The tool immediately tokenizes the input, stripping line comments (
--) and block comments (/* */). - Whitespace between tokens is collapsed or removed where safe.
- Copy the compressed output from the result box.
How the tokenizer works
To sql minify safely, the tool does not just run a regular expression replace. It scans the input character by character and classifies each piece as a keyword, identifier, number, operator, string literal, quoted identifier or comment.
Single-quoted strings are kept intact, including doubled single quotes used to escape an apostrophe (for example 'O''Brien'). Double-quoted identifiers like "my column" are also preserved exactly. Line comments starting with -- and block comments wrapped in /* */ are discarded entirely.
Whitespace between two alphanumeric tokens (keywords, identifiers, numbers) is reduced to a single space so they do not merge. Between a keyword and punctuation such as a comma or parenthesis, the space is removed. For instance, SELECT id , name becomes SELECT id,name.
Example transformations
The following table shows how typical formatting is compressed when you minify SQL statements.
| Original | Minified |
|---|---|
SELECT a, b FROM t | SELECT a,b FROM t |
WHERE x = 1 AND y = 2 | WHERE x=1 AND y=2 |
INSERT INTO t VALUES (1, 2) | INSERT INTO t VALUES(1,2) |
-- comment | SELECT 1 |
Notice that spaces around operators like =, > and < are removed because they cannot create ambiguity. Spaces between words like INSERT INTO remain because merging them would change the meaning.
When to compress SQL
Reducing query size matters when SQL is embedded in application code, sent over a network repeatedly or stored in constrained environments. A shorter string means fewer bytes transmitted and slightly less parsing work for the database engine.
If you generate queries programmatically using tools like the sql query builder or the sql insert generator, running the output through this sql minify step keeps the final payload tight. Likewise, when converting data formats with csv to sql insert, minifying the resulting statements can save space in migration scripts.
Limitations and dialect notes
This tool uses a tolerant, dialect-agnostic tokenizer. It does not parse SQL grammar or validate syntax. That means it will happily compress invalid queries without warning. If you need to check correctness first, use the sql validator.
Dialect-specific features have limits. Backtick and [bracket] identifiers and PostgreSQL dollar-quoted strings ($tag$...$tag$) are kept exactly as written. Hint comments that start with /*+ or /*! are kept too, because Oracle and MySQL read them as instructions. MySQL # comments are not recognised, and a backslash is not treated as an escape inside strings. Bit-string literals (B'1010') and Unicode escapes are handled only if they follow standard quoting rules. For most ANSI-style SQL used in everyday applications, the output is reliable.
Worked examples
| Example | Input | Result |
|---|---|---|
| Simple SELECT | SQL input: SELECT id, name / FROM users / WHERE active = 1; | SELECT id,name FROM users WHERE active=1; |
| With comments | SQL input: -- fetch all users / SELECT * /* columns */ FROM users; -- end | SELECT*FROM users; |
| String with escaped quotes | SQL input: SELECT 'O''Brien' AS name, "my table"."col 1" FROM t; | SELECT 'O''Brien'AS name,"my table"."col 1" FROM t; |
FAQ
Does this tool change what my query does?
No. It only removes comments and unnecessary whitespace (hint comments such as /*+ ... */ are kept). String literals, identifiers and operators are preserved exactly as written.
Are my queries sent to a server?
No. The sql minify process runs entirely in your browser using JavaScript. Nothing leaves your device.
Will it handle strings that contain quotes?
Yes. Doubled single quotes inside single-quoted strings (the standard SQL escape) are kept intact. The tokenizer recognizes the boundary correctly.
Can I use this with MySQL or PostgreSQL specific syntax?
It works well with standard ANSI SQL. MySQL backticks, T-SQL brackets and PostgreSQL dollar-quoting are kept intact, and /*+ */ and /*! */ hint comments survive. Not handled: MySQL # comments and backslash escapes inside strings.
Why not just use a regular expression to remove spaces?
A simple regex would break string literals that contain spaces or newlines. The tokenizer understands context, so it only removes whitespace outside of strings and identifiers.
How much size reduction can I expect?
Heavily formatted queries with many comments and indentation often shrink by 30 to 50 percent. Already compact queries will see little change.