which more efficient in sql min or top?
select min(salary) abc select top 1 salary abc order salary asc
tests made on sql server 2012 ( didn't ask specific version, , have):
-- create table create table abc (salary int) -- insert sample data declare @i int = 0 while @i < 1000000 -- that's right, million records.. begin insert abc values (@i) set @i = @i + 1 end
including execution plans , run both queries:
select min(salary) abc select top 1 salary abc order salary asc
results:
- without indexes: query cost top 1 94% , min 6%.
- with index on salary - query cost both 50% (doesn't matter if index clustered or not).
without index:
with index: (clustered , non-clustered resulted in same execution plan)