Hacker News new | ask | show | jobs
by arh68 2329 days ago
This is my favorite guide yet!

My syntax, like others, is a little different (lowercase, 2 spaces, commas-first, bracket quotes, ons right under joins w/ joined table on LHS, left joins left-aligned): (this query isn't supposed to make sense)

    select
      u.id                   [user]
      , u.email              [email]
      , o.name               [office]
      , sum(t.id)            [# things]
    from main_tblusers_db u
    inner join tbloffices_db o
            on o.id = u.office_id
    inner join things_tbl t
            on t.user_id = u.id
    left  join example e
            on e.user_id = u.id
    where
      u.deleted is null
      and (
        u.active is not null
        or u.special = 1
      )
    group by
      u.id          -- the 1, 2 syntax is new to me!
      , u.email
      , o.name
2 comments

For the record, this syntax is horrific and almost unreadable to my eyes.

Multiple spaces after `LEFT` in `LEFT JOIN`? Just to stick with "river"-style alignment, yet your outer-level keywords (`SELECT`, `FROM`, etc.) aren't aligned?

It's difficult to understand why one would pick this format.

Well how far do you go with the river? Aligning with select means group by sticks out. Aligning with group by means left join sticks out. Aligning with left join means inner join sticks out.

EDIT: feel free to show me something better..

`GROUP BY` you align with the space between `GROUP` and `BY`.

`INNER JOIN` goes fully on the right side of the river.

The guide at https://www.sqlstyle.guide/ is almost perfect.

Well this might be an interesting discussion to read for you. Take what you will from it.

https://gist.github.com/isaacs/357981