Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 max salary query

Author  Topic 

asm
Posting Yak Master

140 Posts

Posted - 2007-03-19 : 07:17:49
Hi

How to get the maximum salry from a table without using top and
aggreate function

thanks

asm

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-19 : 07:20:33
Which interviewer asked you this question?

SET ROWCOUNT 1

SELECT * FROM Employees ORDER BY Salary DESC

SET ROWCOUNT 0


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-03-19 : 09:06:00
I give up





Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-19 : 09:18:57
try this. It does not use TOP, any aggregate function, ORDER BY.


declare @salary int, @max_salary int
declare c cursor for select Salary from employee
open c
fetch next from c into @salary
set @max_salary = @salary
while @@fetch_status = 0
begin
if @salary > @max_salary select @max_salary = @salary
fetch next from c into @salary
end
close c
deallocate c
select @max_salary as [max salary]




KH

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-19 : 09:24:25
Good piece of code!


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -