Blog
Wild & Free Tools

SQL to One Line — Minify SQL Queries in Your Browser

Last updated: April 2026 6 min read

Table of Contents

  1. Why you need SQL on one line
  2. How the minify button works
  3. Minify a query — step by step
  4. Embedding minified SQL in code
  5. When to NOT minify
  6. Round-trip workflow
  7. Frequently Asked Questions

Sometimes you want the opposite of pretty SQL. You want one line — for embedding in a Python string, a config YAML, a shell script, or a JIRA ticket where multi-line code looks broken. The catch: collapsing SQL to one line by hand means manually removing line breaks, normalizing whitespace, and dealing with comments that break the query when joined.

Our formatter has a Minify button that does this in one click. Paste multi-line SQL, click Minify, and the output is a single-line version with comments stripped and whitespace normalized. Safe to embed in code, config, or anywhere a one-liner is needed.

Why You Sometimes Need SQL on a Single Line

Pretty SQL is great for reading. One-line SQL is necessary for these cases:

Each of these is a place where multi-line SQL is technically possible but practically painful.

How the Minify Button Works

Click Minify (instead of Format) and the tool does the following:

  1. Removes single-line comments — anything after -- on a line is stripped.
  2. Removes block comments — /* ... */ blocks are removed entirely.
  3. Collapses all whitespace — newlines, tabs, and multiple spaces become single spaces.
  4. Trims around punctuation — spaces around commas, semicolons, and parentheses get normalized.
  5. Preserves string literals — anything inside single quotes stays exactly as written, including any internal whitespace.

The result is a valid one-line SQL query that runs identically to the original (assuming no commented-out code was important).

How to Minify a SQL Query in Your Browser

  1. Paste your multi-line SQL into the formatter input.
  2. Click Minify — not Format. Minify is the second button in the row.
  3. Output appears as a single line — with syntax highlighting if your browser supports it.
  4. Click Copy — the one-line SQL is on your clipboard.
  5. Paste it into your code, config, or wherever you need it.

If you want to switch back to a pretty version later, paste the one-line SQL back in and click Format. The round trip is lossless except for stripped comments.

Sell Custom Apparel — We Handle Printing & Free Shipping

Embedding Minified SQL in Code — Common Patterns

Python: sql_string = "SELECT id, name FROM users WHERE status = 'active' ORDER BY id LIMIT 100" — one line, no triple-quoted heredoc needed.

JavaScript/TypeScript: const sql = "SELECT id, name FROM users WHERE status = 'active' ORDER BY id LIMIT 100"; — works with all string syntaxes.

Go: sql := "SELECT id, name FROM users WHERE status = 'active' ORDER BY id LIMIT 100" — backticks not needed for single-line strings.

Java: String sql = "SELECT id, name FROM users WHERE status = 'active' ORDER BY id LIMIT 100"; — works without text blocks.

Shell: psql -c "SELECT id, name FROM users WHERE status = 'active' ORDER BY id LIMIT 100" — quoted argument, no escaping needed.

YAML: query: "SELECT id, name FROM users WHERE status = 'active' ORDER BY id LIMIT 100" — single-line value avoids YAML block scalar gotchas.

JSON: {"query": "SELECT id, name FROM users WHERE status = 'active' ORDER BY id LIMIT 100"} — JSON does not allow multi-line strings, so minification is required.

When You Should NOT Minify SQL

Minification has costs. Do not do it for these cases:

Minify for embedding in code where the query is a string. Keep it pretty for everything else.

The Round-Trip Workflow — Minify and Restore

Most developers do not actually need a permanently minified version. They need to minify temporarily for embedding, then restore to pretty format if they need to debug or modify.

The browser tool supports this round trip:

  1. Format your query pretty — work on it, debug it, get it right.
  2. Save the pretty version — to a .sql file in your project.
  3. Click Minify — get the one-line version.
  4. Embed the minified version in your code — Python string, config file, etc.
  5. When you need to debug — copy the embedded string back into the formatter and click Format. The pretty version returns (minus comments).

This way you keep readability for development and compactness for runtime.

Try It Free — No Signup Required

Runs 100% in your browser. No data is collected, stored, or sent anywhere.

Open Free SQL Formatter

Frequently Asked Questions

Does minification preserve string literals correctly?

Yes. Anything inside single quotes is preserved exactly, including internal spaces and special characters. The minifier only collapses whitespace OUTSIDE of string literals.

Will the minified query run identically to the original?

Yes — semantically identical. The only difference is stripped comments. If your original had commented-out code that was significant (e.g., a SET statement that you commented out for testing), it will be removed in the minified output.

Can I un-minify the SQL later?

Yes. Paste the minified one-line query back into the formatter and click Format. You will get a pretty version back. The round trip is lossless except for the stripped comments.

Does minify work on stored procedures?

Technically yes, but practically no. Procedures with BEGIN/END blocks become a single unreadable line. Keep procedures in pretty format.

How small does the SQL get after minify?

Typically 30-50% of the original size. A 1KB pretty query might minify to 400-700 bytes. The exact ratio depends on how much whitespace and how many comments were in the original.

Launch Your Own Clothing Brand — No Inventory, No Risk