Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hello ExpertsI need some help, please suggest.Say I have a table "tbstudents" with the a field "marks". I need to fetch 10 records only of those students who have maximum marks. Please suggest.Thanks & RegardsRegardsgenius_palli
rajdaksha
Aged Yak Warrior
595 Posts
Posted - 2009-12-22 : 06:56:35
HiYou expect this..
CREATE TABLE #DUP([NAME] VARCHAR(1),AGE INT)INSERT INTO #DUPSELECT 'X', 10 UNION ALLSELECT 'Y' ,20 UNION ALLSELECT 'Z' ,30 UNION ALLSELECT 'X' ,10 SELECT TOP 3 MAX(AGE) FROM #DUP
In your sample you can put TOP 10-------------------------R...
genius_palli
Starting Member
42 Posts
Posted - 2009-12-22 : 07:07:09
Hello rajdakshaThanks for the reply buddy.I tried the code you suggested but it is returning only one row having max age/marks. But I need 10 records of those students having maximum marks. Hope you understand what I am looking for.Regardsgenius_palli
genius_palli
Starting Member
42 Posts
Posted - 2009-12-22 : 07:11:23
Hi again..It was quiet simple. I got it from the following query :::==== select top 10 * from tbstudents order by marks desc =====Anyways thanks for the reply again. Be in touch.Regardsgenius_palli
bklr
Master Smack Fu Yak Hacker
1693 Posts
Posted - 2009-12-23 : 03:58:00
go through this linkhttp://sqlblogcasts.com/blogs/madhivanan/archive/2008/09/12/return-top-n-rows.aspx