SQL style guide?

I'd like to start enforcing SQL style for our charts to make it easier for our analysts to jump between each other's reports.  

Does anyone have SQL style guide they recommend?  We actually use similar to what Periscope uses in their blog posts -- all lower case, 2 space indent, each select and where condition on their own line, etc -- so I'm hoping to find one that uses that same style.

5replies Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
  • Hi Mark!

    I will look into this tomorrow, but are you familiar with cmd/ctrl-shift-L to automatically format your code in Periscope Data? If not, prepare to be amazed. :)

    Internally, we generally use that as our "style guide", with the exception that sometimes we condense case statements a bit.

    Reply Like 1
  • Wow very cool, I did not know about that.  I don't know if I could get used to that comma placement, but otherwise its very close to what we already use.  

    Reply Like
      • Sean Cook
      • Product Analyst @ Facebook
      • Sean_Cook
      • 1 yr ago
      • Reported - view

      Mark Lampert I definitely hear that, being a late convert to leading commas, but I highly recommend giving leading commas a shot.
       

      • You'll find it much easier to read, comment out and parse code, especially with multi-line columns like case statements or nested queries.
      • With trailing commas commenting or removing out the last column name/group by/order by breaks the query. With leading commas it does not.
         

      Broken:

      select
       a,
       b,
        -- c,
      from table

      Works:

      select
       a
       , b
       -- , c
      from table
      Reply Like
  • I quite like the Periscope Formatter, with exceptions to some treatments of functions and parentheses, it's good enough that we don't need to enforce tabs/spaces or the syntactical boring stuff.

    That said, for the more abstract part of style, I like to point analysts towards this blog on naming in programming.

    http://arlobelshee.com/good-naming-is-a-process-not-a-single-step/

    I think it captures the hard part of style, like what things go in what chunks, and what to call those chunks. So temporary table / CTE names, column names, calculated field names, and the order in which you spin those up.

    There isn't a great way to write all that stuff down aside from the abstract process he lays out, which we mostly enforce through making sure new analysts are learn SQL from only a few sources internally, and constantly reviewing each other's code to make those tweaks.

    Reply Like
  • Mark Lampert We really like this style guide (in addition to the Periscope formatter) :

    http://www.sqlstyle.guide/

    There's also a style guide from Kickstarter with great examples of dos and don'ts: https://gist.github.com/fredbenenson/7bb92718e19138c20591

    Reply Like 1
Like Follow
  • 1 yr agoLast active
  • 5Replies
  • 776Views
  • 4 Following