Oracle SQL查询练习题

时间:2023-02-23 14:57:09

今天给大家分享几道Oracle SQL查询练习题。大家不要小看这些题,这些题有很大的价值。现在闲话少说,马上给题目给大家。

 

 1、首先给出表结构,表结构需要大家去分析一下他们之间的逻辑关系。

         Student(Sno,Sname,Sage,Ssex) 学生表

         Course(Cno,Cname,Tno) 课程表

         SC(Sno,Cno,score) 成绩表

         Teacher(Tno,Tname) 教师表

2、问题

   (1)查询“c001”课程比“c002”课程成绩高的所有学生的学号;

   (2)查询平均成绩大于60分的同学的学号和平均成绩;

   (3)查询所有同学的学号、姓名、选课数、总成绩;

   (4)查询姓“李”的老师的个数;

   (5)查询没学过“叶平”老师课的同学的学号、姓名;

   (6)查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;

   (7)查询学过“叶平”老师所教的所有课的同学的学号、姓名;

   (8)查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名;

   (9)查询所有课程成绩小于60分的同学的学号、姓名;

   (10)查询没有学全所有课的同学的学号、姓名;

3. 要求:

   (1)请严格用Oracle对以上四张表进行建表。

   (2)请严格按照Oracle SQL语法完成以上10道题

4. 答案:

   (1)查询“c001”课程比“c002”课程成绩高的所有学生的学号答案:

       

          select sno

          from sc

          where cno = 'c001'

          and sc.score >any (select score from sc where cno = 'c002');

  

  (2)查询平均成绩大于60分的同学的学号和平均成绩;答案:

       

          select sno, avg(score)

          from sc

          group by sno

          having avg(score) > 60;

   

   (3)查询所有同学的学号、姓名、选课数、总成绩;答案:

       

          select sc.sno,sname,sum(score),count(cno)

          from sc, student

          where sc.sno = student.sno

          group by sc.sno,sname;

  

    (4)查询姓“李”的老师的个数;答案:

       

          select count(tno)

          from teacher

          where tname like '李%';

   

    (5)查询没学过“叶平”老师课的同学的学号、姓名;答案:

       

          select sc.sno,sname

          from student, teacher, course, sc

          where teacher.tname <> '叶平'

          and teacher.tno = course.tno

          and course.cno = sc.cno

          and sc.sno = student.sno;

     

   (6)查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;答案:

        

         select sc.sno,sname

          from sc,student

          where sc.cno = 'c001'

          and student.sno = sc.sno

          and sc.sno in (select sno from sc where sc.cno = 'c002');

      

     (7)查询学过“叶平”老师所教的所有课的同学的学号、姓名;答案:

        

          select sc.sno,sname

          from student, teacher, course, sc

          where teacher.tname = '叶平'

          and teacher.tno = course.tno

          and course.cno = sc.cno

          and sc.sno = student.sno;

      

     (8)查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名;答案:

        

          select sno from sc

          where cno = 'c002'

          and sc.score 60;

     

     (10)查询没有学全所有课的同学的学号、姓名;答案:

       

        select sc.sno,student.sname

        from sc,student

        where sc.sno = student.sno

        group by sc.sno,student.sname

        having count(cno) < (select count(cno) from course);