MySQL Formatter
Format and beautify MySQL and MariaDB queries: `backtick` names, LIMIT, ON DUPLICATE KEY UPDATE and # comments — with keyword case and indentation options.
How to use this tool
Steps for using the SQL Formatter.
- Paste your MySQL query into Input SQL — MySQL is already selected under Dialect, so its quoting, parameters and operators are understood.
- Pick Keyword case (UPPERCASE, lowercase or Preserve) and Indentation, and open Formatting options for indent style, AND / OR placement and line width.
- Choose an Action: Format to beautify, Minify to put it on one line, Validate to check the syntax, or Explain for a plain-English summary.
- Copy or download the result. Formatting only changes whitespace and keyword case — the query does the same thing.
Example
Backtick names, GROUP BY and LIMIT
select `u`.`id`, `u`.`email`, count(`o`.`id`) as orders from `users` u left join `orders` o on o.user_id = u.id group by u.id having orders > 2 order by orders desc limit 20SELECT
`u`.`id`,
`u`.`email`,
count(`o`.`id`) AS orders
FROM
`users` u
LEFT JOIN `orders` o ON o.user_id = u.id
GROUP BY
u.id
HAVING
orders > 2
ORDER BY
orders DESC
LIMIT
20Common use cases
- Reading queries from the slow query log or an ORM's SQL output.
- Cleaning up migrations and stored queries before code review.
- Formatting queries for Stack Overflow questions and documentation.
- Minifying a formatted query back to one line for a config file.
Formatting MySQL and MariaDB queries
MySQL quotes identifiers with backticks — ` users — and also accepts #` as a line comment. With the MySQL dialect selected, the formatter understands both, so names and comments aren't mistaken for something else.
MySQL-specific clauses such as LIMIT, ON DUPLICATE KEY UPDATE and HAVING on aliases keep their meaning; the formatter only moves them onto their own lines and indents their contents, as the upsert example shows.
Queries copied from an ORM's debug log, the slow query log or EXPLAIN output are usually one long line. Formatting them makes joins and conditions easy to check before you add an index or change a query.
MariaDB is close enough to MySQL that this page works for it too (the full formatter also has a dedicated MariaDB dialect). Other databases: SQL Server, PostgreSQL, Oracle.
Limitations
- The formatter doesn't know your database schema — unknown tables or columns aren't detected.
- Stored programs that change the statement delimiter (DELIMITER $$) are best formatted one statement at a time.
- Comments are kept, but their position may change slightly around reformatted lines.
Frequently asked questions
How do I format a MySQL query in MySQL Workbench?
Workbench has a Beautify Query command under Edit → Format. To format without Workbench — or with consistent keyword case and indentation — paste the query here.
Are backticks kept?
Yes. Backtick-quoted names are left exactly as written; only layout and keyword case change.
Does this work for MariaDB?
Yes — MariaDB's syntax is almost identical. The SQL Formatter also offers a dedicated MariaDB dialect in the Dialect list.
Is my query sent to a server?
No. The MySQL formatter runs entirely in your browser.