SQL Recipes (Beta II)
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 Rating: Thread Rating: 1 votes, 5.00 average.
  #1 (permalink)  
Old 07-24-2006, 01:54 PM
ruby
 
Posts: n/a
Default MySQL dialect question:

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


I have a table - Trade with 2 columns -Client name and quantity.
I want to find out the name of the client with second highest quantity. How do I proceed?
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-01-2006, 01:35 AM
Sebastian Martens
 
Posts: n/a
Default MS SQL answer. Re: How to find second highest value of a column in a table?

SELECT * FROM TABLE
WHERE columnName =
( SELECT MAX(columnName) FROM TABLE WHERE columnname<(SELECT max(columnname) FROM TABLE))
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #3 (permalink)  
Old 08-02-2006, 01:21 PM
Dan
 
Posts: n/a
Default ANY answer. Re: How to find second highest value of a column in a table?

SELECT TOP 1 quantity  FROM Trade
  WHERE quantity < (SELECT MAX(quantity)  FROM Trade)
  ORDER BY  quantity DESC;


Is this query will work? If it works what should be the output?

regards,
venkat
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #4 (permalink)  
Old 08-05-2006, 03:47 AM
sridhar.barla
 
Posts: n/a
Default Oracle answer. Re: How to find second highest value of a column in a table?

SELECT quantity   FROM Trade WHERE  quantity=(SELECT MAX(quantity)
FROM Trade WHERE  quantity<> (SELECT MAX(quantity) FROM  Trade))
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #5 (permalink)  
Old 08-09-2006, 09:02 PM
Dhana Funda
 
Posts: n/a
Default MySQL answer. Re: How to find second highest value of a column in a table?

SELECT Max(quantity) FROM Trade
WHERE quantity NOT IN (SELECT MAX(quantity) FROM Trade);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #6 (permalink)  
Old 06-17-2007, 01:15 PM
ashin
 
Posts: n/a
Default ANY answer. Re: How to find second highest value of a column in a table?

Hi..is it possible to write the same query without using top & max...
can anyone tell me??

Yes it is possible to answer by using correlated queries.

select sal from emp e where 1=(select count(*) from emp where e.sal<sal);


This is a cheap query written..............asdfadfaaf
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #7 (permalink)  
Old 06-22-2007, 09:58 PM
Unregistered
 
Posts: n/a
Default ANY answer. Re: How to find second highest value of a column in a table?

This will get you only the second max salary row
SELECT * FROM EMP
WHERE SAL IN (SELECT MAX(SAL) FROM EMP
WHERE SAL <> (SELECT MAX(SAL) FROM EMP))


from how to find second largest number through a query in sql - Community Forums - Developer Fusion, the UK developer community - VB, ASP, C#, .NET, PHP and XML tutorials & source code
SELECT * FROM tmp_table m1
WHERE (n-1)=(SELECT count(DISTINCT(m2.age))
FROM tmp_table m2 WHERE
m2.age>m1.age)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #8 (permalink)  
Old 07-16-2007, 05:28 AM
Unregistered
 
Posts: n/a
Default ANY answer. Re: How to find second highest value of a column in a table?

Hello can any buddy suggest me how to get the 3rd highest value from table and can ve right any query to get 4th, 5th as well?

For finding 2'nd largest row , we can use various forms ..like
(1)
select max(value1) from ( select value1 from val minus select max(value1) from val );
(2)
Select max(value1) from val where value1 < ( Select max(value1) from val );
(3)
select max( value1) from val where value1 not in( select max(value1) from val );
But there is 1 General way also..we can simply use this formula--
select rownum,ename,eno from emo gropu by rownum,ename,eno having rownum > n minus select rownum, ename , eno from emp group by rownum,ename,eno having rownum > n ;

cheers...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #9 (permalink)  
Old 09-11-2007, 09:48 AM
Unregistered
 
Posts: n/a
Exclamation ANY answer. Re: How to find second highest value of a column in a table?

Second highest value could be find out by entering this command:-
Let say u want to find out salary(second highest) from employ table then..

SELECT MAX(SALARY) FROM EMPLOYEE WHERE SALARY NOT IN (SELECT MAX(SALARY) FROM EMPLOYEE)




Regards,
Mohammed Obaid Khan
Pune, Maharashtra
India
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #10 (permalink)  
Old 09-27-2007, 04:52 PM
zubair_ahmed
 
Posts: n/a
Thumbs up ANY answer. Re: How to find second highest value of a column in a table?

make use of MINUS operator
supposing to find second highest sal from tablename(table)
ie select max(sal) from( select sal from tablename
MINUS
select max(sal) from tablename)
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 out the 4th highest value from the column? Anoj biradar SQL - Questions and Answers 4 02-21-2008 07:24 AM
How to find second highest value of a column in a table? | SQL? jaheer SQL - Questions and Answers 10 07-24-2007 11:07 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 01:47 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-2007 SQL Recipes

1 2 3 4 5 6 7