12,406,342 members (31,273 online)
Rate this:
See more:
I a table "person" and need a query for those personName who's

1.salary is maximum

2.cityCode is 12

3. age is 32

suppose- there are 3 person with name RAM .

AND 3 with name sunil

and 3 person with name rahul....

and i need the only those record who contain the maximum salary

means ram with maximum salary , sunil with maximum salary and rahul with maximum salary
Posted 9-Jul-13 18:50pm
Updated 9-Jul-13 19:31pm
v3

Rate this:

## Solution 1

If you are asking for three queries....
```Select top(1) PersonName from person Order by Salary Desc -- maximum salary
Select PersonName from person where citycode=12  -- CityCode=12
select PersonName from Person where age=32  -- age=32```

if you are asking for name of the person earning highest salary in citycode 12 and age 32. Then the query is:
`Select top(1) PersonName from person where citycode=12 and age=32 order by salary Desc`
...

if this is not what u required.. use Improve Question widget....

As per your requirement u can use the multiQuery written by Damian S or
```Select p1.PersonName from person p1
inner join (Select Max(salary) as maxsalary from person where Citycode=12 and age=32)p2 on p1.salary=p2.maxsalary Where p1.CityCode=12 and p1.age=32```

But if you want max salary grouped on person you can use:
`Select PersonName, max(Salary) as Salary from person where age=32 and Citycode=12 Group by PersonName`
v5
_Damian S_ 10-Jul-13 1:12am

Very nice solution using select top(1)... have a 5 on me!!
Raja Sekhar S 10-Jul-13 1:14am

Thank You _Damian S_...
Rate this:

## Solution 2

This will require a multipart query. You need one part to find the maximum salary for a given citycode and age, and one part to return the name... like this:

```select personName
from person
where salary = (select max(salary) from person where citycode = 12 and age = 32)
and citycode = 12
and age = 32
```
Raja Sekhar S 10-Jul-13 1:16am

Good one... +5..!
_Damian S_ 10-Jul-13 1:18am

Two different solutions for the OP to choose from!!
Raja Sekhar S 10-Jul-13 1:28am

Yep...
Rate this:

## Solution 3

Try this

```select  PersonName, MAX(Salary)
from    PERSON
where   cityCode = 12
and Age = 32
group by PersonName```
siddharth629 10-Jul-13 5:01am

thanks sir.

Top Experts
Last 24hrsThis month
 OriginalGriff 270 Karthik Bangalore 204 Richard MacCutchan 115 BillWoodruff 75 Afzaal Ahmad Zeeshan 75
 OriginalGriff 7,947 Karthik Bangalore 3,521 ppolymorphe 3,384 Richard MacCutchan 2,577 F-ES Sitecore 2,300