What the SQL minifier does
This SQL minifier turns a formatted, commented query into a compact single line. It walks through the SQL with a tokenizer that understands strings and comments, so it knows which spaces and line breaks are safe to remove. -- and /* */ comments are dropped, whitespace is collapsed, and spaces next to commas, parentheses, semicolons and equals signs are removed entirely. Everything inside string literals and quoted identifiers is copied byte for byte.
Example
-- monthly revenue
SELECT
date_trunc('month', created_at) AS month, /* bucket */
SUM(total) AS revenue
FROM orders
WHERE status = 'paid' -- only paid
GROUP BY 1
ORDER BY 1;
SELECT date_trunc('month',created_at)AS month,SUM(total)AS revenue FROM orders WHERE status='paid' GROUP BY 1 ORDER BY 1;
The status bar reports 179 → 121 bytes (32% smaller). Both comment styles are gone, and )AS is valid SQL because a closing parenthesis already separates the tokens.
What is kept exactly
- String literals in single quotes, including doubled quotes (
'it''s') and backslash escapes. A--inside a string is not treated as a comment. - Quoted identifiers:
"double quoted"(standard SQL, PostgreSQL, Oracle) and`backticks`(MySQL, BigQuery). - PostgreSQL dollar-quoted strings such as
$$ ... $$or$fn$ ... $fn$, common in function bodies. - Optimizer hints written as
/*+ INDEX(o idx_orders_date) */(Oracle, MySQL 8) and MySQL's version comments/*! ... */, because removing them would change how the query runs.
SELECT /*+ INDEX(o idx_orders_date) */ o.id FROM orders o WHERE o.note='it''s spaced -- not a comment';
In this output the hint survived, the ordinary comment was removed, and the spaces and -- inside the string were left untouched.
When to minify SQL
- Embedding queries in code or config, such as a JSON setting, an environment variable or a single-line log statement.
- Reducing payload size when queries are sent over an API, for example to a reporting service.
- Comparing queries: two versions that differ only in whitespace and comments minify to the same line, which makes real differences easy to spot.
- Removing comments before sharing a query, when the comments contain internal notes.
Minification does not make a query run faster on the database: the server parses whitespace and comments almost instantly. For readability, keep the formatted version in your repository and generate the minified one when needed.
Minifying is not escaping
A minified query fits on one line, but it may still contain characters that are special where you paste it. Inside a JSON string, double quotes and backslashes must be escaped; inside a shell command, single quotes need care. After minifying, run the line through JSON Escape if it is going into a JSON config, and never build SQL by concatenating user input: use parameterized queries in your application instead.
Limits and caveats
- Line comments (
--) are removed up to the end of their line before lines are joined, so minifying can never turn the following code into part of a comment. - MySQL-style
#comments are not removed. This page has no dialect setting, and#is an operator in other databases, such as#>in PostgreSQL. A#standing on its own keeps the rest of its line and the line break, so the output then has more than one line; a comment written without a space, as in#note, is treated as ordinary text and joined with the next line. Remove such comments before minifying, or use the Minify button of the SQL Formatter with the MySQL, MariaDB, TiDB, SingleStoreDB or BigQuery dialect selected, which drops them. - Unterminated strings, quoted identifiers or
/*comments stop the minifier with an error, since there is no safe way to guess where they end. - Keyword case is not changed; use the SQL Formatter for that, or to turn a minified query back into a readable layout.
To put a value safely inside a SQL string, use SQL Escape. The minifier runs locally in your browser.