*
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
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: 380
    
    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: 380
    
    2
Thanks Jussi!! Could you metion the query for the first approach?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30361
    
150

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: 380
    
    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: 30361
    
150

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: 380
    
    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