UtilityKit

500+ fast, free tools. Most run in your browser only; Image & PDF tools upload files to the backend when you run them.

SQL Minifier

Minify SQL by removing comments and redundant whitespace safely.

About SQL Minifier

When SQL needs to live inside source code — embedded in a Go string, packed into a JSON config, or sent over a URL parameter — multi-line formatted queries are awkward. The SQL minifier strips redundant whitespace, optionally removes comments, and produces a single logical line without changing query logic. Backend developers embedding SQL in Go, Rust, Java, or Python, ORM users storing queries in JSON configs, and SREs packing queries into URL parameters all use minification as a packaging step. The tool is string-literal safe: whitespace inside quoted values is preserved — only outside-quote whitespace is collapsed. Comment removal is optional so you can keep query hint directives the database reads. Pair with the SQL Formatter to reverse the operation with no information lost.

Why use SQL Minifier

Strips Whitespace, Keeps Logic

Removes line breaks, leading and trailing spaces, and consecutive spaces throughout the query — never alters keywords, identifiers, operator symbols, or string literals. The resulting query is logically identical to the original and produces the same execution plan.

Comment Removal Optional

Choose to strip -- and /* */ comments for lean production output, or keep them when your team logs annotated queries for debugging. MySQL and PostgreSQL /*+ HINT */ directives are preserved when comment stripping is disabled.

String Literal Safe

Whitespace inside single-quoted string values is preserved exactly — only whitespace outside quoted strings is collapsed. A WHERE clause like WHERE message = 'failed login attempt' keeps its internal spaces intact.

Single-Statement Output

Even a 200-line formatted query becomes one logical line, ready for backtick embedding in Go, triple-quoted strings in Python, or escaped string literals in Java — no multi-line string workarounds needed.

Reversible With Formatter

Pair with the SQL Formatter to reverse the operation. Minify for source-code embedding and bulk loading, format for review and pull requests. No information is lost in either direction — the round-trip is lossless.

Local-Only

Production queries containing schema names, customer identifiers, and proprietary business logic stay in your browser tab. No data is sent to any server and no logs are written during minification.

How to use SQL Minifier

  1. Paste a formatted or multi-line SQL query into the input panel
  2. Choose whether to strip -- single-line and /* */ block comments, or keep them inline
  3. Click Minify to produce the compacted single-line output
  4. Confirm that string literal content (text inside single quotes) looks correct and unchanged
  5. Copy the single-line result and paste it into your source code, config file, or URL
  6. Use the SQL Formatter tool to expand it back to readable form whenever you need to review

When to use SQL Minifier

  • Embedding a complex SQL query as a string constant in Go, Rust, Java, or Python source code where multi-line strings are verbose
  • Storing queries in a JSON configuration file where embedded newlines would require escape sequences throughout
  • Packing a query into a URL parameter or query string where line breaks and extra spaces add unnecessary bytes
  • Preparing SQL for a context where your logging or tracing infrastructure handles single-line queries better
  • Reducing query string size for environments with strict character limits on API query parameters
  • Normalising SQL for string comparison or caching key generation where whitespace differences would cause false mismatches

Examples

Pretty SQL → one line

Input: SELECT u.id, u.email FROM users u WHERE u.created_at > '2025-01-01' AND u.active = true ORDER BY u.id;

Output: SELECT u.id,u.email FROM users u WHERE u.created_at>'2025-01-01' AND u.active=true ORDER BY u.id;

With comments stripped

Input: -- top customers SELECT id, name /* only paid */ FROM customers WHERE plan='pro';

Output: SELECT id,name FROM customers WHERE plan='pro';

String whitespace preserved

Input: SELECT * FROM logs WHERE message = 'failed login attempt';

Output: SELECT * FROM logs WHERE message='failed login attempt';

Tips

  • Performance gain from SQL minification is essentially zero at runtime — minify for embedding ergonomics, not for query speed.
  • Keep a formatted version of the same query in a .sql file in version control; minify only as a build-time or copy-paste step.
  • If your query is going into a JSON config file, minify first and then JSON-escape the result — the order matters for correct escaping.
  • When embedding in Go, prefer raw string backticks for multi-line SQL instead of minifying — but minify when the query must be a regular string constant.
  • Strip comments only if you are certain they are not execution hints — MySQL and Oracle use /*+ HINT */ syntax that the database reads, not ignores.

Frequently Asked Questions

Will minifying my SQL change how it executes?
No. Minification removes only whitespace tokens between keywords, identifiers, and operators. It never alters the order of clauses, changes operator precedence, or modifies any value. The query's execution plan and result set are identical before and after.
Are comments removed from the minified output?
Only if you enable comment stripping. By default, -- single-line comments and /* */ block comments are removed. If your comments include execution hints like /*+ INDEX(t idx) */ that the database engine reads, keep comment removal off.
Does it preserve whitespace inside quoted strings?
Yes. The minifier tracks quoted string boundaries and never modifies content between matching single-quote characters. WHERE message = 'hello world' stays exactly as written — only whitespace outside string literals is collapsed.
Why minify SQL — does it actually run faster?
Minification gives essentially zero runtime performance gain — modern query parsers handle whitespace in microseconds. The benefit is ergonomic: minified SQL fits cleanly inside string literals, JSON values, and URL parameters without multi-line quoting or escape sequences.
Can I get the original query back from minified output?
You can recover a readable formatted version using the SQL Formatter tool. The formatter will re-add indentation and line breaks. However, original comment text and exact spacing are lost if you stripped comments — keep the original in version control.
Will it break BigQuery or PostgreSQL specific syntax?
No. Minification is syntax-agnostic — it collapses whitespace without parsing dialect-specific constructs. STRUCT(), ARRAY(), and other BigQuery or PostgreSQL extensions are preserved because only whitespace tokens are removed.
Are -- single-line comments handled correctly?
Yes. Single-line comments from the -- character to the end of the line are detected and removed when comment stripping is enabled. Without stripping, they are preserved as inline text in the minified output.
Is the query sent to your server?
No. Minification runs entirely in your browser using JavaScript string processing. No network request is made and no data is sent to any server. Production queries with sensitive schema names or business logic are safe to paste.

Explore the category

Glossary

Whitespace token
Any sequence of space, tab, or newline characters in a SQL query that serves only as separation between tokens. Minification collapses all whitespace tokens to a single space or removes them entirely when they adjoin operators.
Line terminator
A newline character (\n or \r\n) used to end a line in a multi-line SQL query. Minification removes all line terminators, producing a single-line output.
String literal
A sequence of characters enclosed in single quotes in SQL, such as 'hello' or 'failed login attempt'. Whitespace inside a string literal is semantically meaningful and must not be modified by minification.
Block comment
A SQL comment written between /* and */ that can span multiple lines. Block comments containing execution hints (/*+ ... */) are used by some database engines and should be preserved when those hints are needed.
Single-line comment
A SQL comment beginning with -- that extends to the end of the current line. Single-line comments are removed during minification when comment stripping is enabled.
Wire size
The byte count of a query string as transmitted over a network connection or stored in a config. Minification reduces wire size by removing whitespace, which matters for URL parameters and request-size-limited APIs.