admin 管理员组文章数量: 887021
2024年1月18日发(作者:下载的html文件怎么打开)
数据库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 =
groupby havingcount(*)>=1;
(3)--检索王同学没有选修的课程的课程号--
select cno from c
where notin
(select cno from sc,s
where = and sn like'王%')
(4)--检索至少选修两门课程的学生学号--
selectdistinct from s,sc
where =
groupby
havingcount(*)>=2;
(5)--检索全部学生都选修的课程的课程号与课程名--
select cno,cn from c
wherenotexists
(select*from s
wherenotexists
(select*from sc
where = and =))
(6)--检索选修了所有3学分课程的学生学号和姓名--
selectdistinct , from s,sc
whereexists
(select*from c
where ct='3'and = and =)
2、基于“教学管理”数据库jxgl,试用SQL的查询语句表达下列查询。
(1)--统计有学生选修的课程门数--
selectcount()fromsc;
(2)--查询选修4号课程的学生的平均年龄--
selectavg()
froms,sc
=cno='4';
(3)--查询学分为3的每门课程的学生平均成绩--
selectavg()
fromc,sc,s
=='3';
(4)--统计每门课程的学生选修人数(超过3人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,则按课程号升序排列—
select cno,count(sno)from sc
groupby cno
havingcount(sno)>3
orderbycount(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
like('王%');
(7)--在SC中检索成绩为空值的学生学号和年龄--
select , from s,sc
where score isnull;
注意:isnull那里不能用等号‚=‛
(8)--查询年龄大于女同学平均年龄的男同学姓名和年龄--
select , from s
where age>(selectavg(age)from s where sex='女')
and sex='男';
(9)--查询年龄大于所有女同学年龄的男同学姓名和年龄--
select , from s
where age>(selectmax(age)from s where sex='女')
and sex='男';
(10)--检索所有比‘赵四’年龄大的学生姓名、年龄和性别--
select ,, from s
where age>(selectmax(age)from s where sn='赵四');
(11)--检索选修‚‛课程的学生中成绩最高的学生的学号--
select sno,score from sc
where score=(selectmax(score)from sc where cno='2');
(12)--检索学生姓名及其所选修课程的课程号和成绩--
select sn,cno,score from s,sc
where =;
(13)--检索学生姓名及其所选修课程的课程号和成绩--
select sno,sum(score)from sc
where score>=60
and sno in(select sno from sc
groupby sno
havingcount(sno)>4)
groupby sno
orderbysum(score)desc;
(14)--查询‚c001‛课程比‚c002‛课程成绩高的所有学生的学号--
select * from SC a
where ='c001'
and exists(select * from sc b where ='c002' and >
and = )
(15)--查询平均成绩大于60 分的同学的学号和平均成绩--
select sno,avg(score)
from sc
group by sno
having avg(score)>60;
(16)--查询姓‚刘‛的老师的个数--
select count(*)
from teacher
where tn like '刘%';
(17)--查询没学过‚谌燕‛老师课的同学的学号、姓名--
select * from sst where not in
(select distinct sno from sc s join course c on =
join teacher t on = where tname='谌燕');
(18)--查询学过‚c001‛并且也学过编号‚c002‛课程的同学的学号、姓名--
select st.* from sc a
join sc b on =
join s st
on =
where ='c001' and ='c002' and =;
(19)--查询学过‚谌燕‛老师所教的所有课的同学的学号、姓名--
select st.* from sst join sc s on =
join course c on =
join teacher t on =
where ='谌燕'
(19)--查询课程编号‚c002‛的成绩比课程编号‚c001‛课程低的所有同学的
学号、姓名--
select * from s st
join sc a on =
join sc b on =
where ='c002' and ='c001' and <
(20)--查询所有课程成绩小于60 分的同学的学号、姓名--
select st.*, from s st
join sc s on =
join course c on =
where <60
(21)--查询没有学全所有课的同学的学号、姓名--
select * from swhere sno in
(select sno from
(select , from s st
cross join course c
minus
select sno,cno from sc))
(22)--查询每门课程被选修的学生数--
select cno,count(sno)
from sc
group by cno;
(23)--查询出只选修了一门课程的全部学生的学号和姓名--
select ,,count(cno) from sst
left join sc
on =
group by , having count(cno)=1;
(24)--查询男生、女生人数--
select sex,count(*)
from s
group by sex;
(25)--查询姓‚张‛的学生名单--
select * from s
where sn like '张%';
(26)--查询平均成绩大于85 的所有学生的学号、姓名和平均成绩--
select ,,avg(score) from sst
left join sc
on =
group by ,
having avg(score)>85;
(27)--查询课程名称为‚数据库‛,且分数低于60 的学生姓名和分数--
select sn,score from s st,sc,course c
where = and = and ='数据库' and <60;
(28)--查询所有学生的选课情况--
select ,, from s st,sc,course c
where = and =;
(29)--查询任何一门课程成绩在70 分以上的姓名、课程名称和分数--
select ,, from s st,sc,course c
where = and = and >70;
(30)--求选了课程的学生人数--
select count(distinct sno) from sc;
(31)--查询不同课程成绩相同的学生的学号、课程号、学生成绩--
select a.* from sc a ,sc b
where = and <>;
(32)--检索至少选修两门课程的学生学号--
select sno from sc
group by sno
having count(sno)>1;
(33)--查询两门以上不及格课程的同学的学号及其平均成绩--
select sno,avg(score)from sc
where sno in
(select sno from sc where <60
group by sno having count(sno)>1)
group by sno;
版权声明:本文标题:数据库SQL语言--SELECT查询操作 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1705525274h488543.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论