SQL to One Line — Minify SQL Queries in Your Browser
Table of Contents
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:
- Embedded in Python/Java/Go/JavaScript strings — when a SQL query is a string literal in your code, multi-line strings work but one-line is cleaner for short queries.
- Configuration files — Kubernetes ConfigMaps, Helm values.yaml, Terraform locals — single-line SQL avoids YAML's indentation rules getting confused.
- Shell scripts — psql -c "SELECT ..." or mysql -e "SELECT ..." commands need the query on one line.
- Logging — application logs that print the SQL being executed are easier to grep when each query is on one line.
- JIRA tickets and Slack messages — multi-line SQL gets mangled by the formatter in many ticketing systems. One line stays readable.
- Database job definitions — some scheduling tools (older cron-style) want the entire command on one line.
- Cassandra cqlsh and Snowflake SnowSQL execute commands — taking the query as a CLI argument works best on one line.
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:
- Removes single-line comments — anything after -- on a line is stripped.
- Removes block comments — /* ... */ blocks are removed entirely.
- Collapses all whitespace — newlines, tabs, and multiple spaces become single spaces.
- Trims around punctuation — spaces around commas, semicolons, and parentheses get normalized.
- 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
- Paste your multi-line SQL into the formatter input.
- Click Minify — not Format. Minify is the second button in the row.
- Output appears as a single line — with syntax highlighting if your browser supports it.
- Click Copy — the one-line SQL is on your clipboard.
- 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 ShippingEmbedding 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:
- Production migration files — these get checked into git and reviewed by humans. Pretty format helps reviewers spot bugs.
- Stored procedures — once minified, they are unreadable. Keep procs in pretty format and let your CI/CD pipeline handle deployment.
- dbt models — dbt is designed around readable SQL. Minifying defeats the purpose.
- Long queries with complex CTEs — the cognitive cost of parsing a 500-character one-line query exceeds the cost of multi-line scrolling.
- Anything with comments you want to keep — minification strips comments. If your comments document why a query exists, lose them at your peril.
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:
- Format your query pretty — work on it, debug it, get it right.
- Save the pretty version — to a .sql file in your project.
- Click Minify — get the one-line version.
- Embed the minified version in your code — Python string, config file, etc.
- 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 FormatterFrequently 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.

