Loading SQL Formatter...
Please wait a moment

How to Format SQL - Step by Step Guide

Step 1

Input Your SQL Query

Let's get your SQL query into the formatter! Whether you're working with MySQL, PostgreSQL, SQL Server, Oracle, or SQLite databases, need to format JSON, or convert data from CSV to JSON, you have several convenient options:

•
Paste directly: Copy your SQL query from your database client or IDE and paste it into the input editor
•
Upload a file: Click "Upload" to select a .sql or .txt file from your computer
•
Try the sample: Click "Sample" to load an example query and see how the formatter works

Example: Unformatted SQL Input

Here's what messy, unformatted SQL looks like:

SELECT u.user_id,u.username,u.email,COUNT(o.order_id) AS total_orders FROM users u LEFT JOIN orders o ON u.user_id=o.user_id WHERE u.status='active' GROUP BY u.user_id ORDER BY total_orders DESC;
Step 2

Automatic Formatting

The magic happens instantly! As soon as you input SQL, the formatter automatically:

•
Catches broken input: A quote or comment that never closes is reported with its line and column
•
Adds proper indentation: Makes complex queries with joins and subqueries easy to read
•
Syntax highlighting: Color-codes keywords, functions, and strings for better readability
•
Uppercase keywords: Converts SQL keywords to uppercase following best practices

Example: Beautifully Formatted Output

The same SQL query, now properly formatted and readable:

SELECT
  u.user_id,
  u.username,
  u.email,
  COUNT(o.order_id) AS total_orders
FROM
  users u
  LEFT JOIN orders o ON u.user_id = o.user_id
WHERE
  u.status = 'active'
GROUP BY
  u.user_id
ORDER BY
  total_orders DESC;
Step 3

Choose Format Options

Customize your output based on your needs! The formatter gives you control over how your SQL appears:

•
Pretty format: Default beautified SQL with proper indentation (2 spaces) and uppercase keywords
•
Minify option: Click "Minify" to compress SQL into compact format by removing extra whitespace
•
Syntax highlighting: Color-coded keywords make it easy to identify different SQL elements
Step 4

Export Your Formatted SQL

Get your perfectly formatted SQL ready for use! Multiple export options make it easy to integrate into your workflow:

•
Copy to clipboard: One-click copying for quick pasting into your database client or application
•
Download as file: Save as a .sql file for later use or sharing with your team
•
Status badge: Shows Formatted or Minified once the output matches your current input. It is not a syntax check against your database

Frequently Asked Questions

How do I format messy SQL queries?

Paste the query into the input editor and the formatted version appears on the right about half a second after you stop typing. Each clause (SELECT, FROM, WHERE, GROUP BY and so on) starts on its own line, columns are indented by two spaces and keywords are written in uppercase.

Does it work with all SQL databases?

It reads standard SQL first. If the query uses syntax only one database understands, such as PostgreSQL $$ bodies and :: casts, SQL Server [bracketed names] or MySQL # comments, it tries the PostgreSQL, MySQL, SQL Server and SQLite rules in turn. Very unusual vendor syntax can still fail, and then you get an error with the line and column instead of a half-formatted query.

Can formatting change what my query does?

It should not. Only spacing, line breaks and the case of SQL keywords change. Text inside quotes, quoted column names, $$ bodies and numbers such as 8901240544102066246, 14.50 or the string 00501 come out exactly as you typed them. Before showing a result the page compares all of those against your input, and if anything differs it shows an error instead.

Can I both format and minify SQL in the same tool?

Yes. The output is formatted by default. Click Minify to put the query on as few lines as possible: whitespace and comments between tokens are removed, while strings and quoted names stay exactly as written. Editing the input switches back to the formatted view.

Can I format SQL files with multiple queries?

Yes. Statements separated by semicolons are formatted one after another with a blank line between them, and you can upload a .sql or .txt file instead of pasting. Everything runs in your browser, so very large files mostly depend on how fast your device is. You can also use our JSON to Table tool to look at query results.

Is the SQL formatter free?

Yes, it is free and needs no account. Your SQL is formatted in the browser and is not sent to our server. For JSON data there is a separate JSON Formatter.

Related: JSON to Table for query results, and the JSON Formatter for JSON data.