SQL Formatter

0.6.2 · active · verified Sun Apr 12

sql-formatter is a Python library designed to format SQL queries, enhancing readability and quick understanding through consistent indentation and casing. It aims to standardize SQL query writing, similar to how 'black' formats Python code. The library provides both a command-line interface and a Python API, and is actively maintained with frequent bug fixes and feature enhancements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import the `format_sql` function and use it to format a multi-line SQL string.

from sql_formatter.core import format_sql

example_sql = """
create or replace table mytable as -- mytable example
seLecT a.asdf, b.qwer, -- some comment here
c.asdf, -- some comment there
b.asdf2
frOm table1 as a
leFt join table2 as b -- and here a comment
on a.asdf = b.asdf -- join this way 
"""

formatted_sql = format_sql(example_sql)
print(formatted_sql)

view raw JSON →