• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sql query

 
Ayub ali khan
Ranch Hand
Posts: 394
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
Jussi Taimiaho
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 394
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jussi!! Could you metion the query for the first approach?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34965
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first one:

 
Ayub ali khan
Ranch Hand
Posts: 394
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34965
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 394
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jeanne!! I understood the logic. Your help is very much appreciated
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic