DevToolBoxGRATUIT
Blog

Formateur SQL en Ligne — Formater et Embellir les Requetes SQL

16 min de lecturepar DevToolBox

TL;DR

A SQL formatter (also called a SQL beautifier or SQL pretty printer) transforms messy, unindented SQL queries into clean, consistently styled code that is easy to read, review, and debug. Whether you are working with simple SELECT statements or complex multi-table JOINs with subqueries and CTEs, formatting your SQL dramatically improves code quality and team collaboration. Use our free SQL formatter online to instantly beautify any SQL query in your browser, with support for MySQL, PostgreSQL, SQLite, and SQL Server dialects.

Key Takeaways

  • SQL formatting adds consistent indentation, line breaks, and keyword casing to make queries readable and maintainable.
  • Use a SQL formatter online for instant, zero-install query beautification directly in your browser.
  • Uppercase SQL keywords (SELECT, FROM, WHERE, JOIN) is the most widely adopted convention for visual distinction.
  • Always place each major clause (SELECT, FROM, WHERE, GROUP BY, ORDER BY) on its own line for clarity.
  • Format JOINs with the join type, table, and ON condition each clearly visible, using indentation to show relationships.
  • Use Common Table Expressions (CTEs) instead of deeply nested subqueries to improve readability and testability.
  • Different SQL dialects (MySQL, PostgreSQL, SQLite, SQL Server) have distinct syntax nuances that formatters should handle.
  • Integrate SQL linting tools like sqlfluff into your CI/CD pipeline to enforce consistent formatting across your team.

Why SQL Formatting Matters

SQL is one of the most widely used programming languages in the world. Every application that interacts with a relational database relies on SQL queries to read, write, and transform data. Yet SQL is also one of the most commonly written in an unformatted, inconsistent style. Developers frequently write quick one-liner queries during debugging, copy SQL from ORMs or query builders, or inherit legacy queries that were never properly formatted.

Unformatted SQL creates real problems. A 200-character single-line query with multiple JOINs and conditions is extremely difficult to understand at a glance. When a bug appears in production, the developer who needs to diagnose the issue must first mentally parse the query structure before they can even begin to identify the problem. This wastes time and increases the risk of introducing new errors during fixes.

SQL formatting solves these problems by applying consistent indentation, line breaks, keyword casing, and alignment rules to your queries. The semantic meaning of the query remains identical, but the visual structure becomes immediately clear. You can see at a glance which tables are joined, what conditions filter the results, how data is grouped and sorted, and where subqueries or CTEs add complexity.

Studies on code readability consistently show that well-formatted code is reviewed faster, contains fewer bugs, and is easier to maintain over time. This applies to SQL just as much as it does to Python, JavaScript, or any other language. If your team does code reviews on SQL queries (and you should), formatting is not optional.

SQL Formatter Online: Instant Query Beautification

The fastest way to format SQL is with a browser-based tool. No installation, no configuration, no editor plugins required. Just paste your SQL and get perfectly formatted output. Our SQL formatter online tool provides:

  • Instant formatting: Paste raw SQL on the left, see beautified output on the right in real time.
  • Dialect support: Choose between MySQL, PostgreSQL, SQLite, SQL Server, and standard SQL formatting rules.
  • Keyword casing: Automatically uppercase SQL keywords while preserving the case of table and column names.
  • Customizable indentation: Select 2-space, 4-space, or tab-based indentation to match your team conventions.
  • One-click copy: Copy the formatted result to your clipboard instantly.
  • Privacy first: All processing happens client-side in your browser. No data is sent to any server.

This is ideal when you extract a query from application logs, receive SQL from a colleague, export queries from a database GUI, or need to clean up SQL generated by an ORM. Instead of manually adding line breaks and indentation, paste the query and get clean output in milliseconds. Try it now: format SQL online.

SQL Formatting Best Practices

While there is no single universal SQL style guide (unlike Python's PEP 8 or JavaScript's Prettier defaults), the following conventions are widely adopted by database teams, open-source projects, and major tech companies. Applying these consistently will make your SQL immediately more readable.

