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.
Author |
Topic |
derok
Starting Member
14 Posts |
Posted - 2008-04-25 : 14:56:33
|
Hi,I have this table in sql server:It record the info of the grades of students.Table: GradesColumns: GradeId, Grade, Id, SubjectId.i'm doing this to select the highest grade:select max(grade)from grades it works fine, it displays the highest grade,but i want that, it also displays the id of the studentwhat can i do?thanks |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-04-25 : 14:59:16
|
I'm sure this is a homework question, but since you showed us what you tried, I'll bite.SELECT id, MAX(grade) AS HighestGradeFROM gradesGROUP BY idTara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
derok
Starting Member
14 Posts |
Posted - 2008-04-25 : 15:09:51
|
quote: Originally posted by tkizer I'm sure this is a homework question, but since you showed us what you tried, I'll bite.SELECT id, MAX(grade) AS HighestGradeFROM gradesGROUP BY idTara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/
thanks, butthis code:select max(grade)from gradesshows this:87.0and this:SELECT id, MAX(grade) AS HighestGradeFROM gradesGROUP BY idshows this:id--------HighestGrade21--------5432--------87what i want is that only the highest grade which is 87 and the corresponen id "32", gets printed on screen.what can i do? |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-04-25 : 15:11:44
|
You can do this then:SELECT id, gradeFROM gradesWHERE grade = (SELECT MAX(grade) FROM grades)Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
derok
Starting Member
14 Posts |
Posted - 2008-04-25 : 15:25:53
|
quote: Originally posted by tkizer You can do this then:SELECT id, gradeFROM gradesWHERE grade = (SELECT MAX(grade) FROM grades)Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/
thanksyou're incredible!! |
 |
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2008-04-26 : 02:15:30
|
or:select top 1 id, grade from grades order by grade desc elsasoft.org |
 |
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2008-04-28 : 06:12:20
|
or:select top 1 with ties id, grade from grades order by grade descRyan Randall Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
derok
Starting Member
14 Posts |
Posted - 2008-05-04 : 14:41:30
|
thanks guys! |
 |
|
|
|
|
|
|