数据库实验二——SQL查询语言

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_44096577/article/details/102689518

日常更新数据库实验报告

在这里插入图片描述
在这里插入图片描述
我们可以看到,总共有25条要求,老规矩,一个一个来,以实验一的数据为基础

1)查询全体学生的详细记录

select *
from student;

效果如图,以下只放代码,不在放图(除非特别重要的)
在这里插入图片描述

2)查询不是信息系(IS)、计算机系(CS)的学生性别、年龄、系别

select ssex,sage,sdept 
from student 
where not sdept='is' and not sdept='cs';

3)查询选修了4号课的学生学号和成绩,结果按成绩降序排列,同一个成绩按照学号的升序排列


select sno,Grade 
from sc 
where cno=004 
order by Grade asc;

4) 查询每个课程号和相应的选课人数;

select cno,count(*)
as '选课人数' 
from sc 
group by cno;

5)查询年龄18-20岁的学生学号、姓名、系别、年龄

select sno,sname,sdept,sage
from student
where sage>18 and sage<20

6)查询姓刘,且名字第三个字为“晨”的学生情况

select *
from student
where sname like('刘_晨')       

7)查询既选修1号课程,又选修2号课程的学生学号

select sno
from sc               
where cno='001' and sno in (select sno from sc where cno='002')

8)查询学生的姓名和出生年份

select sname,(2018-sage) as '出生年份' 
from student						
where (2018-sage)>0

9)查询没有成绩的学生学号和课程号

select sno,cno 
from sc where grade IS NULL	

10)查询不及格课程超过3门的学生学号

select sno,sname
from student
where sno in ( select sno from sc where grade<60 group by sno having count(*)>=3 )

11)查询选了1号课程的学生平均成绩

select avg(grade)
from sc
where cno='001'	

12)查询每个同学的总成绩

13)查询每门课的间接先修课

select course1.cno,course2.cpno
from course course1,course course2
where course1.cpno=course2.cno

14)将STUDENT,SC进行右连接

select student.* ,sc.*
from student 
right join sc on student.sno=sc.sno	

15)查询有不及格的学生姓名和所在系

select sname,sdept
from student 
where sno in(select sno from sc group by sno having min(grade)<60)

16)查询所有成绩为优秀(大于90分)的学生姓名

select sname
from student 
where sno in(select sno from sc group by sno having min(grade)>90)

17)查询和刘晨同一年龄的学生

select * 
from student 
where sage=(select sage from student where sname='刘晨')

18)查询选修了课程名为“数据库”的学生姓名和年龄

select sname,sage  
from student 
where sno in(select sno from sc where cno in(select cno from course where cname='数据库'))

19)查询其他系中比IS系所有学生年龄都小的学生名单

select * 
from student 
where sage<any(select sage from student where sdept='is') and sdept<>'is'

20)查询选修了全部课程的学生姓名

SELECT sname
from student
where sno in(select sno from sc group by sno having count(cno)=7)

21)查询选修课程1的学生集合和选修2号课程学生集合的差集

select *
from sc where cno='001'and sno not in(select sno from sc where cno='002')

22)查询选修了3号课程的学生平均年龄

select avg(sage) 
from student 
where sno in(select sno from sc where cno='003')

23)统计每门课程的学生选修人数(超过3人的才统计)。要求输出课程号和选修人数,结果按人数降序排列,若人数相同,按课程号升序排列

select distinct cno ,count(sno)
from sc
group by cno
having count(sno)>3
order by 2 desc,cno asc	

24)求年龄大于女同学平均年龄的男同学姓名和年龄

select sname ,sage 
from student where sage>(select avg(sage) from student where ssex='女')and ssex='男'

25)查询95001和95002两个学生都选修的课程的信息

select * 
from course where cno in (select cno from sc where sno='95001')
and cno in (select cno from sc where sno='95002')	

猜你喜欢

转载自blog.csdn.net/weixin_44096577/article/details/102689518