Nat TaylorBlog, Product Management & Tinkering

An ode to sqlfmt

Published on . Updated on

A colleague shared that I write the “cleanest and most understandable SQL queries he’s ever seen” and here’s my secret: It’s a SQL formatter that makes everything lowercase with pretty identation which I have adopted and am now advocate for. You might be wondering: “how much ad hoc SQL do you write?” …and that is an issue for another day, because the point of this story is that I’ve found tremendous value in consistently formatted SQL.

It only took a pip install then about a half-day to fully commit, and now when I see YELLING KEYWORDS it makes me realize how much I like lowercase. The most important bit to my workflow is an Espanso shortcut :fmt (below) so that I can use sqlfmt anywhere I write SQL (be it an example, an ad hoc Looker query, a Slack message or within code).

I suppose it’s no different from any other formatter, but it’s very freeing to just freely write SQL without thinking about the formatting, then knowing it will turn out tidy and consistent. It is especially rewarding to know that colleagues also get value from this consistency.

One thing that tripped me is the potentially query-breaking handling of Snowflake dot notation where foo:Bar becomes foo:bar, which will break your query. Fix this by using quotes (eg foo:"Bar“)

Give it a try!

Here’s my Espanso rule. I select the query, cut it onto my clipboard and type :fmt

- trigger: ":fmt"
  replace: "{{output}}"
    - name: output
      type: shell
        cmd: "echo $(pbpaste) | sqlfmt -"

Popular Posts

Post Navigation