1. Uppercase SQL Keywords

The most universally adopted SQL convention is to write reserved keywords in uppercase. This creates a clear visual distinction between the query structure and the data-specific elements (table names, column names, aliases).

-- Good: Keywords in uppercase
SELECT
  u.id,
  u.name,
  u.email,
  COUNT(o.id) AS order_count
FROM users u
INNER JOIN orders o ON o.user_id = u.id
WHERE u.status = 'active'
GROUP BY u.id, u.name, u.email
HAVING COUNT(o.id) > 5
ORDER BY order_count DESC
LIMIT 20;

-- Avoid: Mixed case or all lowercase keywords
select u.id, u.name, u.email, count(o.id) as order_count
from users u inner join orders o on o.user_id = u.id
where u.status = 'active' group by u.id, u.name, u.email
having count(o.id) > 5 order by order_count desc limit 20;

When keywords are uppercase, you can scan a query vertically and instantly identify the SELECT, FROM, WHERE, GROUP BY, and ORDER BY clauses. This is especially valuable in long queries with many conditions.

2. One Clause Per Line

Each major SQL clause should start on its own line. This is the single most impactful formatting rule. It transforms a wall of text into a structured document.

-- Good: Each clause on its own line
SELECT
  p.product_name,
  p.category,
  p.price,
  s.quantity_in_stock
FROM products p
LEFT JOIN stock s
  ON s.product_id = p.id
WHERE p.price > 50.00
  AND p.category IN ('electronics', 'software')
  AND s.quantity_in_stock > 0
ORDER BY p.price DESC;

Notice how the WHERE conditions are indented under the WHERE keyword. This makes it clear that all three conditions belong to the same filtering clause. The same pattern applies to JOIN conditions indented under the JOIN keyword.

3. Indent Selected Columns

When a SELECT statement retrieves more than two or three columns, list each column on its own line with consistent indentation. This makes it easy to count columns, add or remove columns, and spot typos.

-- Good: Each column on its own line
SELECT
  e.employee_id,
  e.first_name,
  e.last_name,
  e.department,
  e.hire_date,
  e.salary,
  m.first_name AS manager_name
FROM employees e
LEFT JOIN employees m
  ON m.employee_id = e.manager_id;

-- Acceptable for short queries
SELECT id, name, email
FROM users
WHERE id = 42;

4. Consistent Naming Conventions

While formatting handles the visual layout, naming conventions handle the semantic clarity. These two work together to produce truly readable SQL.

  • Table names: Use snake_case and plural nouns: users, order_items, product_categories.
  • Column names: Use snake_case: first_name, created_at, is_active.
  • Aliases: Use meaningful short aliases: users u, orders o, order_items oi. Avoid single arbitrary letters like a, b, c that reveal nothing about the table.
  • Boolean columns: Prefix with is_, has_, or can_: is_active, has_subscription, can_edit.
  • Timestamp columns: Use _at suffix: created_at, updated_at, deleted_at.
  • Foreign keys: Use [referenced_table_singular]_id: user_id, order_id, category_id.

Formatting JOINs Properly

JOINs are where SQL formatting pays the biggest dividends. A poorly formatted multi-join query is one of the hardest things to read in all of programming. A well-formatted one is immediately clear.

Basic JOIN Formatting

-- Good: JOIN type, table, and condition clearly separated
SELECT
  c.customer_name,
  o.order_date,
  o.total_amount,
  p.product_name,
  oi.quantity
FROM customers c
INNER JOIN orders o
  ON o.customer_id = c.id
INNER JOIN order_items oi
  ON oi.order_id = o.id
INNER JOIN products p
  ON p.id = oi.product_id
WHERE o.order_date >= '2025-01-01'
  AND o.status = 'completed'
ORDER BY o.order_date DESC;

Each JOIN starts on a new line at the same indentation level as FROM. The ON condition is indented beneath the JOIN. This vertical alignment makes it trivial to trace the join chain from customers through orders to order items to products.

