Glyph WidgetsGlyph Widgets
Acerca deContactoBlogPrivacidadTérminosApoyar en Ko-fi

© 2026 Glyph Widgets. Todos los derechos reservados.

·

100% Procesamiento del lado del cliente

Volver al Blog

SQL Formatter: Format & Beautify SQL

SQL formatter: beautify queries for MySQL, PostgreSQL, SQL Server, SQLite, and more. Keyword casing, indentation, and minification.

Glyph Widgets
27 de febrero de 2026
10 min read
sql formattersql beautifierformat sql onlinesql pretty printmysql formatter

What Is SQL Formatter?

SQL Formatter is a browser-based tool that transforms unformatted, compressed, or inconsistently styled SQL queries into clean, readable code with proper indentation and consistent keyword casing. Developers and data analysts encounter poorly formatted SQL constantly — queries written inline in application code, output from ORM debug logs, SQL copied from documentation, or queries that evolved through incremental edits and lost their structure.

This tool takes that raw SQL and applies dialect-aware formatting using the sql-formatter library, which understands the syntax rules of Standard SQL, MySQL, MariaDB, PostgreSQL, PL/SQL (Oracle), T-SQL (SQL Server), and SQLite. You can choose your target dialect, set indentation width, toggle keyword uppercase, and minify the result. All processing runs locally in your browser with no server involvement, no sign-up, and no cost.

Key Features

  • Format SQL with proper indentation — Uses the sql-formatter library with configurable tabWidth and linesBetweenQueries: 2 spacing between multiple statements in a single paste.
  • Support for multiple SQL dialects — Choose from Standard SQL, MySQL, MariaDB, PostgreSQL, PL/SQL (Oracle), T-SQL (SQL Server), and SQLite. Dialect selection affects how the formatter handles dialect-specific syntax and reserved words.
  • Customizable indentation settings — Select 2-space or 4-space indentation from the action bar.
  • Uppercase keyword toggle — A toggle button switches between keywordCase: 'upper' and keywordCase: 'preserve'. When uppercase is active (the default), all SQL keywords are standardized to uppercase (SELECT, FROM, WHERE, JOIN, etc.). When disabled, keyword casing is left as-is.
  • Minify SQL — Removes extra whitespace by formatting with tabWidth: 0 and collapsing the result with .replace(/\s+/g, ' '). The minified output is a single line with single spaces between tokens.
  • Copy formatted output to clipboard — Clipboard API with textarea fallback.
  • Download as .sql file — Saves output as formatted.sql with text/sql MIME type.
  • Keyboard shortcuts — Ctrl+Enter (Cmd+Enter) formats; Ctrl+Shift+M (Cmd+Shift+M) minifies.

How to Use SQL Formatter

Step 1: Paste Your SQL

Open the tool at /developer/code/sql-formatter. Paste your SQL query or queries into the Input panel on the left. Multiple statements separated by semicolons are supported — the formatter places two blank lines between each statement in the output.

Step 2: Select Dialect and Options

In the action bar below the panels:

  1. Choose a dialect from the dropdown: Standard SQL, MySQL, MariaDB, PostgreSQL, PL/SQL (Oracle), T-SQL (SQL Server), or SQLite. Match this to the database engine your query will run against for the most accurate formatting.
  2. Choose indentation: 2 or 4 spaces.
  3. Toggle Uppercase: The Uppercase button is active by default (shown with a filled/primary style). When active, all SQL keywords are normalized to uppercase. Click to toggle it off and preserve your original casing.

Step 3: Click Format

Click Format or press Ctrl+Enter / Cmd+Enter. The formatted SQL appears in the read-only Output panel. The tool shows the dialect in the history entry (e.g., "Formatted POSTGRESQL (2.4 KB)").

Example input — inline query from application debug output:

select u.id,u.email,u.created_at,count(o.id) as order_count,sum(o.total_amount) as total_spent from users u left join orders o on u.id=o.user_id where u.active=true and u.created_at>='2024-01-01' group by u.id,u.email,u.created_at having count(o.id)>0 order by total_spent desc limit 25

Output with PostgreSQL dialect, 2-space indent, uppercase on:

