To get the count per student you need to GROUP BY
the Student (s.ID, s.name
):
SELECT s.ID, s.name, COUNT(*) AS credits
FROM student AS s
INNER JOIN takes AS t ON s.ID = t.ID
WHERE t.grade != 'F'
GROUP BY s.ID, s.name
I am guessing that certain courses amount for more credits than others. So if you have a field that shows how many credits each course is, you may want to link that table and use SUM(CreditVal)
instead of COUNT(*)
. Something like:
SELECT s.ID, s.name, SUM(c.credits) AS TotalCreditsEarned
FROM student AS s
INNER JOIN takes AS t ON s.ID = t.ID
INNER JOIN course AS c ON c.ID = t.course_id
WHERE t.grade != 'F'
GROUP BY s.ID, s.name
In the above query just change c.ID = t.course_id
to whatever the fields are that link the two tables together.