有三张表:
学生表studet
Sno学号  Sname姓名  Ssex性别  Sage年龄  ClassNo班级
课程表course
Cno课程号  Cname课程名称  Credit学分  Tno教师代号
成绩表SC
Sno 学号  Cno课程号  Grade成绩  Date考试时间
教师表teacher
Tno教师号  Tname姓名  Ssex性别  Sage年龄问题1
查考试成绩有不及格的学生的学号和姓名,且消除重复行问题2
查询至少选修一门“李峰”老师的课程的学生姓名问题3
检索出没有被任何学生选修的课程问题4
求出少于10个学生选修的课程问题5
求出有四门课程考试不及格的学生的学生号、姓名问题6
求出教了三门课程以上的老师问题7
求出每一个班级中每一门课程获得最高分的学生的学号与姓名

解决方案 »

  1.   


    查询至少选修一门“李峰”老师的课程的学生姓名select distinct Student.* from Student , SC , Course , Teacher 
    where Student.S# = SC.S# and SC.C# = Course.C# and Course.T# = Teacher.T# and Teacher.Tname = N'李峰'
    order by Student.S#
      

  2.   

    参考:
    http://blog.csdn.net/maco_wang/article/details/6281484
      

  3.   

    帮你做了前4题 如果是考试应该及格了 都是上机亲测的答案!
    1 select distinct student.Sno,student.Sname from student,SC where student.Sno=SC.Sno and SC.Grade<60
    2 select distinct student.Sname from student,course,SC,teacher
    where student.Sno=SC.Sno and
    course.Cno=SC.Cno and
    course.Tno=teacher.Tno and
    teacher.Tname='李峰'
    3 select distinct course.Cname from course,SC
    where course.Cno not in (select distinct SC.Cno from SC)
    4 select course.Cname from course where course.Cno in
    (select SC.Cno from SC group by SC.Cno having count(*)<10) or
    course.Cno not in (select distinct SC.Cno from SC)希望你自己能把其余三道题 通过自己的学习做出来。作为一名程序员最起码的学习能力应该有。
      

  4.   


    1.select distinct sno,sname from student st where exists(
    select 1 from sc where sno=st.sno and grade<60)
    2. select sname from student st where exists(
    select 1 from sc join course on sc.sno=st.sno and sc.cno=course.cno
    join teacher on course.tno=teacher.tno and teacher.name='李峰'
    )
    3. select cname from course left join sc on course.cno=sc.cno and sc.cno is null
    4. select cno from sc group by cno having count(1)<10
    5. select sno,sname from student where exists (
    select 1 from sc where sno=st.sno and grade<60
    )group by sno,sname having count(1)=4
    6. select tno,tname from teacher te join(
    select tno from course group by tno having COUNT(1)>3
    )t on tempdb.tno=t.tno
    )
      

  5.   

    问题1
     查考试成绩有不及格的学生的学号和姓名,且消除重复行
     
     SELECT DISTINCT b.sno,b.sname FROM sc a JOIN student b ON a.sno=b.sno
     WHERE a.grade<60
     
    问题2
     查询至少选修一门“李峰”老师的课程的学生姓名
     
     -- 少了一个 选课表 ,难道你叫我通过成绩表来查选课?这不科学
     
    问题3
     检索出没有被任何学生选修的课程
     
     -- 少了一个 选课表 ,难道你叫我通过成绩表来查选课?这不科学
     
    问题4
     求出少于10个学生选修的课程
     
     -- 少了一个 选课表 ,难道你叫我通过成绩表来查选课?这不科学
     
    问题5
     求出有四门课程考试不及格的学生的学生号、姓名
     
    SELECT b.sno,b.sname FROM sc a JOIN student b ON a.sno=b.sno
    GROUP BY a.sno,b.sname HAVING(SUM(CASE WHEN grade<60 THEN 1 ELSE 0 END ))>=4问题6
     求出教了三门课程以上的老师
     
     -- 少了一个 选课表
     
    问题7
     求出每一个班级中每一门课程获得最高分的学生的学号与姓名SELECT w.sno,w.sname FROM 
    (SELECT a.sno,a.sname,a.classno,b.cno FROM student a JOIN sc b ON a.sno=b.sno) w JOIN 
    (
     SELECT a.classno,b.cno,MAX(grade) FROM student a JOIN sc b ON a.sno=b.sno
     GROUP BY a.classno,b.cno 
     ) v ON w.classno=v.classno AND w.cno AND b.cno
      

  6.   

     1.select distinct sno,sname from student st where exists( select 1 from sc where sno=st.sno and grade<60) 2. select sname from student st where exists( select 1 from sc join course on sc.sno=st.sno and sc.cno=course.cno join teacher on course.tno=teacher.tno and teacher.name='李峰') 3. select cname from course left join sc on course.cno=sc.cno and sc.cno is null4. select cno from sc group by cno having count(1)<10 5. select sno,sname from student where exists ( select 1 from sc where sno=st.sno and grade<60 )group by sno,sname having count(1)=4 6. select tno,tname from teacher te join( select tno from course group by tno having COUNT(1)>3 )t on tempdb.tno=t.tno