SQL Formatter

SQL Formatter & Beautifier

Format and beautify SQL queries with proper indentation and keyword highlighting.


    

SQL Keywords Reference

Data Query (DQL)

SELECT, FROM, WHERE, JOIN, GROUP BY, HAVING, ORDER BY, LIMIT

Data Manipulation (DML)

INSERT, UPDATE, DELETE, MERGE, UPSERT

Data Definition (DDL)

CREATE, ALTER, DROP, TRUNCATE, RENAME

Joins

INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, CROSS JOIN

Formatting Best Practices

  • Use uppercase for keywords - Makes queries more readable by distinguishing keywords from identifiers
  • Put each clause on a new line - SELECT, FROM, WHERE, etc. should start on separate lines
  • Indent subqueries - Nested queries should be clearly indented
  • Align columns in SELECT - List each column on its own line for complex queries
  • Use meaningful aliases - Table aliases should be short but descriptive
Horizontal Banner (Responsive) 728x90 / 320x100

How to Use This Tool

  1. Paste Your SQL: Enter your SQL query in the input field, or click "Load Sample" for an example. The tool handles SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, and other SQL statements.
  2. Select Dialect: Choose your database dialect (MySQL, PostgreSQL, SQL Server, Oracle, or Standard SQL). This affects keyword recognition and formatting of dialect-specific syntax like backticks vs. brackets.
  3. Configure Formatting: Set your preferred indentation (2 spaces, 4 spaces, or tabs) and keyword case (UPPERCASE, lowercase, or preserve). Most style guides recommend uppercase keywords.
  4. Click Format SQL: The formatted query appears in the output with proper line breaks, indentation, and aligned clauses. Copy the result for use in your code, documentation, or database client.

Technical Details

SQL formatting parses queries to understand clause structure: SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY, etc. Each major clause starts on a new line; subclauses and conditions are indented. JOIN conditions align under the JOIN keyword. Subqueries are indented relative to their containing clause. The formatter tokenizes SQL into keywords, identifiers, operators, and literals, then reconstructs with consistent spacing.

Dialect differences affect parsing: MySQL uses backticks for identifiers (`table`), SQL Server uses brackets ([table]), PostgreSQL and Oracle use double quotes ("table"). Standard SQL is ANSI-compliant. The formatter preserves dialect-specific functions and syntax. For complex queries with CTEs (WITH clauses), window functions, or CASE statements, proper formatting dramatically improves readability and maintainability.

Common Mistakes to Avoid

  • Formatting Invalid SQL: The formatter may produce unexpected results with syntactically invalid queries. If output looks wrong, check for missing commas, unmatched parentheses, or unclosed quotes in your input first.
  • Losing Intentional Formatting: Some teams use specific formatting conventions (e.g., leading commas, specific alignment). The formatter applies standard rules—if your team has conventions, you may need to adjust the output manually.
  • Dialect Mismatch: Selecting the wrong dialect may misformat dialect-specific syntax. PostgreSQL's ::type casting or MySQL's backticks might be treated incorrectly if formatted as a different dialect.

Related Tools

Working with data format conversions in your queries? Use our CSV/JSON Converter to prepare data for import. For formatting web code rather than SQL, try the HTML/CSS/JS Minifier.

Frequently Asked Questions

Should SQL keywords be uppercase or lowercase?

Uppercase is the convention in most SQL style guides (SELECT, FROM, WHERE). It distinguishes keywords from identifiers and improves readability. However, SQL is case-insensitive for keywords—either works functionally. Consistency matters more than choice.

How should I format long WHERE clauses?

Put each condition on its own line, aligned under WHERE. Use AND/OR at the start of each line for visibility. Group related conditions with parentheses. This makes complex predicates scannable and easier to modify.

What's the best way to format JOINs?

Each JOIN should start on a new line at the same indentation as FROM. The ON condition should be on the same line or indented below. For multiple join conditions, use one line per condition, aligned under ON.