Mazur’s SQL Style Guide

sql-style-guide.png

Even though I work primarily in Looker these days, I do spend a fair amount of time writing and reviewing SQL queries. Over time, I’ve experimented with and adopted various ways of styling my queries. Things like whether to use CTEs or subqueries (CTEs), single vs double quotes (single), how to format joins, and a lot more.

I’ve attempted to distill my preferences into a guide which you can check out on GitHub: Mazur’s SQL Style Guide.

Like with any style guide, you’ll have to consider which conventions you want to adopt. For example, you might write queries that use both single and double quotes and not give it a second thought, but feel strongly about columns always using snake_case and never camelCase. In the end, the key is readability, but opinions will differ on what’s readable and what’s not :).

For those of you writing a lot of SQL, hopefully you walk away from this guide with some things think about.

And like I mention in the guide, if you find yourself disagreeing strongly with any of my preferences, I’d love to chat. Please shoot me an email if do.

Last but not least, I have a Matt on Analytics newsletter where I’ll occasionally (very occasionally at the moment) share new content like this. If you’re interested in this kind of thing, you can sign up here.

One thought on “Mazur’s SQL Style Guide

  1. Excellent read. Like you say, I agree with a lot and disagree with some – the point is consistency camelCasing some things and WhatEvenis_this for others should be anathema.

    One other thought – I far prefer “where” clauses to be included in joins if it is ever possible. My reasoning is that this makes it clear what table(s) are being referenced and means if you modify the query you easily see when to remove them and have a nice prompt to get rid of no longer required tables. I’d value your thoughts!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s