File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes sql query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "sql query" Watch "sql query" New topic
Author

sql query

Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 379
    
    2
Could any one help me how to write a query to find the second largest salary and second smallest salary from a salary table?

Thanks in advance!!


SCEA part I,TOGAF Foundation
Jussi Taimiaho
Ranch Hand

Joined: Mar 01, 2004
Posts: 40
Two approaches come to my mind. First is to order by salaray and take the next row. Limit the number of rows output, database permitting. Second using subquery, which definately is not the most efficient way, but allows this to be done with a single query.

2nd:
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 379
    
    2
Thanks Jussi!! Could you metion the query for the first approach?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29246
    
139

The first one:



[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 379
    
    2
Thank you v.Much Jeanne!! please pardon my ignorance.

From your answer I understood that you sorted the table. What would be the query to retrieve the second largest sal from the sorted table?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29246
    
139

Ayub,
That is correct I sorted the table. The SQL query is what I provided in the above post.

To get the second row:
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 379
    
    2
Thank you Jeanne!! I understood the logic. Your help is very much appreciated
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sql query
 
Similar Threads
J2EE Contract--Colorado Springs
Salary In HP for 2 Years Exp URGENT.
Clarification regarding createNativeQuery() method
SQL Query
hashcode()