-
Notifications
You must be signed in to change notification settings - Fork 407
Formatting
Rene Saarsoo edited this page Oct 31, 2023
·
4 revisions
Links to recources describing how to format SQL.
- StackOverflow: SQL Formatting standards
- StackOverflow: What SQL coding standard do you follow?
- StackOverflow: SQL Statement indentation good practice
- How to indent SQL? The definitive guide to simple and effective indentation
- 24 Rules to the SQL Formatting Standard
- How to Properly Format SQL Code
- SQL style guide by Simon Holywell
- Transact-SQL Formatting Standards
- Gitlab SQL style guide
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 )
);