About the SQL Formatter
A long SQL query written on one line hides its logic and makes mistakes easy to miss. This SQL formatter lays it out so it reads clearly: major clauses such as SELECT, FROM, WHERE and GROUP BY start on their own lines, join conditions line up under their joins, column lists are broken sensibly, subqueries are indented and keywords use consistent capitalization. Readable queries are easier to review, debug and share with colleagues, and they make it obvious when a join condition or filter is missing. Formatting does not change what the query does. For data exports, the CSV Viewer shows results clearly, and the JSON Formatter handles JSON output.
You decide how keywords are capitalised: all capitals, all lower case, or exactly as you typed them. Most teams pick capital keywords with lower-case table and column names, because the contrast lets you see where each clause begins without reading every word. Anything inside quotation marks, quoted identifiers such as "Order Date", and both kinds of SQL comment are recognised and copied across untouched.
Under the hood the tool reads the query as a stream of words and symbols instead of parsing it against one database's grammar. That is why it copes with PostgreSQL, MySQL, SQL Server and SQLite alike and never refuses an unfamiliar statement. It also means it cannot tell you whether the query will run. What it does is turn a four-hundred-character line into something a person can follow, which is normally the job that needs doing.
How to use the SQL Formatter
- Paste the SQLOne query or a script of several statements separated by semicolons.
- Set keyword case and indentationCapital keywords are the common convention, and you can choose the indent width.
- Run FormatThe query is laid out clause by clause below the input.
- Copy it backOnly spacing and keyword case have changed, so it can go straight back into your editor.
SQL Formatter features
- SELECT, FROM, WHERE, GROUP BY and the other main clauses start new lines
- Each join lined up with its ON condition
- Subqueries indented inside their parentheses
- Keywords in capitals, lower case or left as typed
- Quoted strings, quoted names and comments copied exactly
- Several statements in one go, split at the semicolons
- Copy the result or download it
Common uses
- Making sense of a query copied out of an application log
- Reviewing a colleague's change to a report query
- Cleaning up SQL before committing it to version control
- Reading the long query an ORM generated behind the scenes
- Showing students how a complicated query is put together
Formatting SQL in practice: a slow report query, reading EXPLAIN, reviewing migrations, placeholders, CTEs and sharing queries safely
SQL arrives in unreadable forms more often than not: one-line queries in slow query logs, generated SQL from frameworks and long reports pasted into chat. Formatting is the first step to understanding and improving them. This guide formats a slow report query, connects it to the database's query plan and covers reviewing migrations, placeholders that prevent injection, common table expressions, aliases, column lists and sharing queries safely.
A worked example: a slow report query
A shop's monthly sales report times out. The database's slow query log shows the culprit as a single 900-character line. Formatted, its structure is obvious: it joins the order meta table five times to fetch different fields, each join filtering on a text key with no suitable index. Rewriting the query to read those fields in one pass, and adding an index on the key column, brings the report from minutes to seconds.
Reading the query plan
Running the query with EXPLAIN in front shows how the database plans to execute it: which indexes it uses and how many rows it expects to examine in each table. A formatted query makes it easy to match each line of the plan to the join or condition it describes, and to spot the table being scanned in full.
Reviewing migrations and reports
When queries live in code, such as migrations, stored reports and views, one clause per line makes changes reviewable. A diff then shows that a single condition changed, rather than presenting a whole new line of SQL for reviewers to compare by eye. Formatting queries consistently before committing them keeps review focused.
Placeholders, not pasted values
Queries in applications should use placeholders, such as question marks or named parameters, with values supplied separately. Building queries by pasting user input into SQL text invites SQL injection, one of the most damaging and common security flaws. Formatting the query with its placeholders also makes it clear which parts are fixed and which come from users.
Common table expressions
Deeply nested subqueries are hard to follow even when formatted. Rewriting them as common table expressions, using WITH to name each step, such as monthly_orders and repeat_customers, turns a puzzle into a readable sequence. Most modern databases support them, and they often make performance problems easier to see.
Meaningful aliases
Short aliases such as a, b and c force readers to keep scrolling back to see which table each one means. Aliases that hint at the table, such as o for orders and c for customers, or full table names in short queries, make conditions understandable at a glance.
Listing columns
SELECT * returns every column, including ones added later, which can slow queries and break code expecting a fixed set of columns. Listing the needed columns documents what the query uses and lets the database read less data. Formatting the list one column per line makes additions and removals easy to review.
Sharing queries safely
Before pasting a query into a support ticket, forum or chat, replace customer names, phone numbers, emails and amounts in literal values with placeholders. Queries copied from logs often contain real personal data. Include the formatted query, the database type and version, and the EXPLAIN output to get useful help quickly.
Common mistakes
- Optimising queries without reading the query plan.
- Pasting user input into SQL instead of using placeholders.
- Using single-letter aliases in long queries.
- Sharing queries that contain customer data.
Related tools
To inspect exported query results, use CSV Viewer. For JSON columns and API data, try JSON Formatter, and to compare two versions of a query, Text Diff Checker.
Limit rows while exploring
When exploring an unfamiliar table, add LIMIT to queries so a mistake does not return millions of rows or slow a busy production server. Better still, explore on a copy of the database or a read-only replica, where heavy queries cannot affect customers.
Check before changing data
Before running an UPDATE or DELETE by hand, run a SELECT with exactly the same WHERE clause and check the number of rows. Then run the change inside a transaction, confirm the affected row count matches and only then commit. This simple routine prevents the classic mistake of updating every row in a table.
Date ranges
To select a whole day or month, use a half-open range: on or after the start, and before the start of the next period, such as created_at >= '2026-10-01' AND created_at < '2026-11-01'. BETWEEN with an end time of 23:59:59 can miss records with fractional seconds, while an end date of midnight wrongly includes the next day.
Comparing with NULL
In SQL, comparing a value with NULL using an equals sign is never true, so WHERE discount = NULL returns nothing. Use IS NULL and IS NOT NULL instead. Similarly, NOT IN with a list that contains a NULL returns no rows at all, a surprising result that formatted, carefully read queries help to catch.
Privacy
Formatting runs in your browser. Your queries are not uploaded or stored.
Frequently asked questions
Which databases does the formatter understand?
It recognises the keywords shared by standard SQL and the popular extensions, so queries written for PostgreSQL, MySQL, MariaDB, SQL Server, SQLite and Oracle all come out sensibly laid out. It does not check them against any one database's rules.
Can formatting change the results of my query?
No. The formatter only adds line breaks and indentation and adjusts the case of keywords. Table names, column names, values and the order of every part stay the same, so the database runs exactly the same query.
Why are capital keywords the usual choice?
They have been the convention for decades because they work: capital SELECT, JOIN and WHERE stand out against lower-case names, so the shape of the query is visible at a glance. If your team prefers lower case, switch the option.
Will text inside my quotes be changed?
No. Single-quoted strings and double-quoted or bracketed identifiers are detected and copied character for character, even when they contain words such as SELECT or characters such as semicolons that would otherwise be treated as SQL.
Does the tool check whether my query is correct?
No, it only lays the text out. Whether a query is valid, returns the right rows or is safe to run against production data is something only your database and a careful review can tell you.
Is my SQL sent to a server?
No. The formatting is done in your browser, which matters because real queries often contain table structures, customer values and business logic that should never be pasted into a third-party service.