SQL Minifier

This SQL minifier strips comments and squeezes a query onto one line, without touching anything inside strings, quoted names, or PostgreSQL dollar quotes.

Minified in your browser. Your SQL is not uploaded.
Dialect

Minified SQL

Minified SQL shows up here

Paste a query on the left, upload a .sql file, or try the sample to see how much you can save.

0%

Comments removed
Lines
Statements
Hints kept
Minifies as you type. Strings, quoted names, dollar quotes and /*+ hints */ stay as they are.

How to use the SQL Minifier

  1. Pick the dialect of your database. It decides which characters start a comment or a quoted name.
  2. Paste the SQL, upload a .sql file, or load one from a URL.
  3. The single-line result appears below as you type, with the size before and after.
  4. Copy it, or download it as minified.sql.

How it works

The minifier reads the SQL token by token instead of using search and replace, so a -- inside 'a -- b' stays part of the string. These are copied exactly as written:

  • single-quoted strings, with '' as an escaped quote, plus \' in MySQL;
  • quoted names in double quotes, backticks (MySQL, SQLite), or square brackets (SQL Server, SQLite);
  • PostgreSQL dollar-quoted bodies such as $fn$ ... $fn$, common in function definitions;
  • MySQL version comments /*! ... */ and optimizer hints /*+ ... */, because the database reads them.

Everything else is joined with single spaces. Spaces before a comma, semicolon, or closing bracket, and after an opening bracket, are dropped too.

Examples

This query:

SELECT id,
  name -- the name
FROM users
WHERE id = 1;

becomes SELECT id, name FROM users WHERE id = 1;

In MySQL, SELECT 5--3 stays as it is, because MySQL needs a space after -- for a comment and reads this as 5 minus minus 3. Under Standard SQL the same text is SELECT 5 followed by a comment.

With Keep comments ticked, -- note turns into /* note */, which survives on one line.

Limitations

  • The SQL is not checked. A typo stays a typo; use the SQL Validator for that.
  • Keywords keep their case and nothing is shortened or renamed. Only comments and whitespace are removed.
  • An unclosed string or comment stops the minifier with its line and column, so nothing after it is silently lost.
  • Inputs up to 5 MB.

Frequently asked questions

Does minifying SQL make queries faster?

Not noticeably for the database, which parses whitespace quickly. It helps when SQL is embedded in code, logs, or URLs, where one short line is easier to handle.

Will it break my stored procedures?

Bodies in dollar quotes and strings are left alone. Check MySQL procedures that use DELIMITER, since the delimiter line needs its own line to work in the mysql client.

Can I undo the minification?

Run the result through the SQL Formatter to get indentation back. Removed comments cannot be restored.

Often used together with SQL Minifier.

  • SQL Formatter

    Formats SQL queries for MySQL, PostgreSQL, SQLite, SQL Server, and more.

  • SQL Validator

    Syntax check of SQL statements per dialect, with the error position and expected tokens.

  • SQL Query Generator

    Generates CREATE, INSERT, SELECT, UPDATE, and DELETE statements for four databases.