If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > MAX() query usage

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-21-06, 04:29
rexselin rexselin is offline
Registered User
 
Join Date: Aug 2003
Location: India
Posts: 109
MAX() query usage

Hi all,

I have a database of marks and I need to find out the maximum mark taken by each student in any subject. For example If am a student and I have got 60 in Language, 70 in Maths, 80 in Science and 90 in Social Studies, I would need the result "90" and the relevant Social Science subject to be displayed so that I know am good at Social Science of all the subjects. Each student may have different subjects.

The table name is marks. The fields are subject_id, mark_obtained, student_id.

When I try this query.

Select subject_id, max(mark_obtained) from marks group by student_id

In this case, I get the correct maximum mark, but the subject id displayed is incorrect. Can anyone tell me what am doing wrong here?
Reply With Quote
  #2 (permalink)  
Old 06-21-06, 04:46
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,260
you may need to consider usign a "group by" clause to uniqeuly identify each student or subject depending on what you are looking for
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 06-21-06, 05:07
rexselin rexselin is offline
Registered User
 
Join Date: Aug 2003
Location: India
Posts: 109
If I give just

Select max(mark_obtained) from marks group by student_id

it returns the maximum mark each student scored. How do I find out the subject in which each student had secured this mark?
Reply With Quote
  #4 (permalink)  
Old 06-21-06, 05:20
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,260
Select max(mark_obtained) from marks group by student_id, Subject_ID
OR
Select max(mark_obtained) from marks group by Subject_ID, student_id
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #5 (permalink)  
Old 06-21-06, 05:52
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by healdem
Select max(mark_obtained) from marks group by student_id, Subject_ID
OR
Select max(mark_obtained) from marks group by Subject_ID, student_id
both of those queries will return a result set that looks like this --

56
73
86
75
24
90
55
64

might be accurate but hardly what i would call useful

how about this --
Code:
select student_id
     , subject_id
     , mark_obtained
  from marks as T
 where mark_obtained =
       ( select max(mark_obtained)
           from marks
          where student_id = T.student_id )
Edit: fixed error in query
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Last edited by r937; 06-21-06 at 06:55.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On