NC Logo UseToolSuite

SQL Formatter

Format and beautify SQL queries online with proper indentation and uppercase keywords. Also minify SQL — free tool supporting SELECT, JOIN, WHERE, GROUP BY, and more.

What is SQL Formatter?

SQL Formatter is a free online tool that beautifies and formats SQL queries with proper indentation, line breaks, and keyword capitalization. It transforms messy, single-line, or poorly formatted SQL into clean, readable code that is easy to review and debug. The tool also includes a Minify mode that strips all unnecessary whitespace and comments to produce compact SQL for use in scripts or configurations. All formatting runs entirely in your browser using vanilla JavaScript with no external libraries or server calls.

When to use it?

Use the SQL Formatter when you receive a long, single-line SQL query from a log file, ORM debug output, or a colleague's code and need to understand its structure. It is also helpful during code reviews when comparing SQL changes, or when documenting queries in technical specifications and wiki pages where readability is essential. The minify mode is useful for embedding SQL in application code where you want to reduce string length.

Common use cases

Database administrators and back-end developers commonly use SQL Formatter to clean up auto-generated queries from ORMs like Sequelize, Prisma, or Hibernate, format complex JOIN statements with multiple WHERE conditions for debugging, prepare readable SQL snippets for documentation and pull request descriptions, standardize keyword casing across team codebases, and minify stored procedures or migration scripts for deployment. It supports common SQL keywords including SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY, HAVING, INSERT, UPDATE, DELETE, CREATE, ALTER, and DROP.

Key Concepts

Essential terms and definitions related to SQL Formatter.

SQL (Structured Query Language)

A standard language for managing and querying relational databases. SQL supports four main operation types: SELECT (read data), INSERT (add data), UPDATE (modify data), and DELETE (remove data). SQL is used across all major database systems including PostgreSQL, MySQL, SQL Server, Oracle, and SQLite.

JOIN

An SQL clause that combines rows from two or more tables based on a related column. Types include INNER JOIN (only matching rows), LEFT JOIN (all left table rows + matching right), RIGHT JOIN (all right table rows + matching left), and FULL OUTER JOIN (all rows from both tables). JOINs are essential for querying normalized relational data.

Subquery

A SELECT statement nested inside another SQL statement (in WHERE, FROM, SELECT, or HAVING clauses). Subqueries allow complex filtering and derived calculations. Correlated subqueries reference the outer query and execute once per outer row; non-correlated subqueries execute independently and can often be rewritten as JOINs for better performance.

Frequently Asked Questions

Which SQL dialects does this formatter support?

The formatter handles standard SQL syntax that is common across most database systems including MySQL, PostgreSQL, SQLite, SQL Server, and Oracle. Dialect-specific extensions may not format perfectly, but standard SELECT, INSERT, UPDATE, and DELETE queries work well.

Does the formatter uppercase SQL keywords automatically?

Yes. The formatter converts SQL keywords like SELECT, FROM, JOIN, WHERE, GROUP BY, and ORDER BY to uppercase. This follows the widely adopted SQL style convention that makes queries easier to read by visually separating keywords from table and column names.

Can I format stored procedures or DDL statements?

The formatter is optimized for DML queries (SELECT, INSERT, UPDATE, DELETE). DDL statements like CREATE TABLE work for basic formatting, but complex stored procedures with procedural logic (IF/ELSE, loops) may not format as cleanly.

Does the minifier remove SQL comments?

Yes. The SQL minifier removes both single-line comments (--) and multi-line comments (/* */) along with unnecessary whitespace. This produces a compact single-line query suitable for use in application code or logging.

Troubleshooting & Technical Tips

Common errors developers encounter and how to resolve them.

Ambiguous column name error: Column ambiguity in JOIN queries

When joining multiple tables with JOIN, if both tables contain a column with the same name, the database throws an "ambiguous column name" error. Solution: prefix every column reference with the table name or alias: write u.id (FROM users u) instead of just id. The formatter does not fix such semantic errors, but it makes the query readable so you can identify the issue more easily.

Syntax error at or near "GROUP": Incorrect GROUP BY order

SQL clause ordering is strict: SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT. Placing GROUP BY before WHERE or using HAVING without GROUP BY produces a syntax error. The formatter does not automatically correct keyword ordering; it only applies indentation to the existing order. Fix the clause ordering in your query to comply with the SQL standard, then run the formatter.

Subquery parsing error: Parenthesis mismatch in nested SELECT statements

In complex subqueries (nested SELECT), when opening and closing parenthesis counts do not match, the formatter may produce incorrect indentation or fail to parse. Verify that each subquery is properly closed within its own set of parentheses. Pay special attention to IN (SELECT ...), EXISTS (SELECT ...), and FROM (SELECT ... ) AS subq constructs to ensure inner queries are fully closed.

SQL keywords inside string literals are incorrectly formatted

The formatter may detect SQL keywords like SELECT and FROM inside single-quoted ('') string values and convert them to uppercase. For example, the word select in WHERE message = 'select your option' may be affected. This does not break query logic but does change string content. After formatting, check that string literal values have been preserved in their original form.

Related Tools