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
  #11 (permalink)  
Old 09-28-2007, 05:22 PM
Unregistered
 
Posts: n/a
Default ANY answer. Re: How to find second highest value of a column in a table?

Do You Know How to select the nth(for ex : 10th) largest number in a column without using max() function..
I am using orderid column from orders table in nortwind database for example

SELECT top 1 * FROM(SELECT top 10 orderid FROM orders ORDER BY orderid DESC)a ORDER BY orderid ASC

By : Krishna Prasad J
krishnaprasad.jk@gmail.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 10-12-2007, 09:03 AM
Sameer
 
Posts: n/a
Default ANY answer. Re: How to find second highest value of a column in a table?

SELECT * FROM tablename orderby salary  LIMIT 1 offset 2

use 3 for third highest 4 for 4th highest after offset

sameerargade@gmail.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 10-13-2007, 12:07 PM
Abhishek
 
Posts: n/a
Default ANY answer. Re: How to find second highest value of a column in a table?

Hi,
You can use the following:

SELECT *
      FROM (SELECT ename
                         ,enumber
                         ,sal
                         ,ROW_NUMBER() OVER(ORDER BY sal DESC) rn
                  FROM emp)
          WHERE rn < 3;


Also, you can use the DENSE_RANK() functionality to get this done in a better way:

SELECT *
      FROM (SELECT ename
                         , sal
                         , Dense_Rank() over(ORDER BY sal DESC
                                                     )rn
                  FROM emp)
      WHERE rn < 3;




Thanks,
vinay singh
Sagarpur New Delhi, India
email- vinay_1020@live.in
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 10-14-2007, 09:37 AM
Unregistered
 
Posts: n/a
Thumbs up ANY answer. Re: how to display the first ten records?

i have an employe table. i want display the first ten records.how?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15 (permalink)  
Old 10-24-2007, 11:07 AM
Unregistered
 
Posts: n/a
Default ANY answer. Re: How to find second highest value of a column in a table?

Quote:
Originally Posted by ruby View Post
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?

SELECT quantity FROM trade a WHERE 2=(SELECT count(quantity) FROM trade b WHERE a.quantity<=b.quantity);

-hello friend this query is called nested sub query this is the only way to find the values in given count order
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #16 (permalink)  
Old 11-16-2007, 09:04 AM
Unregistered
 
Posts: n/a
Default ANY answer. Re: How to find second highest value of a column in a table?

You can use following query to find any highest from a table column values:

SELECT MIN(columnName)
FROM tableName  WHERE
columnName IN (SELECT DISTINCT TOP N columnName FROM tableName ORDER BY columnName DESC);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #17 (permalink)  
Old 11-27-2007, 10:55 AM
narayana
 
Posts: n/a
Default ANY answer. Re: How to find second highest value of a column in a table?

Correlated query:

1.SELECT sal FROM emp e WHERE 1=(SELECT count(DISTINCT(sal)) FROM emp WHERE e.sal<sal);
this gives the exact second heighest value

2.SELECT sal FROM emp e WHERE &(n-1)=(SELECT count(DISTINCT(sal)) FROM emp WHERE e.sal<sal);

this gives the nth heighest value from atable


k.lakshmi_narayana@yahoo.co.in
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #18 (permalink)  
Old 12-05-2007, 11:16 AM
deepak garg deepak garg is offline
Junior Member
 
Join Date: Dec 2007
Posts: 6
deepak garg is on a distinguished road
Wink ANY answer. How can find the rows excluding the top 2 highest values

this is how you can simply do this

SELECT col_name FROM table_name WHERE col_name NOT IN (SELECT col_name FROM (SELECT col_name FROM table_name WHERE col_name IS NOT NULL ORDER BY col_name DESC)
WHERE rownum<=2)ORDER BY col_name
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #19 (permalink)  
Old 12-05-2007, 11:19 AM
deepak garg deepak garg is offline
Junior Member
 
Join Date: Dec 2007
Posts: 6
deepak garg is on a distinguished road
Wink ANY answer. Re: How to find second highest value of a column in a table?

you can use this and this is general you can find any highest value

SELECT min(sal) FROM (SELECT sal FROM emp WHERE sal IS NOT NULL ORDER BY sal DESC) WHERE rownum<=&rownum
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #20 (permalink)  
Old 02-20-2008, 08:26 AM
Suraj Dhandal
 
Posts: n/a
Default ANY answer. Re: Get Max Salary at any rank

SELECT salary FROM(
    SELECT a1.salary,count(a2.salary)sal
    FROM salarytable a1,salarytable a2
    WHERE a1.salary<=a2.salary OR (a1.salary=a2.salary)
    GROUP BY a1.salary)a WHERE sal=4



Here 4 specifies Max salary at fourth position
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not 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


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 3 12-05-2007 03:54 AM
How to find second highest value of a column in a table? | SQL? jaheer SQL - Questions and Answers 9 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 07:44 AM.


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