Home › Developer Tools › SQL Formatter & Beautifier

SQL Formatter & Beautifier

Paste a long or messy SQL query and get it neatly indented with consistent keyword case, or minify it into one compact line. Pick your database dialect for accurate results. Your SQL never leaves your browser.

Last updated:

Advertisement
Advertisement

How to use the SQL Formatter & Beautifier

  1. Paste your SQL into the left box or press Upload .sql to open a file.
  2. Choose the dialect of your database so that its quoting, operators and keywords are recognised.
  3. Set the style: keyword case, indent and the number of blank lines between queries.
  4. Press Format SQL. The formatted query appears on the right and updates as you type.
  5. Press Minify for a compact one-line version, then Copy or Download the result.

SQL formatting conventions

Most SQL style guides, including the widely used one by Simon Holywell (sqlstyle.guide), agree on a few habits that make queries easier to scan and review:

  • One clause per line: SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY and LIMIT each start a new line, so you can see the shape of the query at a glance.
  • One column per line in long SELECT lists, which keeps diffs small when a column is added or removed.
  • Indent sub-queries and CTE bodies one level deeper than the statement around them.
  • Keywords in one case (usually uppercase) and table or column names in snake_case.
  • Explicit joins: write INNER JOIN … ON instead of listing tables with commas and joining in WHERE.

Quoting and syntax differences between dialects

DialectQuoted identifierString escapesNotable syntax
Standard SQL"name"'it''s'FETCH FIRST n ROWS ONLY
MySQL / MariaDB`name`'it\'s' or 'it''s'LIMIT n, # comments, DELIMITER in scripts
PostgreSQL"name"'it''s', E'it\'s', $$it's$$:: casts, RETURNING, ILIKE
SQL Server (T-SQL)[name] or "name"'it''s', N'…' for UnicodeTOP n, GO batch separator
Oracle PL/SQL"NAME"'it''s', q'[it's]'BEGIN … END; blocks, ROWNUM
SQLite"name", [name], `name`'it''s'Flexible typing, LIMIT n OFFSET m
BigQuery`project.dataset.table`'it\'s', '''multi-line'''STRUCT, ARRAY, QUALIFY
Snowflake"name"'it''s', $$…$$QUALIFY, FLATTEN, semi-structured : paths

Before and after

A one-line query copied from application logs:

select u.id,u.name,count(o.id) as orders from users u left join orders o on o.user_id=u.id where u.active=1 group by u.id,u.name having count(o.id)>5 order by orders desc

After formatting with uppercase keywords and a 2-space indent:

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

The logic is identical, but the grouping, the join condition and the filter on the aggregate are now obvious, which makes reviewing and debugging far quicker.

Tips for long queries

  • Prefer CTEs to deep nesting. A WITH clause names each step of the logic, so a query reads from top to bottom instead of from the innermost subquery outwards.
  • Alias every table in a join and prefix every column with its alias, so readers never have to guess where a column comes from.
  • Comment the why, not the what. A short -- exclude test accounts above a filter explains intent that the code cannot.
  • Format before you review. Running both versions of a query through the same formatter turns a confusing diff into a clear one that shows only the real changes.

Frequently asked questions

Why does the dialect matter?

Databases quote and escape differently. MySQL uses backticks for identifiers and allows backslash escapes in strings, T-SQL uses [square brackets], PostgreSQL has $$ dollar-quoted strings and :: casts, and BigQuery allows triple-quoted strings. Choosing the right dialect makes the formatter read these correctly instead of reporting an error.

Does formatting change what my query does?

No. Only whitespace, line breaks and, if you choose, the letter case of keywords are changed. Keywords are case-insensitive in SQL. Strings, quoted identifiers and comments are left exactly as written.

What does Minify do?

It removes comments and collapses all whitespace outside strings and quoted identifiers into single spaces, giving a compact one-line query that is handy for logs, code strings or URLs. Optimizer hints (/*+ … */) and MySQL version comments (/*! … */) are kept because they change behaviour.

Why do I get a parse error?

The formatter needs to understand the structure of your query. Errors usually come from a missing quote or bracket, the wrong dialect, or template placeholders such as {{name}}. The message shows the line and column where reading stopped. Fix that spot or switch dialect and try again.

Should SQL keywords be uppercase?

