skip to Main Content

I have been given a leaderboard, I need to return the row correspond to 4th to 8th rank in a leaderboard?

id  name    score
1   gongy   3001
2   urandom 2401
3   eduardische 2477
4   Gassa   2999
5   bcc32   2658
6   Alex_2oo8   6000
7   mirosuaf    2479
8   Sparik  2399
9   thomas_holmes   2478

The query I have is not being accepted

select b.name from
(
    select a.name as name ,a.score as score from 
    (
    select name,score from leaderboard order by score desc limit 8
    )a
    order by a.score 
    limit 5 
) b
order by b.score desc;

2

Answers


  1. select x.id,x.name,x.score
    from
    (
      select id,name,score,
       row_number()over(order by score desc)xcol
       from your_table
    )x where x.xcol >=3 and x.xcol<=8
    

    May be something like this ?

    Login or Signup to reply.
  2. You can use LIMIT OFFSET

    select id,
           name,
           score
    from leaderboard
    order by score desc 
    limit 4 offset 3  ; --- fetch 4 records, begin with record 4 (OFFSET 3)
    

    https://dbfiddle.uk/Ys-3jC4L

    Above query skips the first 4 rows and limit the result to 4 make it from 4th to 8th

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search