数据库查询实验

3. 查询选修1号课程的学生学号和成绩,并要求对查询结果按成绩的降序排列,如果成绩相同则按学号的升序排列;

select sno,grade

from sc

where cno='1'

orderby grade desc,sno asc

 

6. 查询缺少了成绩的学生的学号和课程号。

select sno,cno

from sc

where grade=null

 

 

7. 查询每个学生的学号,姓名,选修的课程名,成绩;

select student.sno,Sname,cname,grade

from student,course,sc

where student.Sno=sc.Sno and course.Cno=sc.cno

 

10.  查询每门课程的先行课程的课程名称,学分;

select student.sno,sname,cname,grade

from student,course,sc

where student.sno=sc.sno and  sc.cno=course.cno

 

 

12. 查询每一门课的间接先行课的课程名称;

selectfirst.cno,second.cno,first.cname,second.cname

from course first,course second

wherefirst.cpno=second.cno

 

13. 查询所在系部为“MA”且选修了高等数学课程的学生姓名,年龄,性别;

select sname,sage,ssex

from student,course

where sdept='MA'and cname='数学'

 

 

15. 查询选修了数据结构课程,且成绩在90分以上的学生姓名,年龄;

selectdistinct sage,sname 

from SC,student 

where sc.grade>90 and cno='5' 

 and student.Sno=sc.Sno 

 

 

20. 查询选修了全部课程的学生的姓名;

select Sname 

from  student 

wherenotexists 

     (select* 

       from Course 

       where  notexists 

                 (select* 

                  from sc 

                   where sno=student.Sno 

                          and cno=Course.Cno 

                          ) 

                          ) 

21. 查询至少选修了学号为“201215121”的学生所选修的全部课程的学生学号和姓名;

selectdistinct sno 

from sc scx 

wherenotexists 

         (select* 

           from sc scy 

           where scy.Sno='201215121'and 

                 notexists 

                 (select* 

                   from sc scz 

                   where scz.sno=scx.sno and 

                   scz.Cno=scy.cno 

                   ) 

          ) 

 

25. 查询选修了操作系统课程的学生人数;

selectcount(course.cno)as'number'

from course,sc

where cname='操作系统'and sc.cno=course.cno

29. 查询选修了数据库课程的最高分,平均分;

selectmax(grade)as'max',avg(grade)as'avg'

from sc,course

where sc.cno=course.cno and cname='数据库'

33. 查询每个学生的学号,姓名,所获得的总学分(成绩大于等于60,则获得该门课程的学分);

SELECT sc.Sno,Sname,COUNT(Ccredit)as'add credit'

FROM student,course,sc

WHERE Grade>60 and student.Sno=sc.Sno and sc.Cno=course.Cno

GROUPBY sc.Sno,Sname

 

 

 

 

 

Part-2

4.找出工程项目J2使用的各种零件的名称及其数量

select pname,qty

from p,spj

where p.pno=spj.pno and jno='j2'

6.找出使用上海产的零件的工程名称

selectdistinct jname

from s,j,spj

where spj.sno='s5'and s.sno=spj.sno and j.jno=spj.jno

10.找出供应工程J1零件为红色的供应商号码

select distinct SNO '供应商号码'

  from SPJ

  where JNO = 'J1' and exists(select * from P

 where  SPJ.PNO = P.PNO and p.COLOR = '红' );

17.找出没有使用天津供应商生产的红色零件的工程名称

select jname

from j

where jname notin

(

select jname

from s,p,spj

where p.pno=spj.pno and spj.jno=j.jno and j.city='天津'and color='红'

)  );

22.找出提供零件种类超过了2种的供应商号码

 

select sno

from spj

groupby sno

havingcount(distinct pno)>2

25.找出为3个以上的工程提供零件的供应商名称

selectdistinct sname,s.sno

from s,spj

where s.sno=spj.sno and spj.sno in

(select sno

from spj

groupby sno

havingcount(jno)>=3

)

 

猜你喜欢

转载自blog.csdn.net/qq_42070071/article/details/80715799