Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
Ranking Placing Query in POSTGRESQL

I have two tables: player and player_friend. The table player has three fields: 'id', 'level' and 'username'. The table player_friend has two fields: 'player_id' and 'friend_id'. I can get all places in the ranking by querying player, sorting by 'level' and the index of the row in the result is the placing in the ranking:

SELECT (username, level) FROM player SORT BY level;

How do I query the placing in the ranking, username and level of entries in the player_friend 'friend_id' where 'player_id' = x?

For example:

player

id | username | level
 1 |        a |     1
 2 |        b |     3
 3 |        c |     2
 4 |        d |     5

player_friend

player_id | friend_id
        4 |         1
        4 |         2

expected results:

placing | username | level
      2 |        b |     3
      4 |        a |     1

Explanation: friend with username "b" is the second in the global ranking and friend with username "a" is the global ranking (sorting by level), in the example it is not visible but it is only showing where player_friend 'player_id' is 4.

Thanks in advance!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I think you want row_number():

select p.*
from player_friend pf join
     (select p.*, row_number() over (order by level) as placing
      from player p
     ) p
    on pf.friend_id = p.id;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!