Data

SQL Minifier

Minify SQL by removing comments and redundant whitespace safely.

Loading interactive tool…

Embed this tool on your site

About this tool

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 removes comments, drops line breaks and squeezes whitespace so the query fits on a single line. It keeps a space only where two words would otherwise run together, so SELECT * FROM becomes SELECT*FROM and status = 'open' becomes status='open'. Text inside single quotes, double quotes and backticks is copied exactly, so whitespace in string values survives. Comment removal is not optional: -- line comments, /* */ block comments (including /*+ optimizer hints */) and # line comments are always stripped. Two edge cases need care: a space-separated double minus such as 5 - -3 becomes 5--3, which starts a comment, and # is treated as a comment even in PostgreSQL, where it is an operator. Backend developers embedding SQL in Go, Rust, Java or Python, and anyone storing queries in JSON configs, use it as a packaging step.

Why use it

  • Strips Whitespace and Comments: Removes line breaks, indentation, extra spaces and every comment, keeping a space only between two word characters. A 200-line formatted query becomes one compact line.

  • String Literal Safe: Whitespace inside single-quoted strings, double-quoted identifiers and backtick-quoted names is preserved exactly. A WHERE clause like WHERE message = 'failed login attempt' keeps its internal spaces intact.

  • Doubled-Quote Escapes Respected: Escaped quotes written as '' or "" inside a literal do not end the string early, so values like 'O''Brien' are copied whole.

  • Single-Statement Output: Even a long 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.

  • 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

  1. Paste a formatted or multi-line SQL query into the SQL Input box
  2. Click Minify SQL to produce the single-line output — comments are always removed
  3. Confirm that string literal content (text inside quotes) looks correct and unchanged
  4. Check expressions such as 5 - -3 or a / *b, which would merge into comment markers
  5. Click Copy and paste the single-line result into your source code, config file, or URL

When it helps

  • 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 logging or tracing infrastructure that handles single-line queries better
  • Removing developer comments from a query before shipping it in application code

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;
Expected result
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';
Expected result
SELECT id,name FROM customers WHERE plan='pro';

String whitespace preserved

Input
SELECT *
FROM logs
WHERE message = 'failed login attempt';
Expected result
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.
  • Wrap negative numbers in parentheses (5 - (-3)) before minifying so the two minus signs cannot merge into a -- comment.
  • Do not minify queries that carry /*+ HINT */ optimizer hints — the minifier removes every block comment, hints included.

Frequently Asked Questions

Will minifying my SQL change how it executes?
Usually not: only whitespace and comments are removed, and a space is kept between words. Watch for two edge cases — a space-separated double minus (5 - -3 becomes 5--3, which starts a comment) and a slash followed by an asterisk (a / *b becomes a/*b). Add parentheses, such as 5 - (-3), before minifying.
Are comments removed from the minified output?
Yes, always. -- line comments, /* */ block comments and # line comments are stripped, and there is no option to keep them. That includes optimizer hints written as /*+ INDEX(t idx) */, so do not minify queries that rely on hints.
Does it preserve whitespace inside quoted strings?
Yes. The minifier tracks quoted string boundaries and never modifies content between matching quotes, backticks or double quotes. WHERE message = 'hello world' keeps its inner space — only whitespace outside literals is removed.
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 get a readable version with the SQL Formatter tool, which re-adds line breaks. Comments and your original spacing cannot be recovered, so keep the source query in version control.
Is it safe for PostgreSQL queries?
Mostly. Dollar-quoted strings ($$ … $$) are not recognised as literals, so their inner whitespace is squeezed, and the # operator is treated as the start of a comment, which deletes the rest of that line. Check PostgreSQL output before using it.
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. This minifier removes all such whitespace except a single space between two word characters.
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. Some engines read optimizer hints written as /*+ ... */; this minifier removes them along with every other block comment.
Single-line comment
A SQL comment beginning with -- (or # in MySQL) that extends to the end of the current line. This minifier always removes both forms.
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.
A LITTLE HELP. A LOT DONE.
Good tools.
Great flow.

Your shortcut to the everyday. Create, convert, calculate,
and get back to what you love.

✓ Free to use✓ No sign-up✓ 500+ possibilities