Format and indent a SQL query

formats your SQL queries to make them readable and easy to maintain

Why SQL formatting?

SQL formatting makes SQL queries more readable and easier to understand for developers and database administrators. It eases debugging, code review and query optimisation. Well-formatted SQL is essential to maintain code quality and collaborate effectively within a development team.

Supported SQL query types

This tool supports all SQL query types, including SELECT, INSERT, UPDATE, DELETE statements, as well as table creation and modification statements (CREATE, ALTER, DROP).

How to format SQL queries

On the formatting page, you can format your SQL query by pasting it into the dedicated text area.

As soon as the SQL query is submitted, it will be formatted and displayed in a result area. If the query is invalid, an error will be displayed with an indication of the nature of the problem.

Using the formatted SQL

You can copy the formatted SQL with the dedicated copy button. This makes it easier to integrate SQL queries into your projects or reports.

Your SQL code should look like this:


SELECT u.id, u.name, u.email,
       p.product_name, p.price,
       o.order_date, o.quantity,
       (SELECT SUM(p2.price * o2.quantity)
        FROM orders o2
        JOIN products p2 ON o2.product_id = p2.id
        WHERE o2.user_id = u.id) as total_spent
FROM users u
JOIN orders o ON u.id = o.user_id
JOIN products p ON o.product_id = p.id
WHERE u.id IN (
    SELECT user_id
    FROM orders
    WHERE order_date >= '2023-01-01' AND order_date <= '2023-12-31'
    GROUP BY user_id
    HAVING COUNT(*) > 5
)
AND p.price > 50
ORDER BY u.name ASC, o.order_date DESC
LIMIT 10;

    

Frequently asked questions

Which SQL dialects are supported: MySQL, PostgreSQL, SQL Server?

The formatter recognises standard SQL syntax (ANSI) as well as the main extensions of MySQL, PostgreSQL, SQL Server (T-SQL), Oracle (PL/SQL) and SQLite. Engine-specific keywords (MySQL and PostgreSQL LIMIT, SQL Server TOP, PostgreSQL RETURNING, MERGE) are indented correctly. Native functions specific to an engine are kept as is without validation.

Does the formatter handle CTEs and recursive queries?

Yes. CTEs (Common Table Expressions) declared with WITH ... AS (...) are indented in their own block, and recursive CTEs (WITH RECURSIVE) are recognised. This makes complex queries noticeably more readable, in particular hierarchies (trees, graphs) and sliding windows. Correlated subqueries are also aligned with their parent scope.

What is the difference between a SQL formatter and a query optimiser?

A SQL formatter does not change the semantics of the query: it simply reorganises the code visually (line breaks, indentation, keyword case). An optimiser, on the other hand, rewrites the query or suggests changes (missing indexes, joins to reorder, subqueries to turn into joins) to improve performance. To analyse the execution plan, use EXPLAIN or EXPLAIN ANALYZE depending on the engine.

Are SQL comments preserved?

Yes. Inline comments -- comment and multi-line comments /* ... */ are kept at their original location. This is essential to document a complex query or mark sections (TODO, hint for the optimiser, migration version). If you send the query to an engine that interprets commented hints (/*+ INDEX(...) */ on Oracle), they are also preserved.

Why are SQL keywords uppercased?

Putting keywords (SELECT, FROM, WHERE, JOIN) in uppercase is a historical convention that visually distinguishes them from identifiers (table names, columns). This practice makes reading easier, especially in long queries. Table and column names, on the other hand, are kept in their original case to respect the schema's conventions (in particular on PostgreSQL where case is significant for quoted identifiers).

Does the formatter validate SQL syntax?

The formatter performs a light syntactic analysis that detects gross errors (unbalanced parentheses, missing semicolon at end of statement, inconsistent keywords). It validates neither the existence of tables and columns nor conformance to a precise schema. For that, run the query in EXPLAIN mode against your database, or use a dedicated linter like sqlfluff.

Example request

curl -X POST https://cdrn.fr/api/v1/tools/sql-formatter/execute \
  -H "Content-Type: application/json" \
  -d '{"input":"..."}'

Input schema

Field Type Required Default
input text

Endpoints

  • GET https://cdrn.fr/api/v1/tools - lists every available tool
  • GET https://cdrn.fr/api/v1/tools/sql-formatter - returns the schema for this tool
  • POST https://cdrn.fr/api/v1/tools/sql-formatter/execute - runs this tool with a JSON payload