| | |
What to fetch second highest marks?
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 22
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jun 2008
Posts: 79
Reputation:
Solved Threads: 8
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
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)
SELECT Student.stud_id, Student.stud_name, Max(subject.marks) FROM Student, subject, stud_sub WHERE sum(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) GROUP BY stud_id ORDER BY sum(marks) desc 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/
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/
•
•
Join Date: Jun 2008
Posts: 79
Reputation:
Solved Threads: 8
This is how i goes
The Inner Query :
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
MySQL Syntax (Toggle Plain Text)
SELECT Student.stud_id, Student.stud_name, Max(subject.marks) FROM Student, subject, stud_sub WHERE sum(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) GROUP BY stud_id ORDER BY sum(marks) desc LIMIT 1;
The Inner Query :
MySQL Syntax (Toggle Plain Text)
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
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/
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/
![]() |
Other Threads in the MySQL Forum
- Previous Thread: pass php variables to mysql database
- Next Thread: MySQL Quick Reference Card
| Thread Tools | Search this Thread |
agplv3 alfresco amazon api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright count court crm data database design developer development distinct drupal dui ec2 email enter enterprise eudora facebook form foss gartner gnu government gpl greenit groklaw groupware hiring hyperic images joebrockmeier join journalism keyword keywords kickfire laptop law legal license licensing linux maintenance managing mariadb matchingcolumns metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla multiple music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource penelope php priceupdating query referencedesign reorderingcolumns resultset saas select sharepoint simpledb sourcecode spotify sql syntax techsupport thunderbird transparency virtualization





