1、 操作系统:window10
2、 MySQL 5.7
jxgl数据库如下所示:
1.1 检索年龄大于23岁的男学生的学号和姓名:
select sno,sname
from student
where sage>23;
1.2 检索至少选修一门课程的女学生姓名:
select sname
from student
where ssex='女' and sno in (select sno from sc);
1.3 检索王林不学的课程的课程号:
select distinct cno from course
where cno not in(select cno from sc where sno='2005002');
1.4 检索至少选修两门课程的学生学号:
select sno from sc
group by sno having count(*)>=2;
1.5 检索全部学生都选修的课程的课程号和课程名:
为了有查询结果,这里对表sc的内容进行调整:
select cno,cname from course
where not exists(select *
from student
where not exists(select *
from sc
where sc.cno=course.cno and sc.sno=student.sno));
1.6 检索选修了所有3学分课程的学生的平均成绩:
为了有查询结果,这里对表sc的内容进行调整:
select avg(grade)
from sc
where sno in(select sno
from student
where not exists(select * from course where ccredit=3 and not exists(
select * from sc where sc.cno=course.cno and sc.sno=student.sno)))
group by sno;
2.基于jxgl数据库,使用SQL语句表达以下查询(2中的sc表与最后修改的sc表内容一致,可参考1.6中对sc表做的最后一次修改)
2.1统计有学生选修的课程门数:
select count(distinct cno)
from sc;
2.2求选修4号课程的学生的平均年龄:
select avg(sage)
from student
where sno in (select sno from sc where cno='4');
2.3求学分为3的每门课程的学生平均成绩:
select cno,avg(grade)
from sc
where cno in(select cno from course where ccredit=3)
group by cno;
2.4统计每门课程的学生选修人数,要求超过3人的课程才统计,要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列:
select cno,count(distinct sno) as num
from sc
group by cno having num>3
order by num desc,cno asc;
2.5 检索学号比“王林”同学大而年龄比她小的学生姓名:
select sname
from student
where sno>(select s.sno from student as s where s.sname='王林')
and sage<(select s.sage from student as s where s.sname='王林');
2.6检索姓名以“王”开头的所有学生的姓名和年龄:
select sname,sage
from student
where sname like '王%';
2.7 在sc表中检索成绩为空值的学生的学号和课程号:
为了有查询结果,这里对表sc的内容进行调整:
select sno,cno
from sc
where grade is null;
注意:在对空值进行判断时,要使用where grade is null,而不可以使用where grade=null,因为null不是一个具体的数值,无法用等于号进行判断。
2.8 求年龄大于女学生平均年龄的男学生的姓名和年龄:
select sname,sage
from student
where ssex='男' and sage>(select avg(sage) from student where ssex='女');
2.9 求年龄大于所有女学生年龄的男学生的姓名和年龄:
select sname,sage
from student
where ssex='男' and sage>(select max(sage) from student where ssex='女');
2.10 检索选修4门以上课程的学生总成绩(不统计不及格课程),并要求按总成绩的降序排列出来:
为了有查询结果,这里对表sc的内容进行调整:
select sno,sum(grade) as sum_grade
from sc
where grade>=60
group by sno
having count(*)>4
order by sum_grade desc;