SELECT
  u.id,
  u.email,
  u.created_at,
  COUNT(o.id) AS order_count,
  SUM(o.total_amount) AS total_spent
FROM
  users u
  LEFT JOIN orders o ON u.id = o.user_id
WHERE
  u.active = TRUE
  AND u.created_at >= '2024-01-01'
GROUP BY
  u.id,
  u.email,
  u.created_at
HAVING
  COUNT(o.id) > 0
ORDER BY
  total_spent DESC
LIMIT
  25

Step 4: Minify (Optional)

Click Minify or press Ctrl+Shift+M / Cmd+Shift+M to produce a single-line SQL string. The uppercase and dialect settings still apply during minification. Minified SQL is useful for embedding queries in environment variables, configuration strings, or comparison diffs where line changes would be noisy.

Minified output of the same query:

SELECT u.id, u.email, u.created_at, COUNT(o.id) AS order_count, SUM(o.total_amount) AS total_spent FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE u.active = TRUE AND u.created_at >= '2024-01-01' GROUP BY u.id, u.email, u.created_at HAVING COUNT(o.id) > 0 ORDER BY total_spent DESC LIMIT 25

Step 5: Copy or Download

Click Copy to place the output on your clipboard. Click Download to save formatted.sql. Both buttons are disabled until output exists in the right panel.

Practical Examples

Cleaning Up an ORM Debug Query

Django's or Rails' ORM debug output logs queries on a single line with mixed casing. A typical Django query log entry:

select "products"."id", "products"."name", "products"."price", "products"."stock_count" from "products" where "products"."category_id" = 12 and "products"."active" = true order by "products"."name" asc

Select PostgreSQL dialect (Django uses PostgreSQL quoting with double quotes), enable uppercase, and click Format. The output shows proper indentation with uppercase keywords while preserving the double-quoted identifiers.

Reviewing a Stored Procedure

A T-SQL stored procedure was checked into version control as a single unformatted block. Select T-SQL (SQL Server) dialect and click Format to get an indented, readable version with consistent keyword casing. The formatted version is easier to review in a pull request diff and can be committed back to replace the original.

Embedding a Query in Configuration

Your application stores a report query in a YAML configuration file as a single-line string. Build and test the query with proper formatting in the SQL Formatter, then click Minify to produce the compact version for embedding. Keeping the formatted version in a comment alongside the minified one makes the configuration file maintainable.

# Formatted version (for readability):
# SELECT product_id, SUM(quantity) AS units_sold
# FROM order_items
# WHERE order_date >= :start_date
# GROUP BY product_id
# ORDER BY units_sold DESC

report_query: "SELECT product_id, SUM(quantity) AS units_sold FROM order_items WHERE order_date >= :start_date GROUP BY product_id ORDER BY units_sold DESC"

Tips and Best Practices

Match the dialect to your database. Although most SQL formatting is dialect-agnostic, the sql-formatter library handles dialect-specific syntax (such as :: cast syntax in PostgreSQL, TOP in T-SQL, and LIMIT/OFFSET placement differences) more accurately when the correct dialect is selected.

Use uppercase for code review readability. The SQL convention of uppercase keywords is widely recognized and makes the structural components (SELECT, FROM, WHERE, JOIN) visually distinct from identifiers and literals. Enable the uppercase toggle before formatting code that will be reviewed or committed.

Format before diffing. When comparing two versions of a query, format both with the same settings first. Diffs on unformatted SQL produce noisy output because a single structural change might affect many lines. Formatted SQL diffs show only meaningful changes.

Multiple statements work. You can paste a migration file or a script with multiple CREATE TABLE, INSERT, and ALTER TABLE statements separated by semicolons. The formatter produces two blank lines between each statement for visual separation.

Save presets for team conventions. Glyph Widgets supporters can save the dialect, indentation, and uppercase settings as a named preset. Create one preset per project (e.g., "MySQL 2-space uppercase") to apply your team's conventions in one click when switching between projects.

Common Issues and Troubleshooting

Error message after clicking Format — The sql-formatter library throws an error on syntax it cannot recognize. Common causes are: proprietary extensions not supported by the selected dialect, procedural code (PL/pgSQL function bodies, T-SQL BEGIN/END blocks), or SQL mixed with application-layer template syntax like {{ variable }} or #{variable}. Remove or comment out unsupported portions before formatting.

