Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,


I have a table like this..

EMP_NO EMP_NAME SALARY
1 Basha 1000
2 Pavan 2500
3 santu 2000
4 karthik 500
5 naresh 1500

how to write a query to get the highest salary or ranking based on salary in another row like this

EMP_NO EMP_NAME SALARY Highest
1 Basha 1000 4
2 Pavan 2500 1
3 santu 2000 2
4 karthik 500 5
5 naresh 1500 3


Regards,
S.Inayat Basha.
Posted

Hi Inayat,

Please use following:

SQL
--Table creation
CREATE TABLE Employee
(Name VARCHAR(10), 
Age INT, 
Salary INT)

--Inserting entries
insert into Employee
select 'Haidarali',29,29000 union all
select 'Rashidali',2,20000 union all
select 'Imdadhusen',28,50000 union all
select 'Abbas',25,58000

--ordering entry by rank
SELECT RANK() OVER(ORDER BY Salary DESC) AS Rank,* FROM dbo.Employee;



Please do let me know, if you have any doubt.

Please provide "Vote":thumbsup: if this would be helpful, and make "Accept Answer" if this would be correct answer.:rose:

Thanks,
Imdadhusen
 
Share this answer
 
Comments
inayat basha 23-Nov-10 5:02am    
good answer
A RANK will help you to do the same.

SELECT EMP_NO, EMP_NAME, SALARY,
       RANK() OVER (ORDER BY SALARY DESC) AS Highest
FROM TABLENAME
ORDER BY EMP_NO
 
Share this answer
 
Comments
inayat basha 23-Nov-10 5:02am    
nice one...thx
Sunasara Imdadhusen 23-Nov-10 5:09am    
Good answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900