Multi-Condition JOINs

When a JOIN has multiple conditions, indent each condition and use AND/OR alignment:

SELECT
  e.employee_name,
  d.department_name,
  r.role_name
FROM employees e
INNER JOIN department_assignments da
  ON da.employee_id = e.id
  AND da.is_primary = TRUE
  AND da.effective_date <= CURRENT_DATE
INNER JOIN departments d
  ON d.id = da.department_id
LEFT JOIN roles r
  ON r.id = e.role_id;

Self-Joins

Self-joins are inherently confusing because the same table appears multiple times. Clear aliases are essential:

-- Employee hierarchy with self-join
SELECT
  emp.first_name AS employee_name,
  mgr.first_name AS manager_name,
  dir.first_name AS director_name
FROM employees emp
LEFT JOIN employees mgr
  ON mgr.id = emp.manager_id
LEFT JOIN employees dir
  ON dir.id = mgr.manager_id
WHERE emp.department = 'Engineering';

Formatting Subqueries

Subqueries add nesting complexity that makes formatting critical. Without proper indentation, a subquery buried inside a WHERE clause or FROM clause can be almost impossible to parse visually.

Subqueries in WHERE Clauses

-- Subquery in WHERE with proper indentation
SELECT
  p.product_name,
  p.price,
  p.category
FROM products p
WHERE p.price > (
  SELECT AVG(p2.price)
  FROM products p2
  WHERE p2.category = p.category
)
ORDER BY p.price DESC;

Subqueries in FROM Clauses (Derived Tables)

-- Derived table with clear indentation
SELECT
  dept_stats.department,
  dept_stats.avg_salary,
  dept_stats.employee_count
FROM (
  SELECT
    e.department,
    AVG(e.salary) AS avg_salary,
    COUNT(*) AS employee_count
  FROM employees e
  WHERE e.status = 'active'
  GROUP BY e.department
) dept_stats
WHERE dept_stats.employee_count >= 5
ORDER BY dept_stats.avg_salary DESC;

EXISTS Subqueries

-- EXISTS with formatted subquery
SELECT
  c.customer_name,
  c.email
FROM customers c
WHERE EXISTS (
  SELECT 1
  FROM orders o
  WHERE o.customer_id = c.id
    AND o.order_date >= '2025-01-01'
    AND o.total_amount > 100.00
)
AND NOT EXISTS (
  SELECT 1
  FROM complaints comp
  WHERE comp.customer_id = c.id
    AND comp.status = 'unresolved'
);

Formatting Common Table Expressions (CTEs)

CTEs (the WITH clause) are one of the most powerful features of modern SQL. They allow you to break complex queries into named, reusable building blocks. Properly formatted CTEs are dramatically more readable than equivalent nested subqueries.

Basic CTE Structure

WITH active_customers AS (
  SELECT
    c.id,
    c.name,
    c.email,
    c.signup_date
  FROM customers c
  WHERE c.status = 'active'
    AND c.last_login >= CURRENT_DATE - INTERVAL '90 days'
),
customer_orders AS (
  SELECT
    ac.id AS customer_id,
    ac.name AS customer_name,
    COUNT(o.id) AS order_count,
    SUM(o.total_amount) AS total_spent,
    MAX(o.order_date) AS last_order_date
  FROM active_customers ac
  INNER JOIN orders o
    ON o.customer_id = ac.id
  WHERE o.status = 'completed'
  GROUP BY ac.id, ac.name
)
SELECT
  co.customer_name,
  co.order_count,
  co.total_spent,
  co.last_order_date,
  ROUND(co.total_spent / co.order_count, 2) AS avg_order_value
FROM customer_orders co
WHERE co.order_count >= 3
ORDER BY co.total_spent DESC
LIMIT 50;

Notice the formatting pattern: each CTE is named on its own line with AS and an opening parenthesis, the CTE body is indented, and the closing parenthesis aligns with the CTE name. Multiple CTEs are separated by commas. The final SELECT references the CTEs like regular tables.

Recursive CTEs

