1

I need help writing a Sql command. I am pretty new at sql so everything is a little confusing. The problem: For each instructor list his / her name and the number of students he / she mentors

There are three tables:

  • Person with the Names and ID
  • Instructor with Instructor ID which references ID
  • Student with StudentID and MentorID which references ID

I tried:

select distinct p.Name,count(d.MentorID) 
from Person p, Instructor e, Student d
where e.InstructorID = d.MentorID   
and p.ID = e.InstructorID;

But that only gives me one results instead a count per instructor.

1
  • Use GROUP and HAVING Commented Feb 5, 2015 at 6:46

1 Answer 1

1

Build a group and then count() counts for every group and not the complete result.

select p.Name, count(d.MentorID) 
from Person p
join Instructor e on p.ID = e.InstructorID
join Student d on e.InstructorID = d.MentorID  
group by p.Name
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That worked beautifully and good explanation also!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.