It is a convention, not a rule. Uppercase keywords (SELECT, FROM, WHERE) make it easy to tell keywords from table and column names, which is why most style guides use them. Some teams prefer lowercase for less visual noise; choose whichever your team uses.

Is my query sent to a server?

No. The formatting library runs in your browser after it is downloaded once from a public CDN. Your SQL, which may contain table names or data, is never uploaded.

Are all the tools on FreeToolCabin tested for quality and accuracy?

Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.

Related tools

JSON FormatterBeautify, minify and validate JSON, with a collapsible tree view.XML FormatterBeautify, validate and minify XML with a collapsible tree view.HTML FormatterBeautify and indent HTML code with a live preview.CSS FormatterBeautify CSS with indent controls and optional property sorting.Code Diff CheckerCompare two code files side by side or unified, with syntax colors and changes highlighted.Cron Expression GeneratorBuild crontab schedules visually, read them in plain English and see the next run times.CSS MinifierMinify CSS with csso or a built-in engine and see the size saved instantly.GraphQL FormatterPretty-print and indent GraphQL queries, mutations and schemas, or minify them, instantly.ZIP File CreatorCompress files and folders into a ZIP.HTML MinifierMinify HTML: strip comments and whitespace safely and see how many bytes you save.JavaScript MinifierMinify JavaScript with Terser: compress, mangle and see how many bytes you save.JSON MinifierMinify and validate JSON, sort keys and see exactly how many bytes you save.IPv6 Expander & CompressorExpand IPv6 addresses to full form or compress them per RFC 5952, one or many at once.CSS ValidatorValidate CSS: syntax errors, unknown properties and invalid values by line, plus auto-fix.Gzip Compress / DecompressGzip or deflate text and files, get Base64 output, and decompress Base64 or .gz files.IBAN ValidatorCheck an IBAN's mod-97 checksum, country and length, and see its bank code and account parts.JavaScript Syntax CheckerCheck JavaScript for syntax errors with line and column, for any ES version, script or module.JSON Schema ValidatorValidate JSON against a JSON Schema (draft-07, 2019-09, 2020-12) with clickable error paths.Markdown FormatterBeautify Markdown with Prettier: list markers, prose wrap, aligned tables and live preview.PHP FormatterBeautify PHP code with Prettier PHP: PSR-12 style, indent and brace options, syntax errors.TypeScript FormatterFormat TypeScript and TSX code with Prettier: width, tabs, semicolons and quotes options.XML MinifierMinify and validate XML: strip whitespace and comments, keep CDATA, see the size savings.C# FormatterBeautify C# code online: Allman or K&R braces, indent size and line width, clang-format option.HTML ValidatorCheck HTML for errors and warnings with line numbers, locally or with the W3C Nu checker.Java FormatterBeautify Java code online: indent size, brace style and line width, with clang-format option.JavaScript ObfuscatorObfuscate JavaScript with string encoding, renaming and control-flow flattening presets.Phone Number FormatterFormat and validate lists of phone numbers to E.164, international or national style.Python FormatterFormat Python code in Black/Ruff style: line length, quote style, indent, copy or download.Responsive Design CheckerPreview any website at phone, tablet, laptop and desktop sizes side by side, with rotate and zoom.Browser Compatibility CheckerSearch CSS, JavaScript and HTML features and see which browser versions support them.Core Web Vitals CheckerCheck real-user LCP, INP and CLS for any URL or site and see if it passes Core Web Vitals.Heading Structure CheckerPaste HTML to see its H1-H6 outline and catch missing H1s, multiple H1s and skipped levels.Website Hosting CheckerFind out who hosts a website: hosting provider, IP addresses, ASN, network owner and location.Routing Number ValidatorCheck a US ABA routing number's checksum and format and see its Federal Reserve district.Spam Word CheckerScan an email subject and body for spam trigger words, excess caps, exclamation marks and risky links.Check Digit CalculatorCalculate or validate check digits for UPC, EAN, GTIN, SSCC, ISBN, ISSN and mod 11 codes.Luhn Algorithm CheckerValidate numbers with the Luhn mod 10 check or calculate the missing check digit, step by step.XSD ValidatorValidate XML against an XSD schema in your browser and see each error with its line number.Strftime Format BuilderBuild Python and C strftime format strings by clicking tokens, with a live preview and reference.
See all 41 Code Formatters & Validators →