FormatterHQ

SQL Minifier

Compress SQL to a single line: comments and extra whitespace are removed, while string literals, quoted names and /*+ hints */ are kept exactly.

Input SQL
Output SQL
Ready.

How to use the SQL Minifier

  1. Paste your SQL query or script into the Input editor, or use Sample, Upload file or Load URL.
  2. Press Minify; it runs automatically while Auto update is on.
  3. Check the size saved in the status bar.
  4. Use Copy or Download above the output panel to take the minified SQL with you.

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.

Frequently asked questions

Does minifying SQL improve query performance?
No. Databases parse whitespace and comments quickly, and the execution plan is the same. Minifying is about size and convenience, for example when embedding a query in code.
Are optimizer hints removed?
No. Comments starting with /*+ (optimizer hints) and /*! (MySQL version-specific code) are kept because they affect how the query runs. All other -- and /* */ comments are removed.
Will it break strings that contain -- or /*?
No. The minifier tracks string literals and quoted identifiers, so comment markers inside them are treated as text and copied unchanged.
Which SQL dialects are supported?
The minifier works on the shared lexical rules of SQL, so it handles MySQL, PostgreSQL, SQL Server, Oracle, SQLite and others, including backtick identifiers and PostgreSQL dollar quoting. MySQL-style # comments are the exception: they are not removed, because # is an operator in other dialects.

Last updated October 3, 2026

Guides

Related tools