mysql - Is this the right way to join tables to fetch data? -


i have database tables:

student(sid,name,surname,age) registration(studentid,courseid) course(cid,name,cost) 

i extract name of courses students younger 20. query below that?

select c.name course c inner join registration inner join student s cid = courseid     , sid = studentid     , age < 20 group c.name 

i extract number of students in each course having students younger 20. correct below?

select count(s.name)     ,c.name student s inner join course c inner join registration age < 20     , cid = courseid     , sid = studentid group c.name 

you missing on part join otherwise cross join.

your first query should if want distinct list of student names:

select distinct c.name course c inner join registration r on c.cid = r.courseid inner join student s on r.studentid = s.sid age < 20 

your second query shouldn't have c.name in select if want count unless want count of how many students have name.

select count(*) student s inner join registration r on s.sid = r.studentid inner join course c on c.cid = r.courseid age < 20 group c.name 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -