// blog/developer/
Back to Blog
Developer · May 17, 2026 · 9 min read · Updated May 22, 2026

SQL Formatting and Query Optimization Made Simple

SQL Formatting and Query Optimization Made Simple

Every developer has encountered a SQL query that looks like it was written by someone having a bad day. No indentation, no line breaks, table aliases that are single letters with no discernible pattern, and subqueries nested four levels deep with no comments explaining why. You stare at it for five minutes, give up trying to understand it, and either rewrite it from scratch or pray it does not break.

SQL formatting is not about aesthetics. It is about maintainability. A well-formatted query can be understood in 30 seconds. A poorly formatted query with the exact same logic takes 10 minutes of careful reading. When you multiply that difference across a team of developers reading and modifying queries daily, formatting discipline saves significant time.

A Code Formatter takes your messy SQL and restructures it with consistent indentation, keyword capitalization, and line breaks. The logic does not change. The query produces the same results. But the human readability improves dramatically.

* * *

What Good SQL Formatting Looks Like

There is no single "correct" SQL formatting style, but the best styles share common principles.

Keywords on their own lines: SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY, and HAVING each start a new line. This makes the query structure visible at a glance.

Consistent capitalization: SQL keywords in uppercase (SELECT, WHERE, JOIN) and table/column names in lowercase or mixed case. This visual distinction makes it immediately clear which parts are SQL syntax and which parts are your schema.

Indentation for clauses: Columns in a SELECT list, conditions in a WHERE clause, and join conditions are indented one level beneath their parent keyword. Subqueries get an additional level of indentation.

Meaningful aliases: Use orders AS o and customers AS c, not t1 and t2. Short aliases are fine, but they should relate to the table name.

One condition per line in WHERE clauses: Each AND/OR condition gets its own line. This makes it trivial to comment out individual conditions during debugging.

A formatted query example:

`sql SELECT c.name, c.email, COUNT(o.id) AS order_count, SUM(o.total) AS lifetime_value FROM customers AS c JOIN orders AS o ON o.customer_id = c.id WHERE c.created_at >= '2025-01-01' AND o.status = 'completed' GROUP BY c.id, c.name, c.email HAVING COUNT(o.id) >= 3 ORDER BY lifetime_value DESC LIMIT 100; `

This query is immediately readable. You can see the selected columns, the join relationship, the filter conditions, the grouping logic, and the sort order without parsing a wall of text.

Database administrator reviewing query execution plans
Database administrator reviewing query execution plans
* * *

Common Query Performance Problems

Formatting makes queries readable. Optimization makes them fast. Here are the most common performance problems in SQL queries.

Missing indexes: The single biggest performance killer. If you are filtering or joining on a column that is not indexed, the database scans the entire table. A query that takes 5 seconds without an index might take 5 milliseconds with one. Check your WHERE and JOIN conditions and ensure the relevant columns have indexes.

SELECT *: Selecting all columns retrieves data you do not need, increasing I/O and memory usage. Always specify exactly the columns you need. This also prevents queries from breaking when new columns are added to the table.

N+1 queries: Running one query to get a list of items, then running a separate query for each item to get related data. This produces N+1 database round trips instead of a single JOIN query. Use JOINs or subqueries to fetch related data in one trip.

Functions on indexed columns: WHERE YEAR(created_at) = 2025 prevents the database from using an index on created_at. Rewrite as WHERE created_at >= '2025-01-01' AND created_at < '2026-01-01' to allow index usage.

Unnecessary DISTINCT: Adding DISTINCT to mask a query that produces duplicates (usually due to incorrect JOINs) hides the real problem and adds processing overhead. Fix the JOIN logic instead.

Large IN clauses: WHERE id IN (1, 2, 3, ..., 10000) performs poorly. Use a temporary table or a subquery instead.

Key takeaway

Formatting makes queries readable.

* * *

Understanding Query Execution Plans

Every database has a way to show you how it plans to execute a query. In PostgreSQL, it is EXPLAIN ANALYZE. In MySQL, it is EXPLAIN. In SQL Server, it is the execution plan view. Learning to read these outputs is the most valuable SQL skill you can develop.

The execution plan shows:

Scan type: "Seq Scan" (sequential scan, reads every row) is usually bad for large tables. "Index Scan" or "Index Only Scan" means the database is using an index efficiently.

Join method: "Nested Loop" works well for small result sets. "Hash Join" works well for large result sets. "Merge Join" works well when both sides are sorted. The database chooses automatically, but understanding the choice helps you optimize.

Estimated vs actual rows: A large discrepancy between estimated and actual rows indicates stale statistics. Run ANALYZE (PostgreSQL) or UPDATE STATISTICS (SQL Server) to refresh the statistics that the query planner uses.

