当前位置:   article > 正文

数据库实验二——SQL查询语言_查询'计算机系'和'信息系',年龄在18到20岁的学号,姓名、所在系和年龄

查询'计算机系'和'信息系',年龄在18到20岁的学号,姓名、所在系和年龄

日常更新数据库实验报告

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

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

select *
from student;
  • 1
  • 2

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

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

select ssex,sage,sdept 
from student 
where not sdept='is' and not sdept='cs';
  • 1
  • 2
  • 3

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


select sno,Grade 
from sc 
where cno=004 
order by Grade asc;
  • 1
  • 2
  • 3
  • 4
  • 5

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

select cno,count(*)
as '选课人数' 
from sc 
group by cno;
  • 1
  • 2
  • 3
  • 4

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

select sno,sname,sdept,sage
from student
where sage>18 and sage<20
  • 1
  • 2
  • 3

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

select *
from student
where sname like('刘_晨')       
  • 1
  • 2
  • 3

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

select sno
from sc               
where cno='001' and sno in (select sno from sc where cno='002')
  • 1
  • 2
  • 3

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

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

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

select sno,cno 
from sc where grade IS NULL	
  • 1
  • 2

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

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

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

select avg(grade)
from sc
where cno='001'	
  • 1
  • 2
  • 3

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

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

select course1.cno,course2.cpno
from course course1,course course2
where course1.cpno=course2.cno
  • 1
  • 2
  • 3

14)将STUDENT,SC进行右连接

select student.* ,sc.*
from student 
right join sc on student.sno=sc.sno	
  • 1
  • 2
  • 3

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

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

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

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

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

select * 
from student 
where sage=(select sage from student where sname='刘晨')
  • 1
  • 2
  • 3

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

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

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

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

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

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

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

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

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

select avg(sage) 
from student 
where sno in(select sno from sc where cno='003')
  • 1
  • 2
  • 3

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

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

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

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

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')	
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/40119
推荐阅读
相关标签
  

闽ICP备14008679号