-- Recursive CTE for organizational hierarchy
WITH RECURSIVE org_hierarchy AS (
  -- Base case: top-level managers
  SELECT
    e.id,
    e.name,
    e.manager_id,
    e.department,
    1 AS level,
    e.name AS path
  FROM employees e
  WHERE e.manager_id IS NULL

  UNION ALL

  -- Recursive case: employees under managers
  SELECT
    e.id,
    e.name,
    e.manager_id,
    e.department,
    oh.level + 1,
    oh.path || ' > ' || e.name
  FROM employees e
  INNER JOIN org_hierarchy oh
    ON oh.id = e.manager_id
)
SELECT
  id,
  name,
  department,
  level,
  path
FROM org_hierarchy
ORDER BY path;

Formatting Window Functions

Window functions (OVER clause) add significant complexity to SELECT statements. Proper formatting prevents them from becoming unreadable.

SELECT
  e.department,
  e.name,
  e.salary,
  AVG(e.salary) OVER (
    PARTITION BY e.department
  ) AS dept_avg_salary,
  RANK() OVER (
    PARTITION BY e.department
    ORDER BY e.salary DESC
  ) AS salary_rank,
  SUM(e.salary) OVER (
    PARTITION BY e.department
    ORDER BY e.hire_date
    ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
  ) AS running_total
FROM employees e
WHERE e.status = 'active'
ORDER BY e.department, e.salary DESC;

When the OVER clause has both PARTITION BY and ORDER BY (and optionally a frame specification), place each on its own line inside the parentheses. For simple window functions with just a PARTITION BY, a single line is acceptable: AVG(salary) OVER (PARTITION BY department).

SQL Dialects: Formatting Differences

While core SQL syntax is standardized (SQL:2016, SQL:2023), each database engine adds its own extensions, functions, and syntax variations. A good SQL formatter needs to handle these differences. Here is how formatting varies across the most popular databases.

MySQL

MySQL uses backtick quoting for identifiers, has its own LIMIT syntax, and supports specific functions and data types:

-- MySQL-specific formatting
SELECT
  `user`.`id`,
  `user`.`name`,
  DATE_FORMAT(`user`.`created_at`, '%Y-%m-%d') AS signup_date,
  IFNULL(`user`.`nickname`, 'Anonymous') AS display_name
FROM `users` AS `user`
WHERE `user`.`status` = 'active'
ORDER BY `user`.`created_at` DESC
LIMIT 20 OFFSET 40;

PostgreSQL

PostgreSQL uses double-quote quoting, supports advanced features like DISTINCT ON, array operations, JSONB, and rich type casting:

-- PostgreSQL-specific formatting
SELECT DISTINCT ON (u.department)
  u.id,
  u.name,
  u.department,
  u.salary,
  u.metadata->>'role' AS role,
  ARRAY_AGG(s.skill_name) AS skills
FROM users u
LEFT JOIN user_skills us
  ON us.user_id = u.id
LEFT JOIN skills s
  ON s.id = us.skill_id
WHERE u.salary > 50000
  AND u.metadata @> '{"active": true}'::jsonb
GROUP BY u.id, u.name, u.department, u.salary, u.metadata
ORDER BY u.department, u.salary DESC;

SQLite

SQLite has a more limited feature set but includes some unique behaviors. It uses dynamic typing and has specific pragma statements:

-- SQLite-specific formatting
SELECT
  t.id,
  t.title,
  t.completed,
  COALESCE(t.priority, 'normal') AS priority,
  strftime('%Y-%m-%d', t.created_at) AS created_date
FROM todos t
WHERE t.completed = 0
  AND t.created_at >= datetime('now', '-30 days')
ORDER BY
  CASE t.priority
    WHEN 'high' THEN 1
    WHEN 'normal' THEN 2
    WHEN 'low' THEN 3
    ELSE 4
  END,
  t.created_at DESC
LIMIT 50;

SQL Server (T-SQL)

SQL Server uses square bracket quoting, TOP instead of LIMIT, and has its own string functions and date operations:

