Skip to content
Rene Saarsoo edited this page Oct 31, 2023 · 4 revisions

SQL formatting standards

Links to recources describing how to format SQL.

Tools

Other tools that perform SQL formatting.

  • sqlparse Python library and online formatter.
    (This one is really quite bad. The style is a bit like our tabularLeft, but with variable indentation. The formatting of CREATE TABLE is exceptionally bad.)
  • pgFormatter Perl library an online formatter for PostgreSQL. This looks pretty nice. Has several useful options. Handles comments. Though it doesn't do anything with long lines and the indentation is a bit of a mixed bag.
  • Instant SQL formatter online tool and VS plugin.
    Uses tabularLeft & tabularRight styles, but with 7 instead of 10 spaces.
  • Freeformatter.com a site with online formatters for many languages.
    Uses our standard style.
  • Code Beautify another site with multiple formatters.
    Uses our standard style.
  • SQL Complete a proprietary tool from Devart. Online version
    By default uses a compact version of our standard style, but has huge amount of configuration options.
  • SQL Formatter a proprietary tool from ApexSQL.
  • SQLinForm a proprietary tool (also free versions available)
  • SQL Pretty Printer a proprietary tool.
  • SQL Fluff A linter and formatter for SQL, written in Python. Online version.
  • SQL Prompt A proprietary tool, has an online demo of the formatter.
    Supports multiple distinct styles of formatting.

Here's some example SQL to test out various formatters:

SELECT
  supplier_name, city -- inline comment
  ,(select count(*) from people where supplier_id = s.id) as sup_count
FROM suppliers s left join addresses a on s.address_id=a.id
WHERE s.value>500 and a.city = 'New York'
ORDER BY supplier_name asc,city desc;

/* another comment in here */
INSERT INTO articles
(title, author, submission_date)
VALUES ('Learn SQL', 'John Doe', now());

UPDATE articles
SET author = 'Peter', submission_date = '2022-01-01'
WHERE title like '%by Peter';

CREATE TABLE articles (
  id int not null auto_increment,
  title varchar(100) not null,
  author varchar(40) not null,
  submission_date date,
  primary key ( id )
);