Downloads

SQL formatter and query outline

Format a SQL Server or MariaDB query, see in plain words what it does, and get a list of common mistakes to check.

2dialectsSQL Server and MariaDB
Privateto your browserwhat you paste is not sent anywhere

Formatted query


    

What this query does

Best effort. It reads the formatted query, so unusual syntax can be missed.

Format a query to see its tables, joins, filters, grouping and sorting.

Common mistakes to check

Errors are clear mistakes. Warnings are often wrong or slow. These checks run after formatting.

The order a database reads your query

You write SELECT first, but the database works in a different order. Most "why can't I use my alias here?" errors come from this.

  1. FROMpick the tables
  2. JOINmatch the rows
  3. WHEREfilter rows
  4. GROUP BYmake groups
  5. HAVINGfilter groups
  6. SELECTpick and name columns
  7. ORDER BYsort
  8. TOP / LIMITkeep the first rows

SELECT runs late, so a column alias made there can be used in ORDER BY but not in WHERE.

How it works

  1. Paste a query

    Pick SQL Server or MariaDB and paste your SQL.

  2. Format and explain

    Get a tidy layout and an outline of tables, joins, filters, grouping and sorting.

  3. Check for mistakes

    Common errors and slow patterns are listed with a reason.

How the formatter works

The formatter reads your query with a tokenizer that keeps strings, quoted names such as [Order Date] or "Name", comments and brackets whole. A word inside a string, such as 'from where', is never treated as a clause. Keywords and built-in function names are written in capitals.

The layout then puts each major clause on its own line: SELECT, FROM, each JOIN with its ON condition indented under it, WHERE, GROUP BY, HAVING, ORDER BY and LIMIT or TOP. Each select item sits on its own line. AND and OR start a new line inside a WHERE or ON condition. A subquery inside brackets is indented under the line that opens it, and a WITH clause lists each named result as a block.

The outline and the checks read the formatted text back. The outline lists the tables a query reads, each join with its type and condition, the filters, the grouping, the sorting and any row limit. The checks look for common mistakes, listed below.

What it does not do

Frequently asked questions

Which checks does it run?

SELECT * with no list of columns. A JOIN with no ON condition, which becomes a cross join. A comparison with NULL made with an equals sign, which never matches. A WHERE condition on the right-hand table of a LEFT JOIN, which turns the join into an inner join. A function wrapped around a column in WHERE. A number in quotes compared with a column. TOP without ORDER BY, or LIMIT without ORDER BY in MariaDB. LIMIT in SQL Server, or TOP in MariaDB.

Why does the LEFT JOIN check matter?

A LEFT JOIN keeps the rows of the first table even when there is no match. A condition in WHERE on the second table removes those unmatched rows, so the result is the same as an INNER JOIN. If that is what you want, say INNER JOIN. If not, move the condition into the ON clause.

Does the tool send my query anywhere?

No. The formatting, the outline and the checks run in your browser.

Why is there no button to run the query here?

The SQL editor on this site reads its database from the address, but it does not take a query in the address. Paste the formatted query into the editor to run it.

More tools: all tools · function reference · guides