-- SQL Server (T-SQL) specific formatting
SELECT TOP 20
  [u].[id],
  [u].[name],
  [u].[email],
  FORMAT([u].[created_at], 'yyyy-MM-dd') AS signup_date,
  ISNULL([u].[nickname], 'Anonymous') AS display_name,
  STRING_AGG([r].[role_name], ', ') AS roles
FROM [dbo].[users] [u]
LEFT JOIN [dbo].[user_roles] [ur]
  ON [ur].[user_id] = [u].[id]
LEFT JOIN [dbo].[roles] [r]
  ON [r].[id] = [ur].[role_id]
WHERE [u].[status] = 'active'
GROUP BY [u].[id], [u].[name], [u].[email],
  [u].[created_at], [u].[nickname]
ORDER BY [u].[created_at] DESC;

SQL Linting and Automated Formatting Tools

Manual formatting works for individual queries, but for teams working with hundreds or thousands of SQL files, automated tools are essential. Here are the most popular SQL linting and formatting tools available today.

sqlfluff

sqlfluff is the most popular open-source SQL linter and formatter. It supports multiple dialects and can be integrated into CI/CD pipelines:

# Install sqlfluff
pip install sqlfluff

# Lint a SQL file (report violations)
sqlfluff lint query.sql --dialect postgres

# Fix formatting violations automatically
sqlfluff fix query.sql --dialect postgres

# Check specific rules
sqlfluff lint query.sql --rules L010,L014,L030

# Lint all SQL files in a directory
sqlfluff lint ./sql/ --dialect mysql

Configure sqlfluff with a .sqlfluff file in your project root:

[sqlfluff]
dialect = postgres
templater = raw
max_line_length = 120

[sqlfluff:indentation]
indent_unit = space
tab_space_size = 4

[sqlfluff:rules:capitalisation.keywords]
capitalisation_policy = upper

[sqlfluff:rules:capitalisation.identifiers]
capitalisation_policy = lower

[sqlfluff:rules:capitalisation.functions]
capitalisation_policy = upper

sql-formatter (npm package)

For JavaScript/TypeScript projects, the sql-formatter npm package provides programmatic SQL formatting:

import { format } from 'sql-formatter';

const formatted = format(
  'SELECT id, name, email FROM users WHERE status = "active" ORDER BY name',
  {
    language: 'postgresql',
    tabWidth: 2,
    keywordCase: 'upper',
    linesBetweenQueries: 2,
  }
);

console.log(formatted);
// SELECT
//   id,
//   name,
//   email
// FROM
//   users
// WHERE
//   status = "active"
// ORDER BY
//   name

IDE Integration

Most popular IDEs and editors have SQL formatting support:

  • VS Code: Install the SQL Formatter extension or use the SQLTools extension which includes formatting. Configure keyboard shortcuts for one-click formatting.
  • JetBrains IDEs (DataGrip, IntelliJ, PyCharm): Built-in SQL formatter with extensive configuration. Access via Code > Reformat Code or Ctrl+Alt+L / Cmd+Option+L.
  • DBeaver: Built-in SQL formatter accessible from the SQL Editor toolbar. Supports custom formatting profiles.
  • pgAdmin: Includes a query formatter for PostgreSQL queries in the query tool.

Git Pre-Commit Hooks

Enforce SQL formatting automatically before code is committed:

# .pre-commit-config.yaml
repos:
  - repo: https://github.com/sqlfluff/sqlfluff
    rev: 3.0.0
    hooks:
      - id: sqlfluff-lint
        args: [--dialect, postgres]
      - id: sqlfluff-fix
        args: [--dialect, postgres, --force]

Common SQL Formatting Mistakes

Even experienced developers make formatting mistakes that reduce SQL readability. Here are the most common ones and how to fix them.

1. Everything on One Line

-- Bad: Impossible to read
SELECT u.id, u.name, u.email, COUNT(o.id) AS order_count FROM users u INNER JOIN orders o ON o.user_id = u.id WHERE u.status = 'active' AND u.created_at >= '2025-01-01' GROUP BY u.id, u.name, u.email HAVING COUNT(o.id) > 3 ORDER BY order_count DESC LIMIT 10;

