Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1483
  • Last Modified:

Mysql Rank / Row Number Query

I want to find out the "ranking" of a team based on their number of points.

Teams...
id   name   points
1    test     100
2    test2    50
3    test3    150

I would like to run a query like "SELECT (...) as rank FROM teams WHERE id=2" and have it return 3

THis make sense?
Can this be done?

0
guitarclap
Asked:
guitarclap
  • 2
2 Solutions
 
todd_farmerCommented:
SELECT count(*) FROM teams WHERE points <= (SELECT points FROM teams WHERE id=2);
0
 
Harisha M GCommented:
I think this: ( >= instead of <= )
 SELECT count(*) FROM teams WHERE points >= (SELECT points FROM teams WHERE id=2);
0
 
Harisha M GCommented:
Or this: (in case the points are same for many teams)

SELECT count(*)+1 FROM teams WHERE points > (SELECT points FROM teams WHERE id=2);
0
 
guitarclapAuthor Commented:
Ohhh..
So by this you are counting the number of teams that have more points then the team then adding 1.  So if there are 9 teams with more points than the team wanted then they are #10. Perfect!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now