| SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it. |
|
||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread |
|
|||
SELECT score,player FROM scores WHERE score=(SELECT max(score) FROM scores WHERE score< (SELECT max(score) FROM scores));The above finds the second highest score in the scores table and the name of the player. Reading from right to left: 1. The first select finds the highest score. 2. The second select find the highest from the rest. 3. The last select finds the name of the player. Hope this helps. |
|
|||
|
Consider the following data:
score player #1 100 Alex #2 100 Bob #3 98 Toto ... #9 20 Lilirtyry My previous proposal:
SELECT score,player FROM scores WHERE score=(SELECT max(score) FROM scores WHERE score< (SELECT max(score) FROM scores));would select row #3 as having the second highest score, because 98 is the second highest value present in the table. If this is not the correct result, as is the case with the example I have chosen, you need to define additional criteria to differentiate between rows #1 and #2, e.g. the older player wins, and modify the statement accordingly. |
|
|||
SELECT max(value) FROM TABLE WHERE value NOT IN (SELECT max(value) FROM the TABLE);or SELECT level,max(value) FROM TABLE WHERE level=2 connect proir BY value>value GROUP BY level;vivek |
|
|||
|
![]() |
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to find second highest value of a column in a table? | ruby | SQL - Questions and Answers | 23 | 05-07-2008 05:51 AM |
| how to find out the 4th highest value from the column? | Anoj biradar | SQL - Questions and Answers | 4 | 02-21-2008 07:24 AM |
| how to find third highest record from table? | davinder singh | SQL - Questions and Answers | 3 | 12-19-2006 11:57 AM |