DEV Community

楊東霖
楊東霖

Posted on • Originally published at devplaybook.cc

SQL Formatter Online: Beautify and Indent SQL Queries Instantly Free

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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
);
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

SQL Formatting by Dialect

Different databases have syntax variations a good formatter should handle:

Feature PostgreSQL MySQL SQLite SQL Server
Identifier quoting "name" `name` Both [name]
String concat `\ \ ` CONCAT()
Boolean literal TRUE/FALSE 1/0 1/0 1/0
Limit syntax LIMIT n LIMIT n LIMIT n TOP n
Interval INTERVAL '1 day' INTERVAL 1 DAY Function DATEADD

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
Enter fullscreen mode Exit fullscreen mode

Prettier (with prettier-plugin-sql)

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

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.


Related Tools


Level Up Your Dev Workflow

Found this useful? Explore DevPlaybook — cheat sheets, tool comparisons, and hands-on guides for modern developers.

🛒 Get the DevToolkit Starter Kit on Gumroad — 40+ browser-based dev tools, source code + deployment guide included.

Top comments (0)