当前位置:文档之家› 实验4--SQL语言--SELECT查询操作

实验4--SQL语言--SELECT查询操作

实验4--SQL语言--SELECT查询操作
1、基于‚教学管理‛数据库jxgl,试用SQL的查询语句表达下列查询。

(1)--检索年龄大于23的男学生的学号和姓名--
select sno,sn from s where sex='男'and age > 23
(2)--检索至少选修一门课程的女学生姓名--
select sn from S,SC
where sex='女' AND S.Sno=SC.Sno
group by S.Sn having count(*)>=1;
(3)--检索王同学没有选修的课程的课程号--
select cno from c
where o not in
(select cno from sc,s
where sc.sno=s.sno and sn like'王%')
(4)--检索至少选修两门课程的学生学号--
select distinct s.sno from s,sc
where sc.sno=s.sno
group by s.sno
having count(*)>=2;
(5)--检索全部学生都选修的课程的课程号与课程名--
select cno,cn from c
where not exists
(select*from s
where not exists
(select*from sc
where s.sno=sc.sno and o=o))
(6)--检索选修了所有3学分课程的学生学号和姓名--
select distinct s.sno,s.sn from s,sc
where exists
(select*from c
where ct='3'and s.sno=sc.sno and o=o)
2、基于“教学管理”数据库jxgl,试用SQL的查询语句表达下列查询。

(1)--统计有学生选修的课程门数--
select count(distinct o)from sc;
(2)--查询选修4号课程的学生的平均年龄--
select avg(s.age)
from s,sc
where s.sno=sc.sno and cno='4';
(3)--查询学分为3的每门课程的学生平均成绩--
select avg(sc.score)
from c,sc,s
where s.sno=sc.sno and c.ct='3';
(4)--统计每门课程的学生选修人数(超过3人的课程才统计)。

要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,则按课程号升序排列—
select cno,count(sno)from sc
group by cno
having count(sno)>3
order by count(sno)desc,(cno)asc;
(5)--检索学号比钱横同学大,而年龄比他小的学生姓名--
select sn from s
where sno>(select sno from s where sn='钱横')
and age<(select age from s where sn='钱横');
(6)--检索姓名以王打头的所有学生的姓名和年龄--
select sn,age from s
where s.sn like('王%');
(7)--在SC中检索成绩为空值的学生学号和年龄--
select s.sno,s.age from s,sc
where score is null;
注意:is null那里不能用等号‚=‛
(8)--查询年龄大于女同学平均年龄的男同学姓名和年龄--
select s.sn,s.age from s
where age>(select avg(age)from s where sex='女')
and sex='男';
(9)--查询年龄大于所有女同学年龄的男同学姓名和年龄--
select s.sn,s.age from s
where age>(select max(age)from s where sex='女')
and sex='男';
(10)--检索所有比‘赵四’年龄大的学生姓名、年龄和性别--
select s.sn,s.age,s.sex from s
where age>(select max(age)from s where sn='赵四');
(11)--检索选修‚‛课程的学生中成绩最高的学生的学号--
select sno,score from sc
where score=(select max(score)from sc where cno='2');
(12)--检索学生姓名及其所选修课程的课程号和成绩--
select sn,cno,score from s,sc
where s.sno=sc.sno;
(13)--检索学生姓名及其所选修课程的课程号和成绩--select sno,sum(score)from sc
where score>=60
and sno in(select sno from sc
group by sno
having count(sno)>4)
group by sno
order by sum(score)desc;。

相关主题