SQL Minifier

Compress SQL queries by stripping excess whitespace and optional comments.

SQL Minifier

Compress SQL by removing extra whitespace and optional comments.

Working…

Input

Max length: 5000 characters.

Result

Paste your SQL on the left, choose options, then click Minify. Your compact SQL will appear here with size stats and quick actions.

About SQL Minifier

SQL Minifier for Compact, Shareable SQL

SQL is easiest to understand when it is carefully formatted: keywords aligned, JOIN conditions placed on their own lines, and nested expressions indented like code. That readability is great for development, but it becomes inconvenient when you need to transport a query as a single string. Many systems—API clients, monitoring dashboards, configuration files, or analytics tools—work better with short, compact SQL that doesn’t include extra line breaks or indentation.

A minifier solves that gap by removing whitespace that doesn’t affect execution and optionally stripping comments. The result is the same statement, but smaller and easier to copy, store, and embed. This SQL Minifier focuses on safe transformations: it keeps quoted strings intact, handles common SQL comment styles, and compresses whitespace in a predictable way.

Whether you are shipping a query inside an application, pasting SQL into a ticket, or normalizing statements for testing, compact queries reduce friction. Instead of manually editing formatting, you can paste, minify, and move on.

How It Works

The tool reads your SQL text and processes it character by character. It tracks when it enters and exits quoted regions (single quotes, double quotes, and backticks) and treats those regions as “verbatim.” This is important because many SQL values depend on exact spacing and punctuation inside strings, and some dialects allow quoted identifiers that should not be altered.

Outside of quoted regions, SQL parsing is often whitespace-insensitive. That means sequences of spaces, tabs, and line breaks can be compressed without changing token boundaries. The minifier applies conservative rules that keep a single separator space between tokens and tightens spacing around punctuation where it is safe.

Processing steps

  • 1) Identify safe regions: Detect quoted strings and delimited identifiers so literal content is preserved exactly.
  • 2) Optional comment removal: Strip -- line comments and /* block */ comments only when they appear outside quotes.
  • 3) Whitespace normalization: Collapse repeated spaces/tabs/newlines into a single space, and trim leading/trailing whitespace.
  • 4) Line break handling: If “preserve line breaks” is enabled, the tool keeps \n boundaries but still compresses spaces on each line.
  • 5) Light punctuation tightening: Remove unnecessary spaces around commas, parentheses, and common operators (without rewriting the query).
  • 6) Output: Return a compact query ready for clipboard copy or file download.

Because the minifier’s scope is whitespace and comments, it works well for typical SQL such as SELECT queries, DML statements (INSERT, UPDATE, DELETE), and many DDL scripts. It does not attempt heavy semantic rewriting, reordering, or formatting changes. That keeps results stable, predictable, and easy to validate.

If you are working with procedural SQL (functions, triggers, stored procedures) or scripts that depend on specific client-side delimiter rules, choose the “preserve line breaks” option and test the output in a staging environment. In practice, most everyday queries minify cleanly across PostgreSQL, MySQL/MariaDB, SQL Server, SQLite, Oracle, and cloud data warehouses.

Key Features

Whitespace compression without breaking strings

The minifier collapses long runs of spaces, tabs, and line breaks into a cleaner representation while avoiding changes inside single‑quoted or double‑quoted strings. This helps ensure that values like 'A B' (with two spaces) remain exactly as written. It also preserves escape sequences and repeated whitespace that may be meaningful inside a literal.

By keeping string and identifier boundaries intact, the tool avoids common pitfalls where naïve regex replacements accidentally remove spaces inside quotes or merge tokens together.

Optional comment stripping

Enable comment removal to eliminate both single‑line comments (-- like this) and multi‑line block comments (/* like this */). Removing comments can significantly shrink large scripts copied from an IDE, generated by ORM tooling, or shared in documentation. It can also reduce the risk of shipping internal notes into logs or production artifacts.

If you want to keep comments for maintainability, simply turn the option off and the minifier will focus on whitespace only.

Preserve line breaks when needed

Some workflows prefer multi‑line SQL (for example, when storing queries in a text file, a migration, or a repository). The “preserve line breaks” option keeps newlines while trimming and compressing spaces on each line. You get a smaller script while still retaining line structure that can be helpful for diffs, reviews, and quick scans.

This option is also useful when your destination system expects line boundaries, such as certain import tools or SQL consoles configured for multi-line editing.

Copy and download output

After minifying, you can copy the result to your clipboard with one click or download it as a .sql file. The download option is handy when you are preparing a compact query for deployment scripts, configuration values, CI jobs, or external tooling. Keeping the output as a file can also help you version the minified form alongside the original.

