SQL Minifier: Compress SQL Queries by Removing Whitespace
Strip whitespace, newlines, and comments from SQL queries to produce compact, single-line output. Useful for embedding SQL in APIs, config files, and environment variables.
Try the free online tool
Runs entirely in your browser — no signup, no uploads.
Formatted SQL is ideal for human readers, but there are many situations where compact, single-line SQL is preferable or even required. Embedding SQL in JSON configuration files, passing queries as URL parameters, storing them in environment variables with character limits, sending them in API request bodies, or including them in log messages where newlines would break parsing all benefit from compact SQL with whitespace removed.
SQL minification strips all redundant whitespace, including leading and trailing spaces, blank lines, unnecessary spaces around operators, and newlines between clauses, while preserving a single space wherever the SQL syntax requires one. It also removes SQL comments (both -- line comments and /* block comments */) that are useful during development but add unnecessary bytes in production.
This tool produces the smallest valid SQL representation of your query while guaranteeing that the minified output is syntactically equivalent to the original. It handles string literals and quoted identifiers correctly, never removing whitespace from inside quoted content where it is semantically significant.
What Is SQL Minification and When Is It Needed?
SQL minification is the process of removing all whitespace and comments from a SQL query that are not syntactically required. Like JavaScript minification for web assets, SQL minification reduces the size of the query string without changing its meaning. A well-formatted 20-line SELECT statement might compress to a single line with 60% fewer characters.
The need for compact SQL arises in several practical situations. Many systems that accept SQL as configuration input (such as AWS Athena named queries, database migration tools, or monitoring dashboards) store queries as single-line strings in JSON or YAML. URL-encoded SQL in query parameters must be short enough not to exceed URL length limits. Log aggregation systems that parse structured logs are often sensitive to newlines within field values.
Another use case is comparing two queries for equivalence. After minifying both queries, a simple string comparison can quickly show whether they are identical, which is useful when auditing query changes in version control or validating that a refactored query produces the same SQL as the original.
How to Use This Tool
Follow these steps to minify your SQL:
- 1
Paste your formatted SQL
Copy your well-formatted, human-readable SQL query into the input panel. The tool accepts single queries and multi-statement scripts.
- 2
Configure comment handling
Choose whether to remove all comments (the default), preserve block comments only (useful for preserving query hints like MySQL's /*+ INDEX(...) */ optimizer hints), or preserve all comments.
- 3
Set keyword case option
Optionally normalize keyword casing during minification: convert all SQL keywords to uppercase, lowercase, or preserve the original case. Combining minification with keyword normalization ensures consistent output.
- 4
Click Minify
Press the Minify button to produce the compressed SQL output. The tool shows the original character count and the minified character count so you can see the reduction achieved.
- 5
Copy the minified output
Copy the single-line SQL from the output panel. It is ready to embed in JSON, pass as an API parameter, or store in an environment variable.
Common Use Cases
SQL minification is useful in these development and operations scenarios:
- Embedding SQL queries in JSON configuration files for tools like AWS Glue, dbt, or Redash where multi-line strings are inconvenient
- Storing SQL in environment variables or Kubernetes ConfigMaps that have character limits or where newlines cause parsing issues
- Passing SQL queries as URL parameters or in API request bodies where compact encoding reduces payload size
- Comparing two versions of a query for functional equivalence by minifying both and performing a string comparison
- Removing development-time comments from SQL before including it in production code or migration scripts
Tips and Best Practices
Keep these points in mind when minifying SQL:
- Always maintain the formatted version of your SQL as the source of truth in version control. Minification should be a build step or copy-paste operation, not a permanent transformation of your source SQL.
- Be careful with optimizer hints in MySQL (/*+ HINT */) and PostgreSQL (/*+ pgHint */) as these are comments that actually affect query execution. Use the 'preserve block comments' option to keep them intact during minification.
- SQL string literals (single-quoted values) are never modified by the minifier. Spaces and newlines inside string literals are preserved exactly, since they are part of the data value, not query structure.
- When embedding minified SQL in JSON, remember that backslashes inside the SQL (used in LIKE patterns, for example: LIKE 'prefix\%') must be double-escaped in JSON strings. Use a JSON encoder to safely embed SQL rather than constructing JSON by hand.
- For queries used as template strings with parameter placeholders (? in MySQL, $1 in PostgreSQL), verify after minification that all placeholders are still present and in the correct order.
Frequently Asked Questions
Does minification change the behavior of the SQL query?
No. Minification only removes whitespace and comments that are not part of the SQL data model. The database parser treats minified and formatted versions of the same query identically. The resulting execution plan is the same.
Will the minifier remove spaces inside string literals?
No. The minifier correctly identifies string literals (single-quoted values and dollar-quoted strings in PostgreSQL) and never modifies content inside them. Spaces, tabs, and newlines within quoted strings are part of the string value and are preserved exactly.
How does the minifier handle SQL optimizer hints?
By default, all comments including block comments are removed. If your SQL uses optimizer hints written as special block comments (such as MySQL's /*+ INDEX */ or PostgreSQL's pgHint), enable the 'preserve block comments' option to keep them while still removing regular -- line comments and whitespace.
Can I minify a SQL script with multiple statements?
Yes. Multiple statements separated by semicolons are minified individually and joined with a single space or semicolon in the output. You can configure whether the output preserves statement separators or joins them without delimiters.
What is the typical size reduction from minifying SQL?
Well-formatted SQL with comments and generous spacing typically reduces by 40 to 70 percent after minification. The exact reduction depends on how much whitespace and how many comments the original query contains. CTEs with many WITH clauses benefit most.
Is there any risk of the minifier breaking valid SQL?
The minifier is conservative: it only removes whitespace between tokens and does not reorder, combine, or modify the tokens themselves. Edge cases can arise with dialect-specific comment syntax or non-standard extensions, so always test minified output against your target database for unfamiliar queries.
Should I minify SQL in my application source code?
For most applications, no. The database query planner does not care about whitespace, and keeping SQL readable in source code is more valuable than the trivial byte savings. Minification is best reserved for cases where the SQL must fit specific constraints: environment variables, URL parameters, JSON fields, or systems with string length limits.
Ready to use this tool?
Free, instant, no account required. Runs entirely in your browser.