Skip to main content
← All Articles Β· Β· 6 min read

SQL Formatter Online: Beautify and Indent SQL Queries Instantly Free

Format messy SQL queries online for free. Beautify SELECT, JOIN, subquery, and CTE syntax with proper indentation. Supports PostgreSQL, MySQL, SQLite, and SQL Server.

sqlformatterdatabasepostgresqlmysqldeveloper-tools

SQL Formatter Online: Beautify and Indent SQL Queries Instantly Free

SQL is easy to write in a rush but painful to read later. A single-line query with nested JOINs and subqueries becomes unmanageable within a day. An online SQL formatter adds proper indentation, keyword capitalization, and consistent line breaks β€” making your queries readable, reviewable, and debuggable.


What Does a SQL Formatter Do?

A SQL formatter transforms unstructured SQL input like this:

select u.id,u.name,o.total from users u inner join orders o on u.id=o.user_id where u.active=true and o.total>100 order by o.total desc limit 20

Into clean, readable SQL like this:

SELECT
  u.id,
  u.name,
  o.total
FROM users u
INNER JOIN orders o
  ON u.id = o.user_id
WHERE
  u.active = TRUE
  AND o.total > 100
ORDER BY o.total DESC
LIMIT 20

Try it now: DevPlaybook SQL Formatter β€” paste any SQL query and get clean, indented output instantly.


Why Format Your SQL?

Code Reviews

Unformatted SQL is nearly impossible to review. Consistent formatting makes diffs readable and lets reviewers focus on logic, not layout.

Debugging

When a query returns wrong results, formatted SQL makes it easier to spot incorrect JOIN conditions, missing WHERE clauses, or wrong column references.

Documentation

SQL in README files, Confluence pages, and runbooks should be readable by anyone β€” even non-developers who need to understand what data is being accessed.

Collaboration

When multiple people work on the same database layer, consistent formatting prevents style disagreements and makes queries look like they came from one person.


SQL Formatting Conventions

Good SQL formatting follows these conventions:

Keyword Capitalization

-- βœ… Keywords uppercase, identifiers lowercase
SELECT id, name FROM users WHERE active = TRUE;

-- ❌ Inconsistent casing (hard to scan)
select id, name FROM users where Active = true;

One Column Per Line in SELECT

-- βœ… Readable
SELECT
  user_id,
  first_name,
  last_name,
  email,
  created_at
FROM users;

-- ❌ Compressed (hard to add/remove columns, review diffs)
SELECT user_id, first_name, last_name, email, created_at FROM users;

JOIN Alignment

-- βœ… JOIN conditions indented under the JOIN
SELECT u.name, o.total, p.name AS product
FROM users u
INNER JOIN orders o
  ON u.id = o.user_id
LEFT JOIN order_items oi
  ON o.id = oi.order_id
LEFT JOIN products p
  ON oi.product_id = p.id;

WHERE Clause Formatting

-- βœ… Each condition on its own line
WHERE
  u.active = TRUE
  AND o.total > 100
  AND o.created_at >= '2024-01-01';

-- For complex conditions, use parentheses explicitly
WHERE
  (u.role = 'admin' OR u.role = 'superadmin')
  AND u.active = TRUE;

How to Use an Online SQL Formatter

  1. Open DevPlaybook SQL Formatter
  2. Select your SQL dialect β€” PostgreSQL, MySQL, SQLite, SQL Server, etc.
  3. Paste your SQL query or script into the input box
  4. Click Format to apply consistent indentation and casing
  5. Review the formatted output
  6. Copy and paste it back into your codebase, documentation, or query editor

Formatting Complex SQL

CTEs (Common Table Expressions)

WITH
  active_users AS (
    SELECT id, name, email
    FROM users
    WHERE active = TRUE
  ),
  recent_orders AS (
    SELECT user_id, SUM(total) AS total_spent
    FROM orders
    WHERE created_at >= NOW() - INTERVAL '30 days'
    GROUP BY user_id
  )
SELECT
  au.name,
  au.email,
  COALESCE(ro.total_spent, 0) AS total_spent
FROM active_users au
LEFT JOIN recent_orders ro
  ON au.id = ro.user_id
ORDER BY total_spent DESC;

Subqueries

SELECT
  name,
  (
    SELECT COUNT(*)
    FROM orders
    WHERE user_id = u.id
  ) AS order_count
FROM users u
WHERE u.id IN (
  SELECT DISTINCT user_id
  FROM orders
  WHERE total > 500
);

CASE Expressions

SELECT
  id,
  name,
  CASE
    WHEN score >= 90 THEN 'A'
    WHEN score >= 80 THEN 'B'
    WHEN score >= 70 THEN 'C'
    ELSE 'F'
  END AS grade
FROM students;

SQL Formatting by Dialect

Different databases have syntax variations a good formatter should handle:

FeaturePostgreSQLMySQLSQLiteSQL Server
Identifier quoting"name"`name`Both[name]
String concat||CONCAT()||+
Boolean literalTRUE/FALSE1/01/01/0
Limit syntaxLIMIT nLIMIT nLIMIT nTOP n
IntervalINTERVAL '1 day'INTERVAL 1 DAYFunctionDATEADD

Choose the correct dialect in your formatter to get syntax-aware formatting.


Integrating SQL Formatting Into Your Workflow

VS Code

Install the SQLFluff or SQL Formatter extension for automatic formatting on save.

CLI (sqlfluff)

pip install sqlfluff
sqlfluff fix --dialect postgres my_query.sql

Prettier (with prettier-plugin-sql)

npm install --save-dev prettier prettier-plugin-sql
# Add to .prettierrc
{ "plugins": ["prettier-plugin-sql"], "language": "postgresql" }

Frequently Asked Questions

Does formatting SQL change how it executes?

No. SQL formatting only changes whitespace, indentation, and capitalization β€” none of which affect query execution or performance. The SQL engine ignores formatting. A minified query and a beautifully formatted query produce identical results.

What’s the difference between formatting SQL and linting SQL?

Formatting handles style (indentation, casing, spacing). Linting checks for semantic issues β€” using deprecated syntax, selecting SELECT *, missing aliases, or queries that might be slow. Tools like SQLFluff combine both. For quick formatting, an online tool is fastest.

Should SQL keywords be uppercase or lowercase?

Uppercase keywords (SELECT, FROM, WHERE) is the dominant convention and what most SQL style guides recommend. It provides visual contrast between keywords and identifiers. Some teams use lowercase for everything β€” consistency matters more than which you choose.

Can I format stored procedures and multi-statement SQL?

Yes. Good SQL formatters handle complete scripts with multiple statements, stored procedures, functions, triggers, and DDL (CREATE TABLE, ALTER TABLE). Paste the entire file and format it all at once.

How do I format SQL embedded in application code?

Use your language’s template literal or heredoc syntax to write SQL on multiple lines in source code, then let your code formatter handle indentation of the surrounding code. Keep SQL formatting separate from code formatting.


Free Download

50 CLI Commands That Save Hours Every Week

The exact Git shortcuts, file search tricks, Docker commands, and productivity hacks that senior developers use daily. Compiled from 5+ years of shipping production code.

  • Git mastery (10 cmds)
  • File & search wizardry
  • Docker & DevOps tricks
  • Process & network tools
  • Productivity boosters
  • macOS & Linux install guide

No spam. 1 email/week max. Unsubscribe anytime.

Found this guide useful? Check out our free developer tools.