SQL Recipes
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread
  #1 (permalink)  
Old 08-28-2006, 10:08 PM
jaheer
 
Posts: n/a
Default Oracle dialect question:

How to find second highest value of a column in a table? | SQL?


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 08-29-2006, 07:22 AM
Dimitar
 
Posts: n/a
Default SQL 92 answer. Re: How to find second highest value of a column in a table? | SQL?

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #3 (permalink)  
Old 08-29-2006, 07:59 AM
Dimitar
 
Posts: n/a
Default ANY answer. Re: How to find second highest value of a column in a table? | SQL?

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #4 (permalink)  
Old 08-30-2006, 11:25 AM
Anonymous
 
Posts: n/a
Default SQL 92 answer. Re: How to find second highest value of a column in a table? | SQL?

SELECT score FROM scores WHERE 1 ORDER BY score DESC LIMIT 1,1
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #5 (permalink)  
Old 08-31-2006, 05:28 AM
Dimitar
 
Posts: n/a
Default SQL 92 answer. Re: How to find second highest value of a column in a table? | SQL?

SELECT score FROM scores WHERE 1 ORDER BY score DESC LIMIT 1,1
Dear Anonymous, I think the above is not supported by SQL 92.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #6 (permalink)  
Old 09-20-2006, 11:42 PM
Anvay
 
Posts: n/a
Default ANY answer. Re: How to find second highest value of a column in a table? | SQL?

consider this code:---

SELECT MAX(OFFICE_LOCATION_ID) FROM TABLE
WHERE OFFICE_LOCATION_ID < (SELECT MAX(OFFICE_LOCATION_ID) FROM  TABLE)


hope it works
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #7 (permalink)  
Old 12-06-2006, 01:28 PM
Musclebai
 
Posts: n/a
Default SQL 92 answer. Re: How to find second highest value of a column in a table? | SQL?

SELECT max(score ) FROM scores WHERE score NOT IN (SELECT max(score)
FROM scores)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #8 (permalink)  
Old 06-25-2007, 09:21 AM
Unregistered
 
Posts: n/a
Default ANY answer. Re: How to find second highest value of a column in a table? | SQL?

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #9 (permalink)  
Old 07-17-2007, 05:49 PM
kamal pandey
 
Posts: n/a
Default ANY answer. Re: How to find second highest value of a column in a table? | SQL?

--find 2 or 3 lowest value in record
SELECT min(empmarks )
FROM emp
WHERE  empmarks IN (SELECT top 3 empmarks FROM emp
        ORDER BY empmarks DESC)


--by kamal pandey for sql server only..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #10 (permalink)  
Old 07-24-2007, 11:07 AM
Unregistered
 
Posts: n/a
Thumbs up ANY answer. Re: How to find second highest value of a column in a table? | SQL?

This is sekhar
SELECT max(sal) FROM emp WHERE sal< (SELECT max(sal) FROM emp)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Moderation Tools:


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 27 06-15-2008 08:53 PM
how to find out the 4th highest value from the column? Anoj biradar SQL - Questions and Answers 3 12-05-2007 03:54 AM
how to find third highest record from table? davinder singh SQL - Questions and Answers 3 12-19-2006 11:57 AM


All times are GMT. The time now is 04:54 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2008 SQL Recipes

1 2 3 4 5 6 7 8