What to fetch second highest marks?

Thread Solved

Join Date: Aug 2008
Posts: 22
Reputation: dharam_05 is an unknown quantity at this point 
Solved Threads: 0
dharam_05 dharam_05 is offline Offline
Newbie Poster

What to fetch second highest marks?

 
0
  #1
Aug 25th, 2008
Hi i want need to fetch sec highest marks from given tables.
my database design is like:
My database design:
Table 1:

Student table
(PK)Stud_id, Stud_name

Table 2:

Subject table
(pk)Sub_id, Sub_name, Marks.

Table 3:

Stud_Sub table
Stud_id, Sub_id


And i come up with the query:
Select Student.stud_id, Student.stud_name Max( subject.marks)from Student, subject, stud_sub
where marks < ( Select Max(sum(marks)) from student, subject, stud_sub where student.stud_id = stud_sub.stud_id AND stud_sub.sub_id = Subject.sub_id group by stud_id;

Can any one correct me, if this query is wrong coz m new in query writing.
please help me in finding the second highest marks in database.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 22
Reputation: dharam_05 is an unknown quantity at this point 
Solved Threads: 0
dharam_05 dharam_05 is offline Offline
Newbie Poster

Re: What to fetch second highest marks?

 
0
  #2
Aug 27th, 2008
no one can help me out...in this forum
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 79
Reputation: varmadba is an unknown quantity at this point 
Solved Threads: 8
varmadba varmadba is offline Offline
Junior Poster in Training

Re: What to fetch second highest marks?

 
0
  #3
Aug 27th, 2008
Hey
in the query you r using
"where marks is less than max(sum(marks))"
This will be true for all the records as individual marks will be less than sum of them

  1. SELECT
  2. Student.stud_id,
  3. Student.stud_name,
  4. Max(subject.marks)
  5. FROM Student, subject, stud_sub
  6. WHERE sum(marks)<
  7. ( SELECT Max(sum(marks)) FROM student, subject, stud_sub
  8. WHERE student.stud_id = stud_sub.stud_id
  9. AND stud_sub.sub_id = Subject.sub_id GROUP BY stud_id)
  10. GROUP BY stud_id
  11. ORDER BY sum(marks) desc
  12. LIMIT 1;
:- Varma

We are Happy to inform launch of a new site with loads of database related information Site offers wide range of functionality Forums,Blogs,Articles,Editorials and much more
http://www.sqllibrarian.info/
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 22
Reputation: dharam_05 is an unknown quantity at this point 
Solved Threads: 0
dharam_05 dharam_05 is offline Offline
Newbie Poster

Re: What to fetch second highest marks?

 
0
  #4
Aug 28th, 2008
Thanks for your reply...
i guess it going to work...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 22
Reputation: dharam_05 is an unknown quantity at this point 
Solved Threads: 0
dharam_05 dharam_05 is offline Offline
Newbie Poster

Re: What to fetch second highest marks?

 
0
  #5
Aug 28th, 2008
Can we put only subject.marks in place sum(marks)...
And one more thing can you please ellaborate how this query going to work, i mean in sub query it fetch one record or it going to fetch no. of records. just want to know how this query proceed...
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 79
Reputation: varmadba is an unknown quantity at this point 
Solved Threads: 8
varmadba varmadba is offline Offline
Junior Poster in Training

Re: What to fetch second highest marks?

 
1
  #6
Sep 5th, 2008
This is how i goes

  1. SELECT
  2. Student.stud_id,
  3. Student.stud_name,
  4. Max(subject.marks)
  5. FROM Student, subject, stud_sub
  6. WHERE sum(marks)<
  7. ( SELECT Max(sum(marks)) FROM student, subject, stud_sub
  8. WHERE student.stud_id = stud_sub.stud_id
  9. AND stud_sub.sub_id = Subject.sub_id GROUP BY stud_id)
  10. GROUP BY stud_id
  11. ORDER BY sum(marks) desc
  12. LIMIT 1;

The Inner Query :

  1. SELECT Max(sum(marks)) FROM student, subject, stud_sub
  2. WHERE student.stud_id = stud_sub.stud_id
  3. AND stud_sub.sub_id = Subject.sub_id GROUP BY stud_id

will get the max of sum(marks) "only one value"

so the outer query will bring records which are less than value returned by the inner query
and as we are ordering by sum(marks) desc and limit 1
it will give you the second highest record
:- Varma

We are Happy to inform launch of a new site with loads of database related information Site offers wide range of functionality Forums,Blogs,Articles,Editorials and much more
http://www.sqllibrarian.info/
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 22
Reputation: dharam_05 is an unknown quantity at this point 
Solved Threads: 0
dharam_05 dharam_05 is offline Offline
Newbie Poster

Re: What to fetch second highest marks?

 
0
  #7
Sep 5th, 2008
Thank you!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the MySQL Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC