欢迎光临散文网 会员登陆 & 注册

数据库原理与应用(7)PTA查询部分编程题汇编1

2022-05-09 15:23 作者:洛溪い  | 我要投稿

【简单查询1】

(1)查询所有学生的学号、姓名、性别和出生日期。

select sno,sname,ssex,bday from students;


(2)查询前3门课程的课号及课程名称。

select cno,cname from course limit 0,3;


(3)查询2050年所有学生的姓名及年龄,要求结果中列名显示中文。

select sname 姓名,2050-year(bday) 年龄 from students;


(4)查询至2050年所有年龄小于等于55岁的女生的学号和姓名。

select sno 学号,sname 姓名,ssex 性别 from students where 2050-year(bday)<=55 and ssex='女';


(5)查询“信息学院”的学生姓名、性别和出生日期。

select sname,ssex,bday from students where sdept='信息学院';


(6)查询Students表中的所有系名,要求结果中系名不重复。

select distinct sdept from students;


(7)查询“0000010”课程的课名、先修课号和学分。

select cname,cpno,ccredit from course where cno='0000010';


(8)查询成绩在80~90分之间的选课成绩情况。

select * from sc where score>=80 and score<=90;


(9)查询成绩为69分、79分或89分的记录。

select * from sc where score in(69,79,89);


(10)查询在1970年1月1日之前出生的男教师信息。

select * from teachers where tbirthday<='1970-1-1' and tsex='男';


(11)输出有成绩的学生学号和课号。

select sno,cno from sc where score is not null;


(12)查询所有姓“刘”的学生信息。

select * from students where sname like '刘%';


(13)查询生源地不是“山东”省的学生信息。

select * from students where bplace not like '山东%';


(14)查询名字中含有“明”字的男生的学生姓名和班级。

select sname,class from students where sname like '%明%';


(15)查询姓名是两个字的学生信息。

select * from students where sname like '__';


(16)查询非信息学院和机电学院的学生信息。

select * from students where sdept not like '信息学院' and sdept not like '机电学院';


(17)查询学生表中没有联系电话的学生信息。

select * from students where phone is null;


【简单查询2 聚合函数】

(18)从学生表统计总的学生人数。

select count(*) 人数 from students;


(19)统计有学生选修的课程的门数,多人选修同一门只算一门。

select count(distinct cno) 门数 from sc;


(20)计算“0000001”课程的平均分、最高分和最低分。

select avg(score) 平均分, max(score) 最高分, min(score) 最低分 from sc where cno='0000001';


(21)查询选修了“0000008”课程的学生的学号及其成绩,查询结果按分数降序排列。

select sno,score from sc where cno='0000008' order by score desc;


(22)查询成绩不及格的学生学号、课号和成绩,并按成绩降序排列。

select sno,cno,score from sc where score<60 order by score desc;


【简单查询3 分组查询】

(23)查询各个课程号及相应的选课人数。

select cno 课程号,count(sno) 选课人数 from sc group by cno;


(24)统计每门课程的选课人数和最高分。

select cno 课程号, count(sno) 选课人数, max(score) 最高分 from sc group by cno;


(25)查询选修了3门以上课程的学生学号。

select sno 学号, count(cno) 选课门数 from sc group by sno having count(cno)>3;


(26)统计输出各系学生的人数。

select sdept 系, count(sno) 人数 from students group by sdept;


(27)统计每个学生的选课门数和考试总成绩,并按选课门数升序排列。

select sno 学号, count(cno) 选课门数, sum(score) 考试总成绩 from sc group by sno order by 选课门数 asc;


(28)统计各系的男、女生人数。

select sdept 系别, ssex 性别, count(sno) 人数 from students group by sdept,ssex order by sdept;


(29)统计各班男、女生人数。

select class 班级, ssex 性别, count(sno) 人数 from students group by class,ssex order by class;


(30)统计各系的老师人数,并按人数升序排序。

select tdept 系别, count(tno) 教师人数 from teachers group by tdept order by count(tno) asc;


(31)统计不及格人数超过3人的课程号和人数。

select cno 课程号, count(*) 不及格人数 from sc where score<60 group by cno having count(*)>3;


(32)查询信息学院的男生信息,查询结果按出生日期升序排序,出生日期相同的按生源地降序排序。

select * from students where sdept='信息学院' and ssex='男' order by bday, bplace desc;


(33)统计选修人数最多的3门课。

select cno 课程号, count(*) 选修人数 from sc group by cno order by count(*) desc limit 0,3;


【多表连接查询】

(34)查询信息学院女学生的学生学号、姓名、课号及考试成绩。

select students.sno,sname,cno,score from students join sc on students.sno=sc.sno where sdept='信息学院' and ssex='女';


(35)查询“陈红”同学所选课程的成绩,列出课号和成绩(不考虑重名)。

select cno,score from sc join students on sc.sno=students.sno where sname='陈红';


(36)查询“王珊”老师所授课程的课程名称。

select distinct cname from course inner join teaching on course.cno=teaching.cno inner join teachers on teaching.tno=teachers.tno where teachers.tname='王珊';


(37)查询女教师所授课程的课程号和课程名称。

select distinct course.cno,cname from course inner join teaching on course.cno=teaching.cno inner join teachers on teaching.tno=teachers.tno where tsex='女';


(38)查询至少选修2门课程的女生姓名。

select students.sname from students join sc on students.sno=sc.sno where students.ssex='女' group by sname having count(*)>2;


(39)查询选修课名中含有“数据库”三个字的课程且成绩在80~90分之间的学生学号及成绩。

select sno,score from sc left join course on sc.cno=course.cno where cname like '%数据库%' and score between 80 and 90;


(40)查询选修“0000011”课程的学生至2050年时平均年龄。

select avg(2050-year(bday)) 平均年龄 from students join sc on students.sno=sc.sno where cno='0000011';


(41)列出所有学生的选课情况(包括学号,姓名,课号,成绩),结果中包括没有选课的学生。

select students.sno,sname,cno,score from sc right outer join students on sc.sno=students.sno;


(42)查询没有选课的学生学号和姓名。

select sno,sname from students where sno not in(select sno from sc);

数据库原理与应用(7)PTA查询部分编程题汇编1的评论 (共 条)

分享到微博请遵守国家法律