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
 Top 10 records having max marks

Author  Topic 

genius_palli
Starting Member

42 Posts

Posted - 2009-12-22 : 06:53:13
Hello Experts

I 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 & Regards



Regards
genius_palli

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-12-22 : 06:56:35
Hi

You expect this..

CREATE TABLE #DUP([NAME] VARCHAR(1),AGE INT)

INSERT INTO #DUP
SELECT 'X', 10 UNION ALL
SELECT 'Y' ,20 UNION ALL
SELECT 'Z' ,30 UNION ALL
SELECT 'X' ,10

SELECT TOP 3 MAX(AGE) FROM #DUP


In your sample you can put TOP 10
-------------------------
R...
Go to Top of Page

genius_palli
Starting Member

42 Posts

Posted - 2009-12-22 : 07:07:09
Hello rajdaksha

Thanks 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.

Regards
genius_palli
Go to Top of Page

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.

Regards
genius_palli
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-12-23 : 03:58:00
go through this link
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/09/12/return-top-n-rows.aspx
Go to Top of Page
   

- Advertisement -