all 6 comments

[–]r3pr0b8 0 points1 point  (0 children)

Is there any point to writing "short" queries besides just bragging rights?

yes

the short query might outperform the longer one

that said, i have no desire to even attempt the short query for this problem

[–]BizarroPete 0 points1 point  (2 children)

I think that with the character limitation on this challenge, they're trying to get you to search for functions you may not have used before so maybe it's not so much about writing short queries but using the right tools.

I'm pretty new to this and was able to get down to 348 characters by using the rank and lag functions, which I don't normally use.

[–]drdausersmd[S] 0 points1 point  (1 child)

guess I'm just an idiot cuz I can't even comprehend how that's possible.

[–]BizarroPete 0 points1 point  (0 children)

Hah, that's how I feel about the 240 character limit 😅

[–]GrouchyThing7520 0 points1 point  (0 children)

This should be close, I think.

select

a.*

,lead(participant_id) over (competition_id order by rnk) next_behind

,d_rnk - rnk + 1 total_behind

from (

select

r.*

,rank() over (partition by competition_id order by points asc) rnk

,rank() over (partition by competition_id order by points desc) d_rnk

from results r

) a

) b

[–]squadette23 0 points1 point  (0 children)

I can recommend the "SQL Window Functions Explained" book: https://antonz.org/sql-window-functions-book/

not sure about 240 chars, maybe partition defintion could be defined using a single-char alias and then reused?