Skip to main content
ilovecalcs logoilovecalcs.

Dev Tools · Client-side

SQL Formatter & Minifier

Paste any SQL query to instantly beautify it with proper indentation and keyword casing, or minify it to a single compact line. Supports Standard SQL, MySQL, PostgreSQL, PL/SQL, T-SQL, SQLite, and Snowflake -- all processed locally in your browser.

SQL style guideClient-side

SQL Input

Formatted SQL

SQL formatting guide

Why SQL formatting matters and how to use this tool

SQL queries are often written quickly in development environments, ORMs, or log files, resulting in dense one-liners or inconsistently indented blocks that are difficult to read and review. A consistent formatting style reduces cognitive load, makes queries easier to audit for correctness, and improves collaboration between developers and data analysts. This tool applies a consistent, configurable style to any SQL query in one click.

Format vs. Minify

The two core operations serve opposite purposes:

  • Format (Beautify): Expands the query onto multiple lines with consistent indentation and keyword casing. Use this for code review, documentation, team collaboration, or simply understanding an unfamiliar query.
  • Minify: Collapses the entire query into a single compact line by removing all extra whitespace and comments. Use this to embed SQL in strings, reduce the size of log output, or pass queries as URL parameters.

SQL dialect differences

While most SQL dialects share a common core, they differ in syntax for advanced features. Choosing the correct dialect tells the formatter which syntax rules to apply:

  • Standard SQL: The ANSI/ISO standard. Safe for most general-purpose queries and the best default if you are unsure.
  • MySQL / MariaDB: Adds syntax for things like backtick-quoted identifiers (`table_name`), theREPLACE INTO statement, and MySQL-specific functions.
  • PostgreSQL: Supports double-dollar quoting, common table expressions (CTEs), lateral joins, and window functions with PostgreSQL-specific syntax.
  • PL/SQL (Oracle): The procedural extension for Oracle Database. Supports blocks with DECLARE,BEGIN, and EXCEPTION sections.
  • T-SQL (SQL Server): Microsoft's extension of SQL for SQL Server and Azure SQL. Uses TOP instead ofLIMIT, and has specific syntax for stored procedures, variables, and window functions.
  • SQLite: A lightweight, serverless dialect used in mobile apps and embedded systems. Has limited support forALTER TABLE and some advanced types.
  • Snowflake: A cloud data warehouse dialect with support for semi-structured data (VARIANT,OBJECT, ARRAY) and Snowflake-specific clustering and partitioning syntax.

Keyword casing conventions

SQL is case-insensitive for keywords, but consistent casing is a style choice. Three conventions are widely used:

  • UPPERCASE: The traditional style, dating from early SQL standards. Most style guides for database-heavy environments (data warehousing, BI) still recommend uppercase keywords. Easy to visually distinguish keywords from identifiers and values.
  • lowercase: Increasingly common in modern application development and ORM-generated SQL. Feels less shouty and integrates more naturally with surrounding application code.
  • Preserve: Leaves keywords exactly as you typed them. Useful if you want to maintain a mixed style or are processing queries from a system that uses a specific casing.

Indentation styles

The indentation style controls how nested clauses and sub-queries are visually offset:

  • 2 spaces: Compact but readable. Common in JavaScript-adjacent environments and preferred by most modern code style guides.
  • 4 spaces: More spacious and traditional. Common in Python-adjacent environments and older SQL codebases.
  • Tabs: Uses a tab character for each indentation level. The rendered width depends on the editor's tab size setting. Preferred in some language-specific style guides.

How the formatter works

This tool uses the open-source sql-formatter library (v15), which parses SQL into an abstract syntax tree (AST) and then serializes it back according to the chosen formatting rules. It handles comments, string literals, and complex nested sub-queries correctly. All processing runs in your browser -- no SQL is sent to any server.

The minifier works by stripping single-line comments (starting with--), block comments (/* ... */), and then collapsing all consecutive whitespace characters into a single space, producing the shortest valid representation of the query.

Disclaimer

SQL formatting is syntax-only and does not validate the semantic correctness of your query. A formatted query may still contain logical errors. The formatter works best with complete, syntactically valid SQL. Partial statements or non-standard extensions may produce unexpected output. Always test formatted queries in a safe environment before using them in production.