943,596 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Marked Solved
  • Views: 2071
  • MySQL RSS
Aug 25th, 2008
0

What to fetch second highest marks?

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dharam_05 is offline Offline
22 posts
since Aug 2008
Aug 27th, 2008
0

Re: What to fetch second highest marks?

no one can help me out...in this forum
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dharam_05 is offline Offline
22 posts
since Aug 2008
Aug 27th, 2008
0

Re: What to fetch second highest marks?

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

MySQL Syntax (Toggle Plain Text)
  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;
Reputation Points: 22
Solved Threads: 9
Junior Poster in Training
varmadba is offline Offline
83 posts
since Jun 2008
Aug 28th, 2008
0

Re: What to fetch second highest marks?

Thanks for your reply...
i guess it going to work...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dharam_05 is offline Offline
22 posts
since Aug 2008
Aug 28th, 2008
0

Re: What to fetch second highest marks?

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dharam_05 is offline Offline
22 posts
since Aug 2008
Sep 5th, 2008
1

Re: What to fetch second highest marks?

This is how i goes

MySQL Syntax (Toggle Plain Text)
  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 :

MySQL Syntax (Toggle Plain Text)
  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
Reputation Points: 22
Solved Threads: 9
Junior Poster in Training
varmadba is offline Offline
83 posts
since Jun 2008
Sep 5th, 2008
0

Re: What to fetch second highest marks?

Thank you!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dharam_05 is offline Offline
22 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: pass php variables to mysql database
Next Thread in MySQL Forum Timeline: MySQL Quick Reference Card





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC