Skip to content

SQL Formatter & Validator - Format SQL Queries Online Free

A free SQL formatter and validator. Capitalizes keywords, adds line breaks, and flags syntax issues like unclosed quotes or JOINs missing an ON condition.

SQL Formatter & Validator

Format and validate SQL queries with automatic indentation, keyword capitalization, and syntax error detection.

Enter a SQL query to see the formatted result.
Loading calculator...
📚

Documentation

What is a SQL formatter and validator?

A SQL formatter is a tool that rewrites a SQL query with consistent spacing, line breaks, and capitalized keywords, so it is easier to read. A SQL validator checks that same query for structural mistakes, such as an unclosed quote or a missing parenthesis, without connecting to a database. This tool does both. It runs entirely in the browser, so the query text is never sent to a server.

How to format a SQL query

  1. Paste or type a SQL query into the input box.
  2. The formatted version appears automatically on the right, with no button to press.
  3. Any validation errors appear in a list below the output.
  4. A copy button sends the formatted query to the clipboard.

SQL formatting rules

Keyword capitalization

The formatter uppercases a fixed list of SQL keywords wherever they appear outside a string or quoted identifier. The list covers the main clauses (SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING, LIMIT, OFFSET), join words (JOIN, LEFT, RIGHT, INNER, OUTER, FULL, ON), statement types (INSERT, UPDATE, DELETE, CREATE, ALTER, DROP), and common operators and connectors (AND, OR, NOT, IN, BETWEEN, LIKE, AS, CASE, WHEN, THEN, ELSE, END, UNION, DISTINCT, VALUES, SET, and a few more).

Some words that look like keywords are left as typed. Aggregate functions such as COUNT, SUM, and AVG are not on the list, so count(*) stays lowercase if it was typed lowercase. Sort direction words ASC and DESC, and the literals TRUE, FALSE, and NULL on their own, are also left untouched. Text inside a string literal ('...'), a quoted identifier ("...", `...`, or [...]), or a comment is never changed, no matter what it contains.

Line breaks and indentation

Each main clause starts on a new line at the left margin: SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING, LIMIT, and OFFSET. JOIN (and its variants like LEFT JOIN) also starts a new line. Every comma starts a new line too, so a column list in SELECT or GROUP BY ends up one column per line.

Indentation only appears inside parentheses. Content inside a subquery or a grouped expression is indented two spaces for each level of nesting. A query with no parentheses — the majority of everyday queries — is formatted with every clause flush against the left margin and no indentation at all.

Example: before and after formatting

Before:

1select id, first_name, last_name, email from customers where status = 'active' order by last_name, first_name;
2

After:

1SELECT id,
2first_name,
3last_name,
4email
5FROM customers
6WHERE status = 'active'
7ORDER BY last_name,
8first_name;
9

Every keyword is uppercase and every clause and column sits on its own line. Because the query has no parentheses, no line is indented.

Parentheses change that. Formatting (select count(*) from employees e where e.department_id = d.id) as part of a larger query indents the inner SELECT, FROM, and WHERE two spaces, and leaves count lowercase because it is a function name, not a listed keyword.

What the SQL validator checks

The validator looks for structural problems without connecting to a database:

  • Unclosed quotes. A single or double quote that is opened but never closed.
  • Unbalanced parentheses. The count of ( and ) outside strings and comments must match.
  • Subqueries without parentheses. A SELECT nested inside another SELECT ... FROM ... SELECT pattern that is not wrapped in ( ).
  • WHERE starting with AND or OR. A leading connector with nothing before it.
  • JOIN without a matching condition. A JOIN needs an ON or USING clause. CROSS JOIN and NATURAL JOIN are exempt, since they do not take one.
  • Empty GROUP BY or ORDER BY. The clause keyword with no columns after it, at the end of the query.
  • An incomplete WHERE condition. A dangling comparison such as WHERE status = with no value, or a bare WHERE with nothing after it, at the end of the query.
  • A SELECT with no FROM. Flagged only when the select list is plain column names; an expression, function call, or literal after SELECT is valid on its own and is not flagged.
  • Clause order. Two specific checks: GROUP BY must not come before WHERE, and ORDER BY must not come before GROUP BY. These are checked at the top level of the query, ignoring anything inside a subquery.

What it does not check

The validator does not know whether tables or columns actually exist, whether joined columns have compatible types, or how a query will perform. It also does not flag every rule a real database enforces. Two examples: HAVING used without GROUP BY is accepted, because grouping the whole result into one implicit group is legal SQL, so the validator raises no error for it. Likewise, it does not check whether every non-aggregated column in the SELECT list also appears in GROUP BY — that rule exists in most databases, but this tool does not test for it. A missing trailing semicolon is not flagged either, since a semicolon is a statement separator, not part of a single query.

Example: errors the validator catches

1SELECT id FROM orders
2JOIN users
3WHERE status =
4

This query produces two errors:

  1. "One or more JOIN statements are missing the ON condition." — JOIN users has no ON or USING clause.
  2. "WHERE condition is incomplete." — status = has no value after the equals sign.

Frequently asked questions

Does this tool work with MySQL, PostgreSQL, SQL Server, and Oracle?

It handles the core SQL syntax shared by those databases: SELECT, JOIN, WHERE, GROUP BY, and similar clauses. Database-specific syntax, such as PostgreSQL array literals or T-SQL procedural blocks, will not break the formatter, but it may not receive special handling.

Is the SQL query sent to a server?

No. Formatting and validation both run in the browser. The query text is not transmitted anywhere.

Does it capitalize function names like COUNT, SUM, and AVG?

No. Only clause keywords, join words, and a fixed list of operators are capitalized. Function names such as count, sum, and avg, and the words asc, desc, true, false, and null, are left exactly as typed.

Does the validator flag HAVING used without GROUP BY?

No. A HAVING clause without GROUP BY groups the entire result into a single group, which is valid SQL under the ISO/IEC 9075 standard, so the validator does not treat it as an error.

Can the validator catch every SQL mistake?

No. It checks structure — parentheses, quotes, a handful of clause-order rules, and a few common logical gaps like a JOIN with no condition. It does not check table or column names, data types, or query performance. A real database connection is still needed to confirm a query runs correctly.

Does formatting change how a query runs?

No. Formatting only changes whitespace and the capitalization of unquoted keywords. Anything inside a string, a quoted identifier, or a comment is copied through unchanged, so the query means exactly what it meant before.

References