Skip to main content

    SQL Formatter & Beautifier

    Format and beautify SQL queries with proper indentation, keyword uppercasing, and line breaks.

    Free to use. Runs in your browser.

    Paste a SQL query and click Format to apply consistent indentation, uppercase keywords, and line breaks. Supports PostgreSQL, MySQL, SQL Server, and Oracle syntax.

    Why Formatted SQL Matters

    SQL is one of the most forgiving languages when it comes to whitespace. A query that works perfectly as one enormous line also works formatted across 20 lines. But the person reading that query, you, six months from now, will strongly prefer the formatted version.

    Formatted SQL makes the structure visible. You can see the SELECT clause at a glance, spot which tables are JOINed, understand the WHERE conditions, and verify the GROUP BY columns. With everything on one line, you're scrolling horizontally and hoping for the best.

    This formatter takes messy SQL (pasted from logs, generated by ORMs, or squished into a single line) and produces clean, indented, readable SQL. It handles SELECT, INSERT, UPDATE, DELETE, CREATE, and complex subqueries. Paste, format, understand.

    SQL Formatting Conventions

    ElementConventionExample
    KeywordsUPPERCASESELECT, FROM, WHERE, JOIN
    Table/column nameslowercase or snake_caseusers, first_name, order_id
    Major clausesNew line eachSELECT ... FROM ... WHERE ... ORDER BY
    Column listsOne per line (for readability)id,\n name,\n email
    JOINsNew line, indented ON clauseJOIN orders\n ON users.id = orders.user_id
    SubqueriesIndented, parenthesised on own linesWHERE id IN (\n SELECT ...\n)

    What this means for you: There's no one "correct" SQL formatting standard, but the conventions above are the most widely used. The key is consistency within your project. Pick a style and apply it everywhere.

    SQL Writing Best Practices

    Never use SELECT *

    SELECT * fetches every column, including ones you don't need. It slows queries, breaks when columns are added, and makes code harder to understand. Always list the specific columns you need.

    Use table aliases consistently

    In multi-table queries, alias every table (users u, orders o) and qualify every column (u.name, o.total). This prevents ambiguous column errors and makes it clear where each column comes from.

    Comment complex logic

    A complex WHERE clause or subquery deserves a comment explaining the business logic. SQL comments use -- for single-line or /* */ for multi-line. Future you will appreciate it.

    Use parameterised queries

    Never concatenate user input into SQL strings. Use parameterised queries (prepared statements) to prevent SQL injection. Every ORM and database driver supports them natively.

    SQL Dialect Differences

    FeatureMySQLPostgreSQLSQL Server
    Limit rowsLIMIT 10LIMIT 10TOP 10
    String concatCONCAT(a, b)a || ba + b
    Auto-incrementAUTO_INCREMENTSERIALIDENTITY(1,1)
    UpsertON DUPLICATE KEYON CONFLICTMERGE
    Identifier quotes`backticks`"double quotes"[brackets]

    This formatter handles standard SQL syntax. Dialect-specific keywords format correctly, but always test formatted SQL against your actual database before running in production.

    Related Tools

    How to use this tool

    1

    Paste your SQL query into the input field

    2

    Click Format SQL to beautify with proper indentation

    3

    Copy the formatted output for your codebase

    Common uses

    • Formatting ORM-generated SQL for debugging
    • Cleaning up single-line SQL for code reviews
    • Beautifying log-extracted queries for analysis
    • Standardising SQL style across a development team

    Share this tool

    Frequently Asked Questions

    What does this SQL formatter do?
    It takes messy SQL (single-line, no indentation, inconsistent casing) and reformats it with proper indentation, UPPERCASE keywords, and line breaks at major clauses. The result is clean, readable SQL that's easy to review and debug.
    Does this support all SQL dialects?
    It handles standard SQL syntax that's common across MySQL, PostgreSQL, SQLite, and SQL Server. Dialect-specific extensions (window functions, CTEs, RETURNING clauses) will format but keyword highlighting may vary.
    Does it validate my SQL?
    No, this is a formatter, not a validator. It doesn't check syntax, table names, or column references. It reformats whatever you give it. Use your database client or a linter for validation.
    Is my SQL sent to a server?
    No. All formatting happens locally in your browser using JavaScript string manipulation. Your queries never leave your device, important if you're working with production SQL.
    Why should I uppercase SQL keywords?
    It's a widely-adopted convention that makes SQL structure immediately visible. SELECT, FROM, WHERE, JOIN stand out from table and column names. Most SQL style guides recommend uppercase keywords for readability.
    Can it format complex queries with subqueries?
    Yes. Subqueries, JOINs, UNION clauses, and nested conditions all get proper indentation and line breaks. The formatter recognises all major SQL clauses and indents AND/OR conditions within WHERE clauses.
    Does it handle CREATE TABLE and DDL statements?
    Yes. CREATE, ALTER, DROP, and INDEX statements are supported. Keywords are uppercased and formatted with appropriate line breaks.
    What about Common Table Expressions (CTEs)?
    WITH clauses format correctly. Each CTE gets proper indentation. The AS keyword and subsequent SELECT are formatted on new lines for readability.
    Can I minify SQL instead of formatting it?
    This tool only formats (beautifies) SQL. To minify, you'd strip all extra whitespace and newlines. For SQL, minification is rarely needed, unlike CSS or JavaScript, SQL query size doesn't affect network performance.
    How should I format long SELECT lists?
    Best practice: one column per line. This makes it easy to add, remove, or comment out columns, and produces cleaner diffs in version control. This formatter puts major clauses on new lines.
    Does it handle stored procedures?
    Basic procedure syntax will format, but complex procedural extensions (PL/pgSQL, T-SQL control flow) may not indent perfectly. The formatter focuses on DML (SELECT, INSERT, UPDATE, DELETE) and DDL statements.
    Why are parameterised queries important?
    Never concatenate user input into SQL strings, that's how SQL injection happens. Use parameterised queries (prepared statements) where the database driver handles escaping. Every modern ORM and driver supports them.

    Results are for general informational purposes only and should be checked before use. They are not professional advice. See our Disclaimer and Terms of Service.