Cost: Relative cost numbers help you identify the most expensive operations in a query. Focus optimization efforts on the highest-cost nodes.

The pattern for optimization is always the same: format the query for readability, run EXPLAIN to see the plan, identify the expensive operations, and then apply targeted fixes (add an index, restructure a join, rewrite a subquery). The Code Formatter handles step one so you can focus on the analysis.

* * *

SQL Formatting Tools vs IDE Plugins

You have several options for formatting SQL, each with different trade-offs.

Online formatters: Paste your SQL into a web tool, click format, copy the result. No installation, works from any device, but you are sending your SQL to a third-party server. For non-sensitive queries, this is the fastest option.

IDE plugins: Extensions for VS Code, JetBrains, and other IDEs format SQL as you type or on save. This is the most convenient option for daily work because formatting happens automatically without disrupting your workflow.

CLI tools: Tools like sqlfluff and pg_format can be integrated into CI/CD pipelines to enforce consistent SQL formatting across a team. They reject pull requests with improperly formatted SQL, similar to how linters work for application code.

Database-specific tools: pgAdmin, MySQL Workbench, and SQL Server Management Studio all have built-in formatters tuned to their specific SQL dialect.

For quick formatting of any code type, the Code Formatter supports SQL alongside other languages. A JSON Formatter handles the JSON data that often accompanies SQL queries in API development. And a CSS Minifier serves the reverse purpose for front-end code, where you want to compress rather than expand the formatting.

The best approach is an IDE plugin for daily work (automatic formatting) combined with a CI linter for team consistency (enforced formatting).

Code editor showing formatted SQL query with syntax highlighting
Code editor showing formatted SQL query with syntax highlighting
* * *

Query Optimization Techniques That Actually Matter

Beyond fixing obvious problems, several optimization techniques produce consistent performance improvements.

Use CTEs for readability, subqueries for performance: Common Table Expressions (WITH clauses) make complex queries readable, but some databases materialize CTEs as temporary results rather than inlining them. If a CTE is causing performance issues, try rewriting it as a subquery.

Batch operations: Instead of inserting 10,000 rows one at a time, use bulk INSERT with VALUES clauses or COPY commands. The difference can be 100x faster because you reduce network round trips and allow the database to optimize the write path.

Pagination with keyset instead of OFFSET: LIMIT 20 OFFSET 10000 requires the database to read and discard 10,000 rows before returning the 20 you want. Keyset pagination (WHERE id > last_seen_id ORDER BY id LIMIT 20) uses an index and performs consistently regardless of page depth.

Covering indexes: An index that includes all columns referenced in a query allows the database to answer the query entirely from the index without touching the table data. This is called an "index-only scan" and is significantly faster.

Partial indexes: If you frequently query a subset of data (WHERE status = 'active'), a partial index on that condition is smaller and faster than a full table index.

Connection pooling: Not a query optimization per se, but managing database connections through a pool (PgBouncer for PostgreSQL, for example) prevents the overhead of creating new connections for each query. A single connection establishment can take 50 to 100 milliseconds, which adds up in high-traffic applications.

* * *

FAQ

Does SQL formatting affect query performance?

No. SQL formatting (whitespace, line breaks, capitalization) has zero impact on query performance. The database parser strips all formatting before executing the query. Formatting is purely for human readability.

Should I uppercase SQL keywords?

It is a convention, not a requirement. SQL is case-insensitive for keywords. Uppercasing keywords (SELECT, WHERE, JOIN) helps visually distinguish SQL syntax from table and column names, which improves readability. Most teams adopt one convention and enforce it consistently.

How do I know if my query needs optimization?

If a query takes more than 100 milliseconds for a web application or more than a few seconds for a reporting query, it is worth investigating. Run EXPLAIN ANALYZE and look for sequential scans on large tables, high-cost operations, and large row estimates. Also monitor your application's slow query log to identify the queries that run most frequently or take the longest.

Is it better to use one complex query or multiple simple queries?

Generally, fewer queries are better because each query has network and parsing overhead. But a single extremely complex query with many joins can be harder to maintain and may perform worse than several simpler queries if the optimizer makes poor choices. The practical rule: start with a single well-structured query, and split it only if you have evidence that the complex version performs poorly.

What SQL formatter should I use for my specific database?

Most general-purpose SQL formatters handle standard SQL well. For database-specific syntax (PostgreSQL's JSONB operators, MySQL's backtick quoting, SQL Server's T-SQL extensions), use a formatter that supports your specific dialect. sqlfluff is dialect-aware and supports most major databases.

Server performance monitoring dashboard with query metrics
Server performance monitoring dashboard with query metrics