Skip to content

↑↓ navigate ↵ open Esc close Search runs locally

SQL Formatter & Beautifier (with Laravel Bindings)

Format and minify SQL for MySQL, PostgreSQL, SQL Server, SQLite and many more dialects. Paste a query with ? placeholders and its bindings to get SQL you can run straight away.

  • Runs in your browser
  • Nothing is uploaded
  • Free, no sign-up
0 B
Result
Fill bindings Laravel query log, PDO, toSql()

Replaces ?, $1 and :name placeholders with these values, quoted for the chosen dialect. Paste a JSON array, a PHP array or the output of dd($query->getBindings()).

Runs in your browser. Nothing is uploaded. Tip: open Fill bindings to turn ? placeholders into real values

How to use the SQL Formatter tool#

  1. Paste your SQL into the left box, or drop a .sql file onto it. Several statements separated by ; are fine.
  2. Pick the Dialect your database uses (MySQL is the default), then choose the keyword case and indentation.
  3. Choose Format for readable SQL or Minify for a single line without comments. Copy the result or download it as a .sql file.
  4. Debugging a Laravel or PDO query with ? placeholders? Open Fill bindings and paste the values as a JSON array, a PHP array or dd() output. Each placeholder is replaced with the quoted value.

Examples#

Format a one-line query

Input
select u.id, u.name, count(o.id) as orders from users u left join orders o on o.user_id = u.id where u.active = 1 group by u.id, u.name order by orders desc limit 10
Output
SELECT
  u.id,
  u.name,
  COUNT(o.id) AS orders
FROM
  users u
  LEFT JOIN orders o ON o.user_id = u.id
WHERE
  u.active = 1
GROUP BY
  u.id,
  u.name
ORDER BY
  orders DESC
LIMIT
  10

MySQL, keywords in uppercase, 2-space indentation. Each clause starts on its own line.

Fill Laravel bindings

Input
select * from users where email = ? and created_at > ? and deleted_at is null
Output
SELECT
  *
FROM
  users
WHERE
  email = '[email protected]'
  AND created_at > '2026-01-01 00:00:00'
  AND deleted_at is null

Bindings: ["[email protected]", "2026-01-01 00:00:00"]. Each ? is replaced in order, and strings are quoted, so the query is ready to run in a database client.

Minify and remove comments

Input
-- Active customers with their last order
SELECT c.id,
       c.name,  -- display name
       MAX(o.created_at) AS last_order
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE c.note <> 'VIP  (manual)'
GROUP BY c.id, c.name;
Output
SELECT c.id,c.name,MAX(o.created_at) AS last_order FROM customers c JOIN orders o ON o.customer_id = c.id WHERE c.note <> 'VIP  (manual)' GROUP BY c.id,c.name;

Comments and extra whitespace are removed, but the string 'VIP (manual)' keeps its two spaces.

Choose the right dialect

Input
select [order id], [total] from [orders]
Output
✗ Line 1, column 8: Unexpected "[order id]"

Square brackets around names are SQL Server syntax, so Standard SQL cannot read them. Switch the dialect to SQL Server and the same query formats.

What does a SQL formatter do?#

A SQL formatter rewrites a query with consistent line breaks, indentation and keyword case, so every clause (SELECT, FROM, JOIN, WHERE, GROUP BY) starts on its own line and the structure is clear at a glance. It changes only whitespace and the case of keywords: table names, column names, strings and numbers stay exactly as they were, so the query does the same thing before and after. This tool uses the open-source sql-formatter library, which knows the syntax of many SQL dialects.

Format or minify?#

Format when a person will read the query: a code review, a slow query copied from a log, or SQL in documentation. Minify when a program will read it: one line is easier to put in a log search, a JSON config, a shell command or an environment variable. Minify removes comments, except MySQL's /*! ... */ version comments and optimizer hints such as /*+ INDEX(...) */, because those change how the query runs.

SQL dialects compared#

Every database adds its own syntax to standard SQL. Picking the right dialect lets the formatter read quoted names, placeholders and special strings correctly.

DialectQuoted namesPlaceholdersComments
MySQL, MariaDB`order`?-- (with a space), #, /* */
PostgreSQL"order"$1, $2--, /* */
SQL Server (T-SQL)[order]@name--, /* */
SQLite"order"?, :name--, /* */
Oracle"ORDER":name--, /* */

If you see an error such as Unexpected "[order]", the SQL is usually fine and the dialect is wrong.

See the real query behind the Laravel query builder#

Laravel sends queries as prepared statements: the SQL contains ? placeholders and the values are sent separately as bindings. That is what protects your app from SQL injection, but it also means toSql() and the query log show placeholders instead of values. These are the ways to get the full query:

php
// Laravel 10.15+: the SQL with the values already filled inUser::where('email', $email)->toRawSql(); // Any version: the SQL and the bindings separately$query = User::where('email', $email);$query->toSql();        // select * from `users` where `email` = ?$query->getBindings();  // ['[email protected]'] // Every query that ranDB::enableQueryLog();// ... your code ...dd(DB::getQueryLog());  // query, bindings and time of each query

Paste the SQL into this tool and the bindings into Fill bindings. The values are quoted for the dialect you picked: strings in single quotes with inner quotes doubled, null as NULL, and in MySQL true and false as 1 and 0.

Use filled queries for debugging only. In application code, always pass values as bindings and never build SQL by joining strings together.

SQL formatting conventions#

  • Keywords in uppercase and names in lowercase, as in SELECT name FROM users. It is the most common style and makes keywords easy to tell apart from columns.
  • One clause per line, with columns and conditions indented under it.
  • One column per line in long SELECT lists, so a diff shows exactly which column changed.
  • AND and OR at the start of the line, so a condition can be commented out on its own.
  • Short table aliases (u for users, o for orders) and AS for column aliases.

Format SQL in code#

JavaScript#

javascript
import { format } from 'sql-formatter';   // npm install sql-formatter format(sql, { language: 'mysql', keywordCase: 'upper' });

PHP#

php
// composer require doctrine/sql-formatteruse Doctrine\SqlFormatter\NullHighlighter;use Doctrine\SqlFormatter\SqlFormatter; $formatter = new SqlFormatter(new NullHighlighter());echo $formatter->format($sql);     // formattedecho $formatter->compress($sql);   // one line, comments removed

Python#

python
import sqlparse   # pip install sqlparse print(sqlparse.format(sql, reindent=True, keyword_case='upper'))

Command line#

bash
npx sql-formatter --language mysql query.sql

Common errors and fixes#

  • Parse error: Unexpected "[col]" or a similar message

    The dialect does not match the SQL. Square brackets are SQL Server syntax, $1 is PostgreSQL and backticks are MySQL. Pick the right dialect and format again.

  • A stored procedure or a DELIMITER $$ block does not format

    The formatter does not support stored procedure bodies or custom delimiters. Format the statements inside the procedure on their own.

  • Some ? placeholders stayed after filling bindings

    There are fewer values than placeholders. Laravel keeps bindings in the order the placeholders appear, so copy the full getBindings() array of the same query.

  • Dates from dd() cannot be read as bindings

    Laravel dumps dates as Carbon objects. Paste the dates as strings, such as "2026-01-01 00:00:00", or use toRawSql() in Laravel 10.15 and later.

  • A MySQL comment like --note is not treated as a comment

    In MySQL, -- starts a comment only when a space or line break follows it. Write -- note, or use # or /* */.

Frequently asked questions#

Does formatting change what my query does?

No. Format changes only whitespace, line breaks and the case of keywords. Table names, column names, strings and numbers stay exactly as written. Minify also removes comments, except MySQL version comments and optimizer hints.

Does this tool validate SQL?

Partly. The formatter reads the SQL and reports syntax it cannot understand with the line and column, but it does not connect to a database, so it cannot check that tables and columns exist. Some invalid queries can still format.

Which dialect should I choose?

The one your database uses. Laravel and most PHP apps use MySQL or MariaDB. Pick PostgreSQL for $1 placeholders and SQL Server for names in square brackets. Standard SQL works for simple queries in any database.

How do I see a Laravel query with its values filled in?

Use toRawSql() in Laravel 10.15 and later. In older versions, copy toSql() and getBindings(), paste the SQL here and the bindings into Fill bindings.

How large a SQL file can I format?

Files up to 10 MB. Inputs over 300 KB are formatted when you press Format instead of on every keystroke, so typing stays fast.

Is my SQL uploaded?

No. Formatting, minifying and filling bindings all run in your browser, so it is safe to paste queries from production logs.

Your next tool is one search away.

Free, private developer tools that run in your browser. No signup, nothing uploaded, no waiting.

Browse all tools

Tip: press Ctrl D to bookmark dev247.