Bug Days
Developer guide

Format SQL Queries and DDL for PostgreSQL, MySQL, and SQL Server

Choose the right SQL dialect, format queries and migrations for review, know the limits of pretty printing, and move CREATE TABLE scripts into an ER diagram workflow.

6 minute read SQL formatting and schema review
A formatted SQL query beside related database tables

SQL formatting is about making the query reviewable, not changing what it does. A consistent layout exposes joins, filters, window functions, and migrations that disappear in a one-line query. It should be the first step before a code review, not a substitute for executing a test query.

Choose the closest SQL dialect

PostgreSQL, MySQL, SQL Server, Oracle, DB2, BigQuery, Snowflake, and SQLite share familiar keywords but differ in identifiers, operators, functions, placeholders, and procedural syntax. Choose the closest dialect in the SQL Formatter & Beautifier before formatting. The formatter runs only in your browser; it does not open a connection or run the statement.

Format a compact query before reviewing it

Start with a representative query such as:

select customer_id,count(*) as paid_orders from orders where status = 'paid' and created_at >= current_date - interval '30 days' group by customer_id order by paid_orders desc;

Formatting separates SELECT, FROM, WHERE, GROUP BY, and ORDER BY, making it much easier to spot an accidental filter change or an aggregate that no longer matches the grouping. Keep the original query available for a line-by-line diff when the review needs to distinguish formatting from logic.

Use formatted DDL as the handoff to a schema diagram

For a migration or an existing database export, format the CREATE TABLE statements first. Then paste the supported DDL into the Schema Explorer to draw foreign-key relationships, annotate tables and columns, and export Markdown, Mermaid, or a data dictionary. Formatting does not validate constraints or execute the migration; it simply makes the handoff readable.

Share a scrubbed SQL example

A shareable formatted query is useful for a technical question or an internal review. The link includes the SQL, selected dialect, keyword casing, and indentation settings. It is not a secret store: anyone with the link can read the query. Replace customer IDs, private table names, connection strings, and any credentials with harmless placeholders before sharing.

Continue reading