数据库——数据查询语句

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

select first.cno,second.cpno,second.cname

from course first,course second

where first.cpno=second.cno;

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

select sname,sage,ssex

from student,course,sc

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

and sdept='ma' and cname='数学'

查询选修了数据结构课程,且成绩在90分以上的学生学号;

select sno

from course,sc

where course.cno=sc.cno and cname='数据结构' and grade>90;

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

select sname,sage

from course,sc,student

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

and cname='数据结构' and grade>90;

查询选修了数据结构课程的学生学号,姓名,成绩;

select sc.sno,sname,grade

from course,sc,student

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

and cname='数据结构';

查询所在系部为“MA”的女生人数;P98

select count(distinct sno)

from student

where sdept='ma' and ssex='女';

查询选修了2号课程的学生姓名;

select sname

from student,sc

where student.sno=sc.sno and cno='2';

查询没有选修2号课程的学生姓名;

select sname

from student

where not exists(

select sno

from sc

where student.sno=sc.sno and cno='2');

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

select sname

from student,sc

where student.sno=sc.sno

group by sname

having count(cno)>=7;

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

select distinct sno

from sc scx

where not exists(

select *

from sc scy

where scy.sno='201215121' and

not exists

(select *

from sc scz

where scz.sno=scx.sno and scz.cno=scy.cno));

 

查询学生的总人数;(P97聚集函数)

select count(*)

from student;

查询每个系部的学生人数;

select count(sno) as number,sdept

from student

group by sdept;

查询选修了1号课程的学生人数;

select count(sno) as number

from sc

where cno='1';

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

select count(sc.sno) as number

from course,sc

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

查询课程的课程号和选修该课程的人数;

select count(sc.sno) as number,cno

from sc

group by cno;

查询选修课超过3 门课的学生学号;

select sno

from sc

group by sno

having count(*)>3;

查询选修1号课程的最高分,平均分;

select max(grade) as max,avg(grade) as avg

from sc

where cno='1';

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

select max(grade) as max,avg(grade) as avg

from sc,course

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

查询系别为“CS“,选修2号课程的平均分;

select avg(grade) as avg

from sc,student

where sc.sno=student.sno and sdept='cs' and cno='2';

查询每个学生的学号,姓名,所修课程的平均分;

select sc.sno,sname,avg(grade)as avg

from student,sc

where student.sno=sc.sno

group by sc.sno,sname

查询指定学号的学生所获得的总学分。(成绩大于等于60,则获得该门课程的学分;学号自己给定);

select student.sno,sname,sum(ccredit) as sum

from student,sc,course

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

 and grade>60 and sc.sno='201215121'

group by student.sno,student.sname

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

select student.sno,sname,sum(ccredit) as sum

from student,sc,course

where student.sno=sc.sno and course.cno=sc.cno and grade>60

group by student.sno,student.sname

查询每门课程的平均分,最高分

select avg(grade)as avg,max(grade) as max

from sc

group by cno;

猜你喜欢

转载自blog.csdn.net/hml666888/article/details/80978823