SQL Formatter

SQL (Structured Query Language) is the standard language for relational databases. This tool formats SQL queries with proper indentation, uppercases keywords, and structures complex queries for readability. Supports common SQL dialects.

Specifications

Common Use Cases

  • Format minified SQL from logs or ORM output
  • Make complex queries readable for code review
  • Prepare SQL for documentation
  • Debug and understand existing queries

Features

  • Pretty-print SQL with consistent indentation
  • Uppercase SQL keywords (SELECT, FROM, WHERE, etc.)
  • Format complex JOINs and subqueries
  • Handle CTEs (Common Table Expressions) with WITH
  • Support for window functions
  • 6 SQL dialect support (Standard, PostgreSQL, MySQL, MariaDB, SQLite, T-SQL)
  • Table name extraction
  • Minified view toggle
  • Size savings display (original vs minified)

Examples

Complex Query with JOIN

Try it →

A query with aggregation, join, and ordering.

SELECT
  u.id,
  u.name,
  COUNT(o.id) AS order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.active = true
GROUP BY u.id, u.name
HAVING COUNT(o.id) > 5
ORDER BY order_count DESC

Tips

  • Format queries before committing to version control.
  • Uppercase keywords improve readability but are not required by SQL.
  • Subqueries should be indented for clarity.

Understanding SQL

SQL (Structured Query Language) has been the standard language for relational databases since the 1970s. It provides a declarative syntax for querying, manipulating, and managing data across database systems including PostgreSQL, MySQL, SQLite, SQL Server, and Oracle. While each database adds proprietary extensions, the core language is remarkably consistent.

SQL statements fall into several categories. Data Manipulation Language (DML) includes SELECT, INSERT, UPDATE, and DELETE for reading and modifying data. Data Definition Language (DDL) includes CREATE, ALTER, and DROP for managing database objects. Data Control Language (DCL) handles permissions with GRANT and REVOKE. Modern SQL also supports Common Table Expressions (CTEs) with WITH clauses, window functions for analytics, and JSON operations for semi-structured data.

Formatting SQL is essential for readability and maintenance. A complex query with joins, subqueries, and aggregations can become incomprehensible when compressed to a single line — common in ORM-generated queries, application logs, and database monitoring tools. Consistent formatting with uppercased keywords, proper indentation, and aligned clauses makes queries easier to review, debug, and optimize.

SQL formatting also aids performance analysis. Well-formatted queries make it easier to identify missing indexes (by seeing which columns appear in WHERE and JOIN conditions), spot unnecessary subqueries that could be rewritten as joins, and understand the logical flow of complex queries involving CTEs and window functions.

SQL is case-insensitive for keywords, so SELECT and select are treated identically by the database engine. Uppercase keywords (SELECT, FROM, WHERE, JOIN) are a widely followed convention that improves readability by visually distinguishing SQL keywords from table and column names. Most formatters and style guides recommend uppercase keywords for this reason.

A Common Table Expression (CTE) is a named temporary result set defined with the WITH keyword that exists for the duration of a single query. CTEs improve readability by breaking complex queries into named steps, replacing deeply nested subqueries with a sequential, top-to-bottom structure. Recursive CTEs extend this capability to traverse hierarchical data like organizational charts or threaded comments.

Core SQL syntax (SELECT, JOIN, WHERE, GROUP BY) is consistent across database dialects, but differences appear in proprietary features. MySQL uses backticks for quoting identifiers, PostgreSQL uses double quotes, and SQL Server uses square brackets. Functions, data types, and advanced features like LATERAL joins, MERGE, and UPSERT also vary by dialect. This formatter handles standard SQL syntax that works across all major databases.

← Back to all tools