-- Good: Each clause on its own line
SELECT
  u.id,
  u.name,
  u.email,
  COUNT(o.id) AS order_count
FROM users u
INNER JOIN orders o
  ON o.user_id = u.id
WHERE u.status = 'active'
  AND u.created_at >= '2025-01-01'
GROUP BY u.id, u.name, u.email
HAVING COUNT(o.id) > 3
ORDER BY order_count DESC
LIMIT 10;

2. Inconsistent Keyword Casing

-- Bad: Mixed keyword casing
Select u.name, u.email
From users u
where u.status = 'active'
ORDER by u.name
Limit 20;

-- Good: Consistent uppercase keywords
SELECT u.name, u.email
FROM users u
WHERE u.status = 'active'
ORDER BY u.name
LIMIT 20;

3. Using SELECT * in Production Code

-- Bad: SELECT * hides what data you actually need
SELECT * FROM users WHERE status = 'active';

-- Good: Explicit column list
SELECT
  id,
  name,
  email,
  status,
  created_at
FROM users
WHERE status = 'active';

Using SELECT * is acceptable for quick ad-hoc queries during debugging, but production code should always list columns explicitly. This makes the query self-documenting, prevents breaking changes when columns are added or reordered, and allows the database to optimize the query plan.

4. Missing Table Aliases in JOINs

-- Bad: Ambiguous column references without aliases
SELECT
  name,
  email,
  order_date,
  total_amount
FROM users
INNER JOIN orders ON orders.user_id = users.id;

-- Good: Clear aliases resolve ambiguity
SELECT
  u.name,
  u.email,
  o.order_date,
  o.total_amount
FROM users u
INNER JOIN orders o
  ON o.user_id = u.id;

5. Deeply Nested Subqueries

-- Bad: Triple-nested subquery
SELECT * FROM (
  SELECT * FROM (
    SELECT id, name, (
      SELECT COUNT(*) FROM orders WHERE orders.user_id = users.id
    ) AS cnt FROM users
  ) sub WHERE cnt > 5
) final WHERE name LIKE 'A%';

-- Good: CTEs make the logic clear
WITH user_order_counts AS (
  SELECT
    u.id,
    u.name,
    COUNT(o.id) AS order_count
  FROM users u
  LEFT JOIN orders o
    ON o.user_id = u.id
  GROUP BY u.id, u.name
),
frequent_buyers AS (
  SELECT id, name, order_count
  FROM user_order_counts
  WHERE order_count > 5
)
SELECT id, name, order_count
FROM frequent_buyers
WHERE name LIKE 'A%';

Formatting INSERT, UPDATE, and DELETE Statements

Formatting is not just for SELECT queries. INSERT, UPDATE, and DELETE statements also benefit from consistent structure.

INSERT Statements

-- Single row insert
INSERT INTO users (name, email, status, created_at)
VALUES ('Alice Johnson', 'alice@example.com', 'active', NOW());

-- Multi-row insert
INSERT INTO products (name, category, price, stock)
VALUES
  ('Keyboard', 'electronics', 79.99, 150),
  ('Mouse', 'electronics', 29.99, 300),
  ('Monitor', 'electronics', 499.99, 50),
  ('Desk Lamp', 'office', 34.99, 200);

-- INSERT from SELECT
INSERT INTO order_archive (
  order_id,
  customer_id,
  order_date,
  total_amount
)
SELECT
  o.id,
  o.customer_id,
  o.order_date,
  o.total_amount
FROM orders o
WHERE o.order_date < '2024-01-01'
  AND o.status = 'completed';

UPDATE Statements

-- Simple update
UPDATE users
SET
  status = 'inactive',
  updated_at = NOW()
WHERE last_login < CURRENT_DATE - INTERVAL '365 days'
  AND status = 'active';

-- Update with JOIN (PostgreSQL)
UPDATE order_items oi
SET
  price = p.current_price,
  updated_at = NOW()
