数据库SQL语言--SELECT查询操作

更新时间:2023-11-15 18:50:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

数据库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 groupby S.Sn havingcount(*)>=1;

(3)--检索王同学没有选修的课程的课程号-- select cno from c where c.cno notin (select cno from sc,s

where sc.sno=s.sno and sn like'王%')

(4)--检索至少选修两门课程的学生学号-- selectdistinct s.sno from s,sc where sc.sno=s.sno groupby s.sno

havingcount(*)>=2;

(5)--检索全部学生都选修的课程的课程号与课程名-- select cno,cn from c wherenotexists (select*from s wherenotexists (select*from sc

where s.sno=sc.sno and c.cno=sc.cno))

(6)--检索选修了所有3学分课程的学生学号和姓名-- selectdistinct s.sno,s.sn from s,sc whereexists

(select*from c

where ct='3'and s.sno=sc.sno and c.cno=sc.cno)

2、基于“教学管理”数据库jxgl,试用SQL的查询语句表达下列查询。 (1)--统计有学生选修的课程门数-- selectcount(distinctsc.cno)fromsc;

(2)--查询选修4号课程的学生的平均年龄-- selectavg(s.age) froms,sc

wheres.sno=sc.snoandcno='4';

(3)--查询学分为3的每门课程的学生平均成绩--

selectavg(sc.score) fromc,sc,s

wheres.sno=sc.snoandc.ct='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 wheres.sn like('王%');

(7)--在SC中检索成绩为空值的学生学号和年龄-- select s.sno,s.age from s,sc where score isnull;

注意:isnull那里不能用等号?=?

(8)--查询年龄大于女同学平均年龄的男同学姓名和年龄-- select s.sn,s.age from s

where age>(selectavg(age)from s where sex='女') and sex='男';

(9)--查询年龄大于所有女同学年龄的男同学姓名和年龄-- select s.sn,s.age from s

where age>(selectmax(age)from s where sex='女') and sex='男';

(10)--检索所有比‘赵四’年龄大的学生姓名、年龄和性别-- select s.sn,s.age,s.sex 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 s.sno=sc.sno;

(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 a.cno='c001'

and exists(select * from sc b where b.cno='c002' and a.score>b.score and a.sno = b.sno)

(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 st.sno not in

(select distinct sno from sc s join course c on s.cno=c.cno join teacher t on c.tno=t.tno where tname='谌燕');

(18)--查询学过?c001?并且也学过编号?c002?课程的同学的学号、姓名-- select st.* from sc a join sc b on a.sno=b.sno join s st

on st.sno=a.sno

where a.cno='c001' and b.cno='c002' and st.sno=a.sno;

(19)--查询学过?谌燕?老师所教的所有课的同学的学号、姓名-- select st.* from sst join sc s on st.sno=s.sno join course c on s.cno=c.cno join teacher t on c.tno=t.tno where t.tname='谌燕'

(19)--查询课程编号?c002?的成绩比课程编号?c001?课程低的所有同学的

学号、姓名-- select * from s st

join sc a on st.sno=a.sno join sc b on st.sno=b.sno

where a.cno='c002' and b.cno='c001' and a.score < b.score

(20)--查询所有课程成绩小于60 分的同学的学号、姓名-- select st.*,s.score from s st join sc s on st.sno=s.sno join course c on s.cno=c.cno where s.score <60

(21)--查询没有学全所有课的同学的学号、姓名-- select * from swhere sno in (select sno from

(select st.sno,c.cno 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 sc.sno,st.sn,count(cno) from sst left join sc

on sc.sno=st.sno

group by st.sn,sc.sno having count(cno)=1;

(24)--查询男生、女生人数-- select sex,count(*) from s

group by sex;

(25)--查询姓?张?的学生名单-- select * from s where sn like '张%';

(26)--查询平均成绩大于85 的所有学生的学号、姓名和平均成绩--

select st.sno,st.sn,avg(score) from sst left join sc

on sc.sno=st.sno group by st.sno,st.sn having avg(score)>85;

(27)--查询课程名称为?数据库?,且分数低于60 的学生姓名和分数-- select sn,score from s st,sc,course c

where st.sno=sc.sno and sc.cno=c.cno and c.cn='数据库' and sc.score<60;

(28)--查询所有学生的选课情况-- select st.sno,st.sn,c.cn from s st,sc,course c where sc.sno=st.sno and sc.cno=c.cno;

(29)--查询任何一门课程成绩在70 分以上的姓名、课程名称和分数-- select st.sn,c.cn,sc.score from s st,sc,course c

where sc.sno=st.sno and sc.cno=c.cno and sc.score>70;

(30)--求选了课程的学生人数-- select count(distinct sno) from sc;

(31)--查询不同课程成绩相同的学生的学号、课程号、学生成绩--

select a.* from sc a ,sc b

where a.score=b.score and a.cno<>b.cno;

(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 sc.score<60

group by sno having count(sno)>1) group by sno;

本文来源:https://www.bwwdw.com/article/chiv.html

Top