dev cosmos/ blog/ sql-formatter-guide

SQL Formatter: Write Readable Queries That Teams Actually Understand

From chaotic single-line dumps to beautifully structured SQL — formatting conventions, keyword standards, and practices that make every query maintainable.

SQL

SQL Formatter

Format · Beautify · Standardise

Raw SQL from ORMs, query logs, or copy-pasted examples often looks like a wall of text. Unreadable queries slow down code reviews, hide bugs, and make onboarding painful. Formatting your SQL is one of the highest-ROI habits a developer can build — and the Dev Cosmos SQL Formatter does the heavy lifting instantly.

SQL
Open in Dev Cosmos
SQL Formatter & Beautifier →

Why SQL Formatting Matters

Consider this query pulled from a production log:

-- Before formatting
select u.id,u.first_name,u.last_name,u.email,count(o.id) as order_count,sum(o.total) as total_spent from users u left join orders o on u.id=o.user_id where u.is_active=1 and u.created_at>='2024-01-01' group by u.id order by total_spent desc limit 50

And after running through the formatter:

-- After formatting
SELECT
    u.id,
    u.first_name,
    u.last_name,
    u.email,
    COUNT(o.id) AS order_count,
    SUM(o.total) AS total_spent
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE
    u.is_active = 1
    AND u.created_at >= '2024-01-01'
GROUP BY u.id
ORDER BY total_spent DESC
LIMIT 50

The logic is identical. The second version takes seconds to parse mentally; the first can take minutes.

Formatter Options Explained

Keyword Casing

The SQL standard doesn't mandate case for keywords, but every team should pick one and stick to it:

ModeExampleBest For
UPPERCASESELECT FROM WHEREMost common, maximum keyword visibility
lowercaseselect from wherePreferred by some PostgreSQL teams
TitleCaseSelect From WhereRare — avoid for consistency reasons
💡
Team Recommendation
Stick with UPPERCASE keywords. It creates visual separation between your SQL keywords (what the engine does) and your identifiers (your table/column names), making queries scan much faster.

Indentation

The formatter supports 2 spaces, 4 spaces, or tabs. Four spaces is the most common SQL convention and produces alignment that's easy to follow. Tabs work well if your team uses them for all code.

Dialect

Different databases have slightly different syntax. The formatter respects dialect-specific keywords:

  • Standard SQL — ANSI SQL, safe for any database
  • PostgreSQL — Includes ILIKE, RETURNING, NULLS FIRST/LAST
  • MySQL — Handles backtick identifiers, LIMIT x, y syntax
  • SQL Server — Recognises TOP, NOLOCK, T-SQL keywords

A Practical SQL Style Guide

One Major Clause Per Line

Always start SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY, and HAVING on their own lines at zero indentation. This creates a visual scaffold that makes the query structure immediately obvious.

Indent AND / OR

Place each condition on its own line, indented once, with AND/OR at the start of the line:

WHERE
    status = 'active'
    AND created_at >= '2024-01-01'
    AND (role = 'admin' OR role = 'manager')

Use Meaningful Aliases

Single-letter aliases like u for users are fine in short queries but become confusing in complex ones with many joins. Consider full aliases: users AS usr, orders AS ord.

Prefer CTEs Over Nested Subqueries

Complex logic nested in subqueries is notoriously hard to read. Common Table Expressions (CTEs) with WITH give each logical step a name:

-- Hard to read: nested subquery
SELECT * FROM (
    SELECT user_id, SUM(total) AS revenue
    FROM orders
    GROUP BY user_id
) ranked WHERE revenue > 1000;

-- Better: named CTE
WITH user_revenue AS (
    SELECT user_id, SUM(total) AS revenue
    FROM orders
    GROUP BY user_id
)
SELECT * FROM user_revenue
WHERE revenue > 1000;

Common SQL Formatting Mistakes

MistakeProblemFix
SELECT *Fetches unused columns, breaks when schema changesAlways name columns explicitly
Mixed case keywordsSELECT from WHERE → confusing to scanPick one case, use it everywhere
No aliases on aggregatesCOUNT(id) unnamed → unclear column nameAlways use AS count_name
Inline comments in prod queriesSome drivers strip or choke on commentsUse comments in dev, strip for prod
⚠️
Never Use SELECT * in Production
Beyond performance, SELECT * breaks code silently when columns are added, reordered, or renamed. Always enumerate the columns you need.

Formatting Workflow

  1. Write or paste the query into the Dev Cosmos SQL Formatter
  2. Choose your team's keyword casing and indent size
  3. Select the correct dialect for your database
  4. Click Format or press Ctrl+Enter
  5. Copy the formatted result back into your codebase
SQL
Open in Dev Cosmos
SQL Formatter & Beautifier →
More from the Blog