一、建表语句
create table student
(
sno char(20) primary key,
sname char(20) not null,
ssex char(20),
sage int,
sdept char(20)
);
create table course(
cno char(20) primary key,
cname char(20) not null,
cpno char(20),
ccredit int ,
foreign key(cpno) references course(cno)
);
create table sc(
sno char(20) not null,
cno char(20) not null,
grade int,
primary key(sno,cno),
foreign key(sno) references student(sno),
foreign key(cno) references course(cno)
);
insert into student values('201215121','李勇','男',20,'cs');
insert into student values('201215122','李晨','女',19,'cs');
insert into student values('201215123','王敏','女',18,'ma');
insert into student values('201215125','张立','男',19,'is');
insert into course values('1','数据库','5',4);
insert into course values('2','数学',null,2);
insert into course values('3','信息系统','1',4);
insert into course values('4','操作系统','6',3);
insert into course values('5','数据结构','7',4);
insert into course values('6','数据处理',null,2);
insert into course values('7','PASCAL语音','6',4);
insert into sc values('201215121','1',92);
insert into sc values('201215121','2',85);
insert into sc values('201215121','3',88);
insert into sc values('201215122','2',90);
insert into sc values('201215122','3',80);
select * from student;
select * from course;
select * from sc;
二、Sql查询语句
一
1 查询所有年龄在岁以下的学生姓名及其年龄
select sname,sage from student where sage<20;
2 查询考试成绩不及格的学生的学号
select distinct sno from sc where grade<60;
3 查询年龄在20~23岁之间的学生姓名、系别和年龄
select sname,sdept,sage from student where sage between 20 and 30;
4 查询年龄不在20~23之间的学生姓名、系别和年龄
select sname,sdept,sage from student where sage not between 20 and 30;
5 查询计算机科学系(CS)、数学系(MA)和信息系(IS)学生的姓名和性别
select sname,ssex from student where sdept in('cs','ma','is');
6 查询既不是计算机科学系、数学系、也不是信息系的学生的姓名和性别
select sname,ssex from student where sdept not in('cs','ma','is');
7 查询学号为的学习的详细情况
select * from student where sno like '201215121';
select * from student where sno = '201215121';
8 查询所有姓刘的学生的姓名、学号和性别
select sname,sno,ssex from student where sname like '刘%';
9 查询姓“欧阳”且全名为三个汉字的学生的姓名
select sname from student where sname like '欧阳_';
10 查询名字中第二个字为“阳”的学生姓名和学号
select sname,sno from student where sname like '_阳%';
11 查询所有不姓刘的学生的姓名、学号和性别
select sname,sno from student where sname not like '刘%';
12 查询DB_Destign课程的学号和学分
select cno,ccredit from course where cname like 'DB\_Design' escape'\';
13 查询以"DB_"开头的,且倒数第三个字符为i的课程的详细情况。
select * from course where cname like 'DB\_%i__' escape'\';
14 某些学生选修课程后没有参加考试,所有有选课记录,但是没有考试成绩。查询缺少成绩的学生的学号和相应的课程号
select sno,cno from sc where grade is null;
15 查询有成绩的学生学号和课程号
select sno,cno from sc where grade is not null;
16 查询计算机科学系年龄在岁以下的学生姓名
select sname from student where sdept='cs' and sage<20;
模仿语句:
查询年龄在20~23岁之间的男学生姓名、系别和年龄
select sname,sdept,sage from student where ssex = '男' and sage between 20 and 30 ;
查询以"DB_"开头的,且包含字符为i的课程的详细情况。
select * from course where cname like 'DB\_%i%' escape'\';
查询所有不姓伟的学生的姓名、学号和性别
select sname,sno from student where sname not like '%伟%';
二 order by 子句
1 查询选修了号课程的学生的学号及成绩,查询结果按分数的降序排列
select sno,grade from sc where cno='3' order by grade DESC;
2 查询全体学生的情况,查询结果按所在的系的系号进行排列,同一系中的学生按年龄排列
select * from student order by sdept,sage DESC;
模仿语句:
查询全体学生的情况,查询结果按所年龄排序,年龄相同按学号排序
select * from student order by sage DESC,sno DESC;
注:以上句形成对比
三 聚集函数
1 查询学生总人数
select COUNT(*) from student ;
2 查询选修了课程的学生人数
select COUNT(Distinct sno) from sc;
3 计算选修号课程的学生平均成绩
select AVG(grade) from sc where cno = '1';
4 查询选修号课程的学生的最高分数
select MAX(grade) from sc where cno='1';
5 查询学生选修课程的总学分
select SUM(ccredit) from sc,course where sno='201215012' and sc.cno = course.cno;
模仿语句:
查询性别为男的学生中的最高成绩
select max(grade) from sc,student where sc.sno=student.sno and student.ssex='男' ;
四 ground by 字句
1 求各个课程号及相应的选课人数
select cno,COUNT(sno) from sc group by cno;
2 查询选修了三门以上的课程的学生学号
select sno from sc group by sno having COUNT(*)>3;
3 查询平均成绩大于等于分的学生学号和平均成绩
select sno,AVG(Grade) from sc group by sno having AVG(grade)>=90;
模仿语句:
查询平均成绩小于等于分大于等于的学生学号和平均成绩
select sno,AVG(Grade) from sc group by sno having AVG(grade)<=85 and AVG(grade)>=60;
一、连接查询
1.等值连接
3.49 查询每个学生及其选修课程的情况
select student.*,sc.* from student,sc where student.sno=sc.sno;
3.50 自连接
select student.sno,sname,ssex,sage,sdept,cno,grade from student,sc where student.sno = sc.sno;
3.51 查询选修课号课程且成绩在分以上的所以学生的学号和姓名
select student.sno,sname from student,sc where student.sno=sc.sno and sc.cno='2' and sc.grade>90;
2.自身连接
3.52 查询每一门课的间接先修课(先修课的先修课)
select first.cno,second.cpno from course first, course second where first.cpno=second.cno;
3.外连接
3.53 查询每个学生及其选修课程的情况
select student.sno,sname,ssex,sage,sdept,cno,grade from student left outer join sc on (student.sno=sc.sno);
4.多表连接
3.54 查询每个学生的学号、姓名、选修的课程名及成绩
select student.sno,sname,cname,grade from student,sc,course where student.sno =sc.sno and sc.cno =course.cno;
模仿查询
查询大于平均成绩的学生姓名
select sname from student where sno in( select sno from sc where grade>(select AVG(grade) from sc));
查询学生 考试成绩最低的分数
select sname,min(grade) as 最低分 from student,sc where student.sno = sc.sno group by (sname);
二、嵌套查询
1.带有IN的谓语的子查询
3.55 查询与“李晨”在同一个系的学习的学生
select sno,sname,sdept from student where sdept in(select sdept from student where sname='李晨');
此处可将 in 换为 =
select sno,sname,sdept from student where sdept =(select sdept from student where sname='李晨');
自连接方式:
select s1.sno,s1.sname,s1.sdept from student s1,student s2 where s1.sdept =s2.sdept and s2.sname='李晨';
3.56 查询选修了课程名为“信息系统”的学生号和姓名
select sno,sname from student where sno in(select sno from sc where cno in(select cno from course where cname='信息系统'));
连接查询方式:
select student.sno,sname from student,sc,course where student.sno =sc.sno and sc.cno = course.cno and course.cname = '信息系统';
模仿查询
查询大于平均成绩的学号
select sno from sc where grade>(select AVG(grade) from sc);
2.带有比较运算符的子查询
3.57 找出每个学生超过他自己选修课程平均成绩的课程号
select sno,cno from sc x where grade >=(select AVG(grade) from sc y where y.sno=x.sno);
3.带有ANY(SOME)或ALL谓词的子查询
3.58 查询非计算机系中比计算机科学系任意一个学生年龄小的学生姓名和年龄
select sname,sage from student where sage<any(select sage from student where sdept='cs')and sdept<>'cs';
3.59 查询非计算机科学系中比计算机科学系所有学生年龄都小的学生姓名及年龄
select sname,sage from student where sage<all(select sage from student where sdept='cs')and sdept<>'cs';
用聚合函数实现
select sname,sage from student where sage<(select min(sage) from student where sdept='cs')and sdept<>'cs';
模仿查询
查询非计算机科学系中比计算机科学系所有学生平均年龄都小的学生姓名及年龄
select sname,sage from student where sage<all(select avg(sage) from student where sdept='cs')and sdept<>'cs';
4.带有EXISTS谓语的子查询
3.60 查询所有选修了号课程的学生姓名
select sname from student where exists(select * from sc where sno=student.sno and cno='1');
3.61 查询没有选修号课程的学生姓名
select sname from student where not exists(select * from sc where sno=student.sno and cno='1');
查询与“李晨”在同一个系的学习的学生
select sname from student s1 where exists(select * from student s2 where s2.sdept = s1.sdept and s2.sname='李晨');
3.62 查询选修了全部课程的学生姓名
select sname from student where not exists(select * from course where not exists(select * from sc where sno=student.sno and cno = course.cno));
3.63 查询至少选修了学生选修的全部课程的学生号码
select distinct sno from sc scx where not exists(select * from sc scy where scy.sno='201215121' and not exists(select * from sc scz where scz.sno=scx.cno and scz.cno=scy.cno));
模仿查询
查询与“李晨”在同一个系的学习的学生且年龄大于的女性
select sname from student s1 where exists(select * from student s2 where s2.sdept = s1.sdept and s2.sname='李晨')and sage>18 and ssex='女';
三、集合查询
3.64 查询计算机科学系的学生及年龄不大于岁的学生
select * from student where sdept='cs' union select * from student where sage<=19;
3.65 查询选修了课程或者选修了课程的学生
select sno from sc where cno='1' union select sno from sc where cno='2';
3.66 查询计算机科学系的学生与年龄不大于岁的学生的交集
select * from student where sdept='cs' intersect select * from student where sage<=19;
3.67 查询即选修了课程又选修了课程的学生。就是查询选修课程的想也是集合与选修课程的学生集合的交集
select sno from sc where cno='1' intersect select sno from sc where cno='2';
可写成子查询
select sno from sc where cno='1' and sno in(select sno from sc where cno='2');
3.68 查询计算机科学系的学生与年龄不大于岁的学生的差集
select * from student where sdept='cs' except select * from student where sage<=19;
简单查询方式:
select * from student where sdept='cs' and sage>19
模仿语句
查询计算机科学系的学生与年龄不大于岁的学生与男学生的交集
select * from student where sdept='cs' intersect select * from student where sage<=20 intersect select * from student where ssex='男';
课堂问题
求至少选修c语言、数据库、数据结构的人
select * from student where snoin(select sno from sc where cno not in (select cno from course where cname notin('数学','数据库','数据结构')));