数据库原理及应用上机实验三

  |  

实验名称:实验三 数据高级查询

实验目的

掌握复杂数据查询操作。

实验步骤与调试过程(请用简单的文字描述)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
对各表中的数据进行不同条件的连接查询和嵌套查询;
(1)查询每个学生及其选课情况;
(2)查询每门课的间接先修课
(3)将STUDENT,SC进行右连接
(4)查询既选修了2号课程又选修了3号课程的学生姓名、学号;
(5)查询和刘晨同一年龄的学生
(6)选修了课程名为“数据库”的学生姓名和年龄
(7)查询其他系比IS系任一学生年龄小的学生名单
(8)查询其他系中比IS系所有学生年龄都小的学生名单
(9)查询选修了全部课程的学生姓名
(10)查询计算机系学生及其性别是男的学生
(11)查询选修课程1的学生集合和选修2号课程学生集合的差集
(12)查询李丽同学不学的课程的课程号
(13)查询选修了3号课程的学生平均年龄
(14)求每门课程学生的平均成绩
(15)统计每门课程的学生选修人数(超过3人的才统计)。要求输出课程号和选修人数,结果按人数降序排列,若人数相同,按课程号升序排列
(16)查询学号比刘晨大,而年龄比他小的学生姓名。
(17)求年龄大于所有女同学年龄的男同学姓名和年龄

实验结果(上传实验结果截图或者简单文字描述)

1
2
3
4
1.经过实验可以知道求总数时可以用count()函数。
2.在进行分组操作时,用group by 时要用having来限制条件。
3.order by是排序要求 desc是降序 ,asc是升序。
4.any()函数是任意的意思,all()是所有。

疑难小结(总结个人在实验中遇到的问题或者心得体会)

1
2
3
1.在进行求总数操作时,由于一开始我不知道有count函数,很是迷茫,后来查阅了资料之后,才得知。
2.在进行将STUDENT,SC进行右连接的操作时,由于少打了一个关键字,总是报错,后来检查了几遍之后才找到问题。
3.我对SQL语言还有很多地方不是很熟练,需要勤加练习。

实验详细操作步骤或程序清单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

(1) 查询每个学生及其选课情况

select student.sno,sname,ssex,sage,sdept,cno,grade
from student,sc
where student.sno=sc.sno

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

select first.cno,second.cpno
from course first,course second
where first.cpno=second.cno

3) 将STUDENT,SC进行右连接

select student.sno,sname,ssex,sage,sdept,cno,grade
from student right outer join sc on student.sno=sc.sno
4) 查询既选修了2号课程又选修了3号课程的学生姓名、学号

select student.sno,sname
from student inner join sc on student.sno=sc.sno
where cno='3' and sc.sno in
(select sno
from sc
where cno='2')

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

select student.sno,sname
from student
where sname!='刘晨' and sage=
(select sage
from student
where sname='刘晨')

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

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

7)查询其他系比IS系任一学生年龄小的学生名单

select student.sno,sname
from student
where sdept<>'IS' and
sage<any
(select sage
from student
where sdept='IS')

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

select student.sno,sname
from student
where sdept<>'IS' and
sage<all
(select sage
from student
where sdept='IS')

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

select sname
from student
where Sno in
(select Sno from SC
group by Sno
having count(*) = (select count(*) from course ))

10)查询计算机系学生及其性别是男的学生

select student.sno,sname
from student
where sdept='IS' and ssex='男'

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

select sno
from sc
where cno='1' except
select sno
from sc
where cno='2'

12)查询李丽同学不学的课程的课程号

select cno
from course
where cno not in
(select cno
from sc
where sno in
(select sno
from student
where sname='李丽'))

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

select AVG(sage) as avgsage
from student inner join sc on student.sno=sc.sno
where cno='3'

14)求每门课程学生的平均成绩

select cno,AVG(grade) as avggrade
from sc
group by cno

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

select course.cno '课程号', count(sc.sno) '人数'
from course,sc
where course.cno=sc.cno
group by course.cno having count(sc.sno)>3 order by COUNT(sc.sno) desc,course.cno asc

16)查询学号比刘晨大,而年龄比他小的学生姓名

select sname
from student
where sno>
(select sno from student where sname='刘晨')and
sage<(select sage from student where sname='刘晨')

17)求年龄大于所有女同学年龄的男同学姓名和年龄

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

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. 实验名称:实验三 数据高级查询
  2. 2. 实验目的
  3. 3. 实验步骤与调试过程(请用简单的文字描述)
  4. 4. 实验结果(上传实验结果截图或者简单文字描述)
  5. 5. 疑难小结(总结个人在实验中遇到的问题或者心得体会)
  6. 6. 实验详细操作步骤或程序清单
,
字数统计:87.6k 载入天数...载入时分秒...