mysql - Need help writing SQL queries(joins, subqueries) -
given following schema, i'm supposed write queries questions.
my first query runs no result , second 1 gives subquery returns more 1 row
error.
student (sid, sname, sex, birthdate, gpa) prof (pname, dname) course (cnum, dname, cname) section (cnum, secnum, pname) enroll (sid, cnum, secnum, grade)
for each course, return number of sections (numsections), total number of students enrolled (numstudents), average grade (avggrade), , number of distinct professors taught course (numprofs). show courses in chemistry or computer science department. make sure show courses if have no students. not show course if there no professors teaching course.
return students received higher grade course section average in @ least 2 courses. order number of courses higher average , show top 5.
sql query:
select c.cnum, c.cname, count(*) numsections, count(e.sid) numstudents, avg(e.grade) avggrade, count(p.pname) numprofs course c join section s on c.cnum = s.cnum join enroll e on c.cnum = e.cnum join prof p on s.pname = p.pname c.cname = 'chemistry' or c.cname = 'computer science' group c.cnum, c.cname;` select s.sid, s.sname student s left join enroll e on s.sid = e.sid e.grade > (select avg(grade) course c join enroll e2 on c.cnum = e2.cnum group c.cnum limit 5);`
about second query...
your subquery getting more 1 row of data. use "<" need yo sure bringing 1 row , 1 column.
and if understand correctly, have show top 5 students order number of times better average of course.. realice learning example wont if give query..
you need select top 5 students, know have count() number of times grade greater avg() of each course have taken, @ point in subquery sould have list of students order number of times achive beeing above average.
Comments
Post a Comment