Keywords not being uppercased — Confirm the Uppercase button is in its active state (filled, primary style). If the button is in outline style, casing is set to preserve. Click it once to enable uppercase mode.

Wrong dialect causes unexpected line breaks — The sql-formatter library's line-break rules vary by dialect. If a query formats oddly, try Standard SQL as the dialect. For example, PL/SQL and T-SQL dialects may handle subquery formatting differently than Standard SQL.

Minified output still has multiple spaces in places — The minification approach collapses all whitespace sequences to single spaces. However, if the formatter itself inserts intentional newlines within string literals or comments, a single space replaces them. Quoted string content is not affected — the spaces inside single-quoted strings are preserved.

"Please enter SQL to format" error — The input is empty. Paste a SQL query into the left panel before clicking Format.

Downloaded file is named formatted.sql every time — This is the fixed filename. Rename the file after downloading to match your project's naming convention.

Privacy and Security

SQL Formatter processes all SQL text locally in your browser using the sql-formatter library. No query text — including table names, column names, data values, credentials in connection strings, or any other content — is sent to a server. The tool works offline once the page has loaded. This makes it safe to use with proprietary schema names, internal business logic, or queries containing sample data.

Frequently Asked Questions

Is SQL Formatter free? Yes, completely free. All formatting, minification, and dialect options are available without a fee or account. Glyph Widgets supporter features like saved presets and session history are available to Ko-fi supporters, but the core SQL formatting is unrestricted.

Does it work offline? Yes. Once the page loads, all formatting runs locally in your browser using JavaScript. You can disconnect from the internet and continue formatting queries without interruption.

Is my SQL safe to paste here? Yes. Your SQL is never transmitted to any server. It is processed entirely in browser memory and is discarded when you clear the tool or close the tab. You can safely paste queries containing internal table names, business logic, or sample data values.

Which SQL dialects are supported? The tool supports Standard SQL, MySQL, MariaDB, PostgreSQL, PL/SQL (Oracle), T-SQL (SQL Server), and SQLite. These map directly to the language parameter of the sql-formatter library.

Does the tool validate SQL syntax? The sql-formatter library performs enough parsing to format SQL and will throw an error on certain syntax it cannot handle, but it is not a SQL validator in the strict sense. It will format syntactically questionable SQL without always reporting an error. Use your database engine's EXPLAIN or query planner for true syntax validation.

Can I format multiple queries at once? Yes. Paste multiple statements separated by semicolons. The formatter places two blank lines between each formatted statement, making the boundaries between queries clear.

What does the Uppercase toggle do exactly? When enabled, the formatter sets keywordCase: 'upper' in the sql-formatter options, which converts all SQL reserved words to uppercase — SELECT, FROM, WHERE, JOIN, ON, GROUP BY, ORDER BY, LIMIT, function names like COUNT, SUM, MAX, etc. When disabled, keywordCase: 'preserve' leaves whatever casing was in your input unchanged.

Does the formatter handle CTEs (Common Table Expressions)? Yes. CTEs (WITH ... AS (...)) are formatted correctly with proper indentation for the CTE definition and the main query that follows. This applies across all supported dialects.

What is the keyboard shortcut to format? Ctrl+Enter on Windows/Linux or Cmd+Enter on macOS. To minify, use Ctrl+Shift+M on Windows/Linux or Cmd+Shift+M on macOS.

Can I format DDL statements like CREATE TABLE? Yes. The sql-formatter library handles DDL (CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE INDEX, etc.) as well as DML (SELECT, INSERT, UPDATE, DELETE) and DCL (GRANT, REVOKE). Mixed scripts with both DDL and DML format correctly when separated by semicolons.

Related Tools

JSON Formatter — Format and validate JSON data with schema validation, schema generation, and an interactive tree viewer.

XML Formatter — Format and validate XML documents with configurable indentation and minification.

YAML Formatter — Format YAML configuration files and convert them to JSON.

Try SQL Formatter now: SQL Formatter

Última actualización: 27 de febrero de 2026

Seguir Leyendo

Más ArtículosProbar SQL Formatter