Practical defaults for instant results

The tool loads with a realistic example query so you can see an immediate before/after. Defaults are designed so the first page load shows a working input and produces a valid output on the first run, helping you understand exactly what the minifier changes.

Works for one-liners and full scripts

You can paste anything from a single query to a long export script. The minifier will normalize spacing consistently, and the comment option lets you decide whether your output should be “transport optimized” (no comments) or “review friendly” (comments preserved).

Use Cases

  • Embedding SQL in application code: Reduce a formatted query to a compact string for constants, query builders, or test fixtures, especially when you need a single-line representation.
  • API payloads and configuration: Fit SQL into JSON/YAML values, environment variables, webhooks, or scheduler settings without messy line breaks or indentation.
  • Log and telemetry pipelines: Store smaller query samples in logs to reduce noise, ingestion costs, and storage volume—while still preserving enough detail to troubleshoot.
  • Sharing snippets: Paste a short, single‑line query into chat tools, tickets, and pull requests when space is limited and you want the statement to remain intact.
  • Migration scripts and tooling: Clean up exported SQL scripts so they are lighter and easier to commit, review, and ship between environments.
  • Benchmarking and testing: Normalize whitespace so comparisons focus on semantics rather than formatting differences, reducing false “diffs” in snapshots.
  • Security and redaction workflows: Remove comments that may include internal notes, temporary debugging details, or sensitive context before sharing SQL externally.
  • Documentation and examples: Provide compact examples for READMEs and knowledge bases where long indentation blocks reduce readability.

In most of these scenarios, your database engine will treat multiple whitespace characters the same as a single space. A minified query keeps the same structure but becomes easier to move across systems that are sensitive to formatting. If you ever need to go back to a readable version, you can keep your original formatted SQL alongside the minified output.

Minification can also complement formatting: keep nicely formatted SQL in source control for humans, and generate minified SQL for machines. This is a common pattern in build pipelines where the output is optimized for size and transport.

Optimization Tips

Strip comments for production transport

If your SQL is leaving your IDE and heading into production configuration, a build pipeline, or a monitoring system, remove comments. Comments are valuable for humans, but they increase size and can expose implementation details in logs or error reports. For example, an inline note that mentions table names, business logic, or temporary workarounds might not be appropriate to ship outside your team.

Comment stripping is also helpful when you are generating SQL dynamically and want to ensure your output is as compact as possible.

Preserve line breaks for long scripts

If you’re minifying a multi‑statement script, preserving line breaks can make it easier to inspect while still removing excessive indentation. You’ll get a smaller file without turning everything into a single unreadable line. This is especially useful for scripts you may need to review later, or for files that will be diffed in code review.

Validate after minification when using unusual dialect features

Most SQL is whitespace‑insensitive, but certain vendor‑specific syntaxes and client tools can behave differently around delimiters or procedural blocks. If your script includes complex routines, custom delimiters, or embedded language blocks, run it once in a staging environment after minifying to confirm expected behavior.

If you notice an issue, try leaving comments intact, preserving line breaks, or minifying only the specific statement you intend to transport.

FAQ

In general, no. SQL engines treat runs of whitespace as separators between tokens, so collapsing whitespace typically preserves meaning. The tool avoids touching content inside quoted strings to prevent changing literal values or quoted identifiers.

When enabled, the minifier removes single-line comments that start with -- and block comments surrounded by /* and */. Comment removal is applied only outside of quoted strings, so literal text that contains -- remains safe.

Yes for the shared formatting rules across most dialects (PostgreSQL, MySQL/MariaDB, SQL Server, SQLite, Oracle, and many warehouses). Because the tool focuses on whitespace and comment handling rather than deep parsing, it is broadly compatible.

Yes. Paste a full script containing multiple statements and the tool will process it as text. If you want to keep script readability, enable “preserve line breaks” so each line stays separated while extra whitespace is removed.

Toggle comment removal and line-break preservation to narrow down the change you’re seeing. If your SQL includes unusual delimiter rules or procedural blocks, consider preserving line breaks and testing the result in a staging environment before deploying.

Why Choose This Tool

This SQL Minifier is built for speed and practicality: paste your query, choose the options you need, and get a compact version immediately. It is especially useful when you are moving SQL through systems that prefer short, single-line values—like environment variables, CI scripts, or JSON payloads.

By keeping quoted strings intact and applying conservative whitespace tightening, the tool produces outputs that are easy to copy, store, and share while preserving the intent of your SQL. Keep readable, formatted SQL for humans in your editor or repository, and generate minified SQL for machines when size and portability matter. The combination gives you the best of both worlds: clarity during development and compactness during delivery.