Automate SQL Formatting with SQLFluff

Inconsistent SQL formatting and style errors can reduce code readability and make code reviews more difficult.

SQLFluff solves this problem by automatically linting and fixing SQL code formatting issues across multiple dialects, including ANSI, MySQL, PostgreSQL, BigQuery, Databricks, Oracle, and more.

To demonstrate, let’s create a sample SQL file named sqlfluff_example.sql with a simple SELECT statement.

# sqlfluff_example.sql
SELECT a+b  AS foo,
c AS bar from my_table

Next, run the SQLFluff linter on the sqlfluff_example.sql file using the PostgreSQL dialect.

$ sqlfluff lint sqlfluff_example.sql --dialect postgres

To fix the style errors and inconsistencies found by the linter, we can run the fix command.

$ sqlfluff fix sqlfluff_example.sql --dialect postgres
SELECT
    c AS bar,
    a + b AS foo
FROM my_table

Now, the SQL code is formatted and readable.

Link to SQLFluff.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Work with Khuyen Tran

Work with Khuyen Tran