资源描述
实验五实验5.1 数据查询1) 要求以School数据库为例,在该数据库中存在四张表格,分别为:l 表STUDENTS(sid, sname, email, grade);l 表TEACHERS(tid, tname, email, salary);l 表COURSES(cid, cname, hour);l 表CHOICES(no, sid, tid, cid, score)在数据库中,存在这样的关系:学生可以选择课程,一个课程对应一个教师。在表CHOICES中保存学生的选课记录。按以下要求对数据库进行查询操作:(1) 查询年级为2001的所有学生的名称并按编号升序排列。 程序:Select sname from students where grade=2001 order by sid asc; (2) 查询学生的选课成绩合格的课程成绩。 程序:Select score from choices where score59;(3) 查询课时是48或60的课程的名称。 程序:select cname from courses where hour=48or hour=60;(4) 查询所有课程名称中含有data的课程编号。 程序:select cid from courses where cname like%data%;(5) 查询所有选课记录的课程号(不重复显示)。 程序:select distinct cid from choices;(6) 统计所有教师的平均工资。 程序:select avg(salary) from teachers;(7) 查询所有教师的编号及选修其课程的学生的平均成绩,按平均成绩降序排列。程序:select tid,avg(score) from choices GROUP BY tid order by avg(score) desc;(8) 统计各个课程的选课人数和平均成绩。 程序:select count(distinct sid),avg(score)from choicesgroup by cid;(9) 查询至少选修了三门课程的学生编号。程序:select sid from choices group by sid having count(cid)=3;(10) 查询编号800009026的学生所选的全部课程的课程名和成绩。程序:select distinct cname ,score from courses,choices where sid=800009026and courses.cid=choices.cid(11) 查询所有选修了database的学生的编号。程序:select sid from choices ,courses where cname=databaseand choices.cid=courses.cid;(12) 求出选择了同一个课程的学生对。 程序:Select x.sid,y.sid From choices x,choices y Where x.cid=y.cid and x.sidy.sid (13) 求出至少被两名学生选修的课程编号。 程序:select cid from choices group by cid having count(sid)=2;(14) 查询选修了编号800009026的学生所选的某个课程的学生编号。 程序:select sid from choices where cid in (select cid from choices where sid=800009026)and sid800009026;(15) 查询学生的基本信息及选修课程编号和成绩。 程序:select students.sid,sname,email,grade,cid,score from students,choices where students.sid=choices.sid;(16) 查询学号850955252的学生的姓名和选修的课程名及成绩。 程序:select sname,cname,score from students,courses,choices where choices.sid=850955252and students.sid=choices.sid and courses.cid=choices.cid; (17) 查询与学号850955252的学生同年级的所有学生资料。 程序:select * from students where grade=(select grade from students where sid=850955252);(18) 查询所有有选课的学生的详细信息。 程序:select students.sid,sname,email,grade,no ,cid,score from students,choices where cid is not null and students.sid=choices.sid;
展开阅读全文