Menu

How to Format SQL for MySQL, PostgreSQL, SQLite, Oracle, and SQL Server

Learn how SQL formatting differs across MySQL, PostgreSQL, SQLite, Oracle PL/SQL, SQL Server T-SQL, and MariaDB, and format each dialect safely in your browser.

Posted on By
On this page

SQL formatting is not only about adding line breaks and indentation. Different database systems use different keywords, operators, quoting rules, procedural syntax, and dialect-specific extensions.

A formatter that treats every query as generic SQL can produce awkward or misleading output. For reliable results, select the dialect that matches the database you actually use.

SQLiz provides a free SQL Formatter that supports MySQL, MariaDB, PostgreSQL, SQLite, Oracle PL/SQL, and SQL Server T-SQL. Formatting runs locally in your browser and does not send SQL text to SQLiz.

MySQL SQL formatting

Open the formatter with the MySQL dialect preselected.

For example, this query:

select department,count(*) as total from employees where active=1 group by department order by total desc;

can be formatted into a layout such as:

SELECT
  department,
  COUNT(*) AS total
FROM employees
WHERE active = 1
GROUP BY department
ORDER BY total DESC;

The MySQL dialect is useful when your query includes MySQL-specific syntax such as backtick-quoted identifiers, LIMIT, JSON functions, or MySQL expressions.

For MySQL functions and data types, see the MySQL Reference.

PostgreSQL SQL formatting

Open the formatter with the PostgreSQL dialect preselected.

PostgreSQL queries commonly use syntax such as:

  • RETURNING
  • ILIKE
  • ::type casts
  • DISTINCT ON
  • JSON/JSONB operators
  • array expressions

Selecting the PostgreSQL dialect helps the formatter recognize those tokens rather than treating the query as generic SQL.

Example:

select distinct on (customer_id) customer_id,created_at,total from orders order by customer_id,created_at desc;

can be formatted as:

SELECT DISTINCT ON (customer_id)
  customer_id,
  created_at,
  total
FROM orders
ORDER BY
  customer_id,
  created_at DESC;

For related syntax, see the PostgreSQL Reference.

SQLite SQL formatting

Open the formatter with the SQLite dialect preselected.

SQLite has its own functions and extensions, including JSON functions, PRAGMA statements, flexible typing behavior, and SQLite-specific date/time patterns.

After formatting a SQLite query, you can send it to the SQLite Playground and run it directly in the browser.

Example:

with ranked as(select name,department,salary,row_number() over(partition by department order by salary desc) rn from employees) select * from ranked where rn<=3;

becomes easier to review after formatting:

WITH ranked AS (
  SELECT
    name,
    department,
    salary,
    ROW_NUMBER() OVER (
      PARTITION BY department
      ORDER BY salary DESC
    ) AS rn
  FROM employees
)
SELECT *
FROM ranked
WHERE rn <= 3;

For functions and syntax, see the SQLite Function Reference.

Oracle SQL and PL/SQL formatting

Open the formatter with Oracle PL/SQL preselected.

Oracle syntax differs from other SQL systems in areas such as:

  • PL/SQL blocks
  • BEGIN ... END
  • packages and procedures
  • MERGE
  • Oracle date/time functions
  • hierarchical queries
  • object-relational syntax

Formatting a procedural PL/SQL block with a generic SQL formatter can produce poor results because the formatter needs to understand block structure.

Example:

begin update accounts set active=0 where last_login<sysdate-365; commit; end;

is much easier to inspect when formatted into separate statements and block levels.

For Oracle functions and data types, see the Oracle Reference.

SQL Server T-SQL formatting

Open the formatter with SQL Server T-SQL preselected.

T-SQL includes syntax that differs from MySQL or PostgreSQL, such as:

  • TOP
  • CROSS APPLY
  • OUTER APPLY
  • TRY_CONVERT()
  • bracket-quoted identifiers
  • DECLARE variables
  • T-SQL procedural statements

Example:

select top 10 customer_id,sum(total) total_sales from orders group by customer_id order by total_sales desc;

formats into:

SELECT TOP 10
  customer_id,
  SUM(total) AS total_sales
FROM orders
GROUP BY customer_id
ORDER BY total_sales DESC;

For built-in functions and data types, see the SQL Server Reference.

MariaDB SQL formatting

MariaDB is closely related to MySQL but has its own functions and features.

Use the MariaDB dialect when formatting MariaDB queries, especially when the SQL uses MariaDB-specific JSON, date, sequence, or procedural syntax.

For syntax references, see the MariaDB Reference.

What a formatter does not do

Formatting improves readability, but it does not prove that a query is correct.

A formatter does not normally:

  • connect to your database
  • check table or column names
  • validate permissions
  • verify server-version compatibility
  • run the SQL
  • guarantee that the query returns the intended result

You should still test formatted SQL against the target database.

For SQLite, you can use the SQLite Playground to run the result immediately.

Does formatting change query behavior?

A formatter should primarily change whitespace, indentation, and optional keyword casing.

However, database extensions and unusual procedural syntax may not be recognized perfectly by every formatter. Review the output before running important SQL.

The SQLiz formatter does not execute the query and does not automatically translate SQL from one database dialect to another.

Format SQL without uploading it

The SQLiz formatter runs in the browser.

Your query text is not sent to SQLiz for formatting.

This is useful when working with internal SQL that you do not want copied to a remote formatting service.

To format a query now, open the SQL Formatter and select the matching database dialect.