SQL is a very flexible language when it comes to formatting. As long as the commands and syntax are in the right order then the SQL query will work. Whitespace or newlines (or even capitalisation) have no impact on the output of a query.
With this being the case there are almost infinite style preferences different teams and companies have when it comes to formatting their SQL queries.
For example the following SQL queries will all return equivalent results:
-- version 1
SELECT foo, bar, cat FROM my_amazing_table;
-- version 2
SELECT
foo, bar, cat
FROM my_amazing_table;
--or even version 3
select
foo
,bar,
cat FROM
my_amazing_table;
SQLfluff is a great tool for ensuring a consistent style preferences are across SQL queries. It works well with a variety of SQL dialects such as BigQuery and Databricks.
SQLfluff also plays nice with pre commit. This means it is possible to set up SQLfluff to format any .sql
file that is being committed into a git repository. The following steps will show how to set this up with uv.
##############################
# 0 - Install UV if it is not already installed
##############################
# mac
curl -LsSf https://astral.sh/uv/install.sh | sh
# windows
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
##############################
# 1 - Install uv
##############################
uv tool install pre-commit --with pre-commit-uv
##############################
# 2 - navigate to root of git repo
##############################
cd some/path/to/root/directory/ot/git/repository
##############################
# 3 - add pre commit files and install any dependencies
##############################
# Add the following code to an existing __ file
# or create it with the following code inside:
repos:
- repo: https://github.com/sqlfluff/sqlfluff
rev: 3.4.2 # Use the latest stable version
hooks:
- id: sqlfluff-lint
additional_dependencies: # dbt extras for templating and your dialect
- 'sqlfluff-templater-dbt'
- 'dbt-databricks==1.7.2' # For Databricks; match to your dbt version (check with dbt --version)
args: [--dialect, bigquery]
types: [sql] # Only run on .sql files
# delete the code block below if you do not want sqlfluff to automatically try and fix SQL formatting rules that have been broken.
- id: sqlfluff-fix # Optional: Auto-fix fixable issues (e.g., indentation)
args: [--dialect, bigquery]
types: [sql]
# and then run
pre-commit install
Now when an sql file file is being commited into the repository the pre commit tool will check if the code is formatted according to SQLfluffs rules. If it is then the commit will be allowed. If not then the commit will not go forward and the sql file will have the formatting rules automatically applied to the file.