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 > Please help with creating this SQL query.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-17-11, 01:37
vannuge vannuge is offline
Registered User
 
Join Date: Feb 2011
Posts: 2
Please help with creating this SQL query.

So I'm trying to use the right SQL commands to solve this question..
"return the top 5 students with the highest major GPA (Grade Point Average). Major GPA is determined by the courses taught in a student’s home department and has the attribute Core=1." as a single query.

I tried the following with no luck.
SELECT s1.name,s1.deptID,avg(grade) as GPA
FROM
Transcripts
JOIN Students s1 using (studentID)
JOIN Departments D1 USING (deptID)
JOIN Sections USING (sectionID)
JOIN Courses USING (deptID)
JOIN Departments D2 ON (Courses.deptID=D2.deptID)
WHERE D1.deptID=D2.deptID AND core=1
GROUP BY s1.studentid
ORDER by GPA DESC
LIMIT 5
;

What am I doing wrong? The WHERE course.core=1 seems to not be doing anything for me. To clarify, I'm trying to only average the grades that are core=1. Any help is greatly appreciated. Thank you.

here is the schema.
http://pic80.picturetrail.com/VOL966.../395200168.jpg

Last edited by vannuge; 02-17-11 at 01:39. Reason: typo
Reply With Quote
  #2 (permalink)  
Old 02-17-11, 04:58
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
sorry, "with no luck" is not an error message that i recognize

you will have to explain what the problem is

also, why do you have two departments tables in the query?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-17-11, 05:21
vannuge vannuge is offline
Registered User
 
Join Date: Feb 2011
Posts: 2
I was under the impression that's how I can compare the department of the student with the department of the courses. That way, I can filter it to only consider courses within the student's department. I'm willing to try out other methods though.

My real question is how can I do a conditional AVG, so that it'll only average in the courses that have core=1. The error is that regardless of whether core=0 or core=1, my answer remains the same, proving to me that it's not affecting the query at all.

Please let me know if I need to clarify anything else. I'm still stuck. Thanks for replying.
Reply With Quote
  #4 (permalink)  
Old 02-17-11, 05:49
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
do me a favour, please re-write your query without using the USING keyword, instead, use an ON clause where columns are matched explicitly

also, please qualify every column properly with its table prefix
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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