FROM products p
WHERE p.id = oi.product_id
  AND oi.price <> p.current_price;

DELETE Statements

-- Delete with conditions
DELETE FROM session_tokens
WHERE expires_at < NOW()
  AND user_id IN (
    SELECT id
    FROM users
    WHERE status = 'deleted'
  );

Formatting CASE Expressions

CASE expressions add conditional logic to SQL queries. Proper formatting makes the branches clear:

SELECT
  o.id,
  o.total_amount,
  CASE
    WHEN o.total_amount >= 1000 THEN 'premium'
    WHEN o.total_amount >= 500 THEN 'standard'
    WHEN o.total_amount >= 100 THEN 'basic'
    ELSE 'micro'
  END AS order_tier,
  CASE o.status
    WHEN 'completed' THEN 'Delivered'
    WHEN 'shipped' THEN 'In Transit'
    WHEN 'processing' THEN 'Being Prepared'
    WHEN 'cancelled' THEN 'Cancelled'
    ELSE 'Unknown'
  END AS status_label
FROM orders o
ORDER BY o.total_amount DESC;

Each WHEN clause goes on its own line, indented under CASE. The ELSE and END align with CASE or are indented one level. Both the searched CASE (with conditions) and the simple CASE (with a reference value) follow the same pattern.

SQL Formatting in Team Environments

Individual formatting preferences matter less than team consistency. Here is how to establish and enforce SQL formatting standards across a development team.

Create a SQL Style Guide

Document your team's SQL conventions in a shared style guide. Key decisions to make:

  • Keyword casing: UPPER, lower, or Title Case (UPPER is the most common)
  • Indentation: 2 spaces, 4 spaces, or tabs
  • Comma placement: Leading (comma at start of line) or trailing (comma at end of line)
  • Join style: Whether to write JOIN or INNER JOIN explicitly
  • Alias style: Whether to use AS keyword (users AS u vs users u)
  • Maximum line length: 80, 100, or 120 characters

Automate Enforcement

Once conventions are defined, automate them:

  1. Pre-commit hooks: Use sqlfluff with pre-commit to check formatting before code is committed.
  2. CI/CD pipeline: Add a sqlfluff lint step to your CI pipeline that fails the build on formatting violations.
  3. Editor configuration: Share editor settings (.editorconfig, VS Code workspace settings) that apply SQL formatting rules automatically.
  4. Code review: Include SQL formatting in code review checklists, but rely on automated tools for enforcement rather than manual checks.

Performance Considerations: Formatting vs. Optimization

It is important to understand that SQL formatting is purely cosmetic. It does not affect query performance. The database query planner receives the same logical query regardless of whether it is on one line or a hundred lines with perfect indentation. Whitespace and casing are stripped during parsing.

However, well-formatted SQL makes it much easier to identify performance problems. When a query is clearly structured, you can quickly spot:

  • Missing indexes: WHERE and JOIN conditions on unindexed columns are visible at a glance.
  • Unnecessary JOINs: Tables joined but never referenced in SELECT or WHERE clauses stand out.
  • Suboptimal patterns: Correlated subqueries that could be JOINs, or N+1 query patterns, become obvious.
  • Cartesian products: Missing JOIN conditions are much easier to spot when each JOIN is on its own line.
  • Redundant conditions: Duplicate WHERE clauses or contradictory conditions are visible in formatted code.

For query optimization beyond formatting, check out our guide on SQL query optimization techniques.

Frequently Asked Questions

What is a SQL formatter and why should I use one?

A SQL formatter (also called a SQL beautifier or SQL pretty printer) is a tool that transforms unstructured, hard-to-read SQL queries into clean, consistently indented code. It adds line breaks between clauses, indents nested elements, and standardizes keyword casing. You should use one because formatted SQL is dramatically easier to read, debug, and review. It reduces bugs, speeds up code reviews, and improves team collaboration. Our SQL formatter online processes everything in your browser with no data sent to any server.

Does formatting affect SQL query performance?

No. SQL formatting is purely cosmetic and has zero impact on query performance. The database engine strips all whitespace, line breaks, and casing during query parsing. The query planner receives the same logical query regardless of how it is formatted. Formatting only affects human readability. However, well-formatted SQL makes it much easier to identify performance issues like missing indexes, unnecessary JOINs, or suboptimal subqueries.

Should SQL keywords be uppercase or lowercase?

The most widely adopted convention is to write SQL keywords in UPPERCASE (SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY). This creates a clear visual distinction between the query structure (keywords) and the data-specific elements (table names, column names, aliases). While lowercase keywords work identically, uppercase is the industry standard used by most style guides, books, documentation, and professional teams. Pick one convention and apply it consistently across your entire codebase.

What is the difference between SQL formatting and SQL linting?

SQL formatting reorganizes the visual layout of a query (indentation, line breaks, casing) without changing its meaning. SQL linting goes further by analyzing the query for potential issues, anti-patterns, and style violations. A linter like sqlfluff can detect problems such as using SELECT *, implicit JOINs, missing table aliases, inconsistent naming conventions, and overly complex subqueries. Formatting is a subset of what linters do. For best results, use both: a formatter for instant beautification and a linter in your CI/CD pipeline for comprehensive code quality checks.

How do I format SQL for different database dialects?

Different databases have distinct syntax: MySQL uses backtick quoting and LIMIT/OFFSET, PostgreSQL uses double-quote quoting and supports DISTINCT ON and JSONB operators, SQL Server uses square brackets and TOP, and SQLite has its own function set. A good SQL formatter handles these differences by letting you select the target dialect. Our online SQL formatter supports MySQL, PostgreSQL, SQLite, and SQL Server. When using command-line tools like sqlfluff, specify the dialect with the --dialect flag.

When should I use CTEs instead of subqueries?

Use Common Table Expressions (CTEs) whenever a subquery is more than a few lines long, when the same subquery logic is referenced multiple times, when the query has more than two levels of nesting, or when you need to build up a complex result step by step. CTEs make queries self-documenting because each CTE has a descriptive name. They are also easier to test individually and debug. The main exception is simple, single-use subqueries in WHERE clauses (like WHERE id IN (SELECT ...)) where the subquery is short and clear. In those cases, a subquery is perfectly fine.

What are the best tools for automated SQL formatting?

The best tools depend on your workflow. For browser-based formatting, use our SQL formatter online. For Python projects, sqlfluff is the industry standard linter and formatter with CI/CD integration. For JavaScript/TypeScript projects, the sql-formatter npm package provides programmatic formatting. For IDE users, DataGrip has the best built-in SQL formatter, while VS Code users can install the SQL Formatter extension. For team enforcement, combine sqlfluff with pre-commit hooks and CI pipeline checks.

Related Tools and Resources

Explore more SQL and formatting tools on DevToolBox:

𝕏 Twitterin LinkedIn
Cet article vous a-t-il aidé ?

Restez informé

Recevez des astuces dev et les nouveaux outils chaque semaine.

Pas de spam. Désabonnez-vous à tout moment.

Essayez ces outils associés

SQLSQL FormatterPSSQL to Prisma Schema{ }JSON Formatter<>XML Formatter

Articles connexes

Bonnes pratiques de formatage SQL : Guide de style pour des requêtes lisibles

Écrivez du SQL propre et lisible. Indentation, capitalisation, alignement JOIN, style de sous-requêtes et CTE.

Les jointures SQL expliquées : Guide visuel avec exemples

Apprenez les jointures SQL avec des diagrammes clairs. INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN et plus.

Optimisation des requêtes SQL : 15 techniques pour accélérer votre base de données

Maîtrisez l'optimisation des requêtes SQL avec 15 techniques : index, requêtes réécrites, partitionnement et vues matérialisées.

PostgreSQL vs MySQL 2026: Quel Base de Données Choisir?

Comparaison complète PostgreSQL vs MySQL 2026: performances, fonctionnalités, quand utiliser chacun.