SQL style guide by Simon Holywell

(sqlstyle.guide)

23 points | by thunderbong 3 hours ago

9 comments

  • hcarvalhoalves 1 hour ago
    I’m probably alone in this, but I dislike naming tables in plural.

    IMO, reading “SELECT employee.first_name” makes much more sense than “SELECT staff.first_name”.

    • ammojamo 54 minutes ago
      You are not alone at all, I also prefer singular names for the same reason. I reserve plural names for the rare cases where the single row of a table actually contains information about more than one item, which is usually when I'm doing something denormalized or non-relational e.g. CREATE TABLE user_settings ( user_id INT, settings_data JSON)
    • other_herbert 51 minutes ago
      You can always alias to a singular … like

      join users as user on user….

      Then do as you please without the that if you are dealing with a user or leave it plural if multiple…

      And if we’re talking personal preference I really dislike caps in reserved words in sql, even before highlighting was everywhere it still just feels archaic for no good reason

    • croes 43 minutes ago
      According to the guide it would be e.first_name or s.first_name.
    • hackernewds 1 hour ago
      Yes, you are indeed alone in this
  • gnabgib 3 hours ago
    Page title: SQL Style Guide, discussions in:

    2018 (59 points, 16 comments) https://news.ycombinator.com/item?id=17924917

    2016 (257 points, 147 comments) https://news.ycombinator.com/item?id=12671667

    2015 (16 points, 10 comments) https://news.ycombinator.com/item?id=9941150

  • harterrt 53 minutes ago
    For comparison, here’s Mozilla’s SQL style guide: https://docs.telemetry.mozilla.org/concepts/sql_style
    • yen223 8 minutes ago
      So much cleaner in my eyes, plus it uses constant-sized indents, which means less futzing about with spaces and all that.

      Also means you can comment out the first select item, something you can't do with the article's approach.

    • cwbriscoe 51 minutes ago
      I am definitely not a fan of that style. Wastes too much vertical space without much benefit.
  • Sn0wCoder 1 hour ago
    Not bad advice. The one about “where possible avoid simply using id as the primary identifier for the table” stood out to me. In the past with multiple ORMs (ya, ya, we all hate them) the default was to map to a column named id. Also when doing joins its cleaner to use the table_name.id or alias.id then table_name.table_name_id or alias.table_name_id or whatever else besides id is used. The best is when multiple people have worked on the project over the years and the columns are a combo of camel, snake, camel_snake, all UPPER / lower. Must look at the table definitions or ERD every time you want to write some non-trivial query. So having a consistent style guide is better than having any one specific style guide. This would be a good starting point and adjust with your team as needed.
    • jpnc 12 minutes ago
      > Also when doing joins its cleaner to use the table_name.id or alias.id then table_name.table_name_id or alias.table_name_id or whatever else besides id is used However, using 'table_name.table_name_id' and then having another table with an FK that references it with the same name i.e. 'table_2.table_name_id' allows you to use a shorthand 'USING' clause instead of 'ON' in databases that support it.
    • psadri 1 hour ago
      I have found that naming ids as <thing>_id helps downstream code when trying to figure out which thing's id you are dealing with. It also helps with avoiding renaming fields when a structure contains multiple ids.

      I do agree it makes joins more verbose.

  • jkubicek 1 hour ago
    I've stopped using aliases in the SQL I write and it's dramatically increased the clarify.

    From this style guide, the aliases section would look like this in my style guide:

        SELECT first_name
        FROM staff
        JOIN students
          ON students.mentor_id = staff.staff_num;
    • Sn0wCoder 1 hour ago
      If the table names are all short one word like that, they are already basically aliases. What do you do when you end up on some legacy project (you did not make the schema) where_the_table_names_look_like_this? Seems some sort of alias might be more appropriate. Also, when you are writing longer / sub queries or using Common Table Expressions it’s impossible to not use an alias.
  • cwbriscoe 53 minutes ago
    This is really good advice and the coding style (alignment) matches what I came to without any real guidance when I was learning SQL 20+ years ago. The only thing I slack on, is uppercasing the keywords. I hate switching case so much. But, I will fit the coding style of the codebase I am working on when it comes to that.
  • croes 40 minutes ago
    I‘m not a fan of upper case keywords especially when there is also syntax highlighting that gives them a unique color.

    Shifts my focus away from the rest of the query.

    • jbverschoor 35 minutes ago
      SQL keywords have been upper case for decades. I prefer it because it is faster to match visually.

      Just like I don’t like uppercase paragraphs because of the same reason

  • hackernewds 1 hour ago
    Great document to feed to GPT while ensuring it writes code :)