CREATE TABLE students
(sno VARCHAR(3) NOT NULL,
sname VARCHAR(10) NOT NULL,
ssex VARCHAR(5) NOT NULL,
sbirthday DATETIME,
class VARCHAR(5));
CREATE TABLE courses
(cno VARCHAR(5) NOT NULL,
cname VARCHAR(20) NOT NULL,
tno VARCHAR(10) NOT NULL);
CREATE TABLE scores
(sno VARCHAR(3) NOT NULL,
cno VARCHAR(5) NOT NULL,
degree int NOT NULL) ;
CREATE TABLE teachers
(tno VARCHAR(3) NOT NULL,
tname VARCHAR(10) NOT NULL, tsex VARCHAR(5) NOT NULL,
tbirthday DATETIME NOT NULL, prof VARCHAR(20),
depart VARCHAR(10) NOT NULL);
INSERT INTO students(SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,'Flank' ,'boy' ,'1977-09-01',95033);
INSERT INTO students(SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,'Maker' ,'boy' ,'1975-10-02',95031);
INSERT INTO students(SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,'Alice' ,'girl' ,'1976-01-23',95033);
INSERT INTO students(SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,'Bob' ,'boy' ,'1976-02-20',95033);
INSERT INTO students(SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,'Lose' ,'girl' ,'1975-02-10',95031);
INSERT INTO students(SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,'Jacker' ,'boy' ,'1974-06-03',95031);
INSERT INTO courses(CNO,CNAME,TNO)VALUES ('3-105' ,'computer intorduction',825);
INSERT INTO courses(CNO,CNAME,TNO)VALUES ('3-245' ,'operation system' ,804);
INSERT INTO courses(CNO,CNAME,TNO)VALUES ('6-166' ,'data circuit' ,856);
INSERT INTO courses(CNO,CNAME,TNO)VALUES ('9-888' ,'advanced math' ,100);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (103,'3-245',86);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (105,'3-245',75);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (109,'3-245',68);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (103,'3-105',92);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (105,'3-105',88);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (109,'3-105',76);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (101,'3-105',64);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (107,'3-105',91);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (108,'3-105',78);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (101,'6-166',85);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (107,'6-106',79);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (108,'6-166',81);
INSERT INTO teachers (TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (804,'licheng','boy','1958-12-02','associate professor','computer');
INSERT INTO teachers (TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (856,'zhangxu','boy','1969-03-12','lecturer','electronic');
INSERT INTO teachers (TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (825,'wangpin','girl','1972-05-05','lecturer','computer');
INSERT INTO teachers (TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (831,'liubing','girl','1977-08-14','teaching assistant','electronic');```
// 自然连接默认连接两个表中的相同字段 假设相同字段是 sno 相当于 A join B on A.sno=B.sno
// 没有什么单表查询是自连接解决不了的,只是效率相对较低
1、 查询Student表中的所有记录的Sname、Ssex和Class列。
select sname,ssex,class from students;
2、 查询教师所有的单位即不重复的Depart列。
select depart from teachers group by depart;
3、 查询Student表的所有记录。
select * from students;
4、 查询Score表中成绩在60到80之间的所有记录。
select * from scores where degree between 60 and 80;
5、 查询Score表中成绩为85,86或88的记录。
select * from scores where degree in (85,86,88);
6、 查询Student表中“95031”班或性别为“女”的同学记录。
select * from students where class="95031" or ssex="girl";
7、 以Class降序查询Student表的所有记录。
select * from students order by class desc;
8、 以Cno升序、Degree降序查询Score表的所有记录。
select * from scores order by cno asc ,degree desc;
9、 查询“95031”班的学生人数。
select count(*) from students where class="95031";
10、查询Score表中的最高分的学生学号和课程号。
可以先排序 为desc,然后指定limit 为0, 1
select sno,cno from scores where degree=(select max(degree) from scores);
11、查询‘3-105’号课程的平均分。
select avg(degree) from scores where cno="3-105";
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
rlike "[3]\w*" 也行
select avg(degree) from scores where cno like "3%" group by cno having count(*)>5;
13、查询最低分大于70,最高分小于90的Sno列。
select sno from score group by sno having min(degree)>70 and max(degree)<90;
14、查询所有学生的Sname、Cno和Degree列。
select sname ,Sc.cno,Sc.degree from scores Sc natural join students;
15、查询所有学生的Sno、Cname和Degree列。
select S.sname,C.cname,Sc.Degree from students natural join scores natural join courses
16、查询所有学生的Sname、Cname和Degree列。
select sname ,C.cname,Sc.degree from scores Sc natural join students S natural join courses C;
17、查询“95033”班所选课程的平均分。
select avg(degree) from scores Sc natural join students S where S.class="95033";
18、假设使用如下命令建立了一个grade表:
create table grade(low int,upp int,rank char(1));
insert into grade values(90,100,'A');
insert into grade values(80,89,'B');
insert into grade values(70,79,'C');
insert into grade values(60,69,'D');
insert into grade values(0,59,'E');
现查询所有同学的Sno、Cno和rank列。
select S.sno,Sc.cno,rank from students S join scores Sc on S.sno = Sc.sno join grade where degree between low and upp;
19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
select * from students S natural join scores Sc where Sc.cno = "3-105" and Sc.degree >(select degree from scores where sno="109" and cno="3-105");
20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
分析:本题要将scores按照学号分组,求出每组的degree最大值然后每组degree数据都和最大值比较,不等于最大值, 同时还要满足 学号分组里面每小组的数据量要大于1 即输出
select * from scores where sno in( select sno from scores group by sno having count(*)>1) and degree not in(select max(degree) from scores group by sno);
21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
子查询里面如果只有一条数据可以直接使用 > ,< ,=,!=,如果有多条数据就要考虑使用in ,not ni, >any,>all
select * from scores where degree > (select degree from scores where sno="109"and cno="3-105");
22、查询“张旭“教师任课的学生成绩。
select * from scores where cno in (select cno from courses where tno in(select tno from teachers where tname = "zhangxu"));
select degree from scores natural join courses natural join teachers where tname="zhangxu";
23、查询选修某课程的同学人数多于5人的教师姓名。
select tname from teachers where tno in (select tno from courses where cno in (select cno from scores group by cno having count(*)>=5));
select tname from teachers natural join courses where cno in (select cno from scores group by cno having count(*)>5);
24、查询95033班和95031班全体学生的记录。
select * from students where class in ("95033","95031");
25、查询存在有85分以上成绩的课程Cno.
select distinct cno from scores where cno in (select cno from scores group by cno having max(degree)>85);
26、查询出“计算机系“教师所教课程的成绩表。
select * from scores where cno in (select cno from courses where tno in (select tno from teachers where depart="computer"));
27、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
//使用自连接将teachers表分出两组, 一组是computer 一组是electronic 然后获取到两组中的相同职称 ,作为筛选条件 只要不是相同职称即可
select tname,prof from teachers where prof not in (select A.prof from teachers A join teachers B on A.prof=B.prof and A.depart ="computer"and B.depart="electronic");
28、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
select * from scores where cno="3-105" and sno in (select A.sno from scores A join scores B on A.sno = B.sno where A.cno ="3-105" and B.cno="3-245" and A.degree>B.degree) order by degree desc;
29、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
30、查询所有教师和同学的name、sex和birthday.
select sname,ssex,sbirthday from students union select tname,tsex,tbirthday from teachers;
31、查询所有“女”教师和“女”同学的name、sex和birthday.
select sname,ssex,sbirthday from students where ssex="girl" union select tname,tsex,tbirthday from teachers where tsex="girl";
32、查询成绩比该课程平均成绩低的同学的成绩表。
select * from scores S1 join(select cno ,avg(degree) degree1 from scores group by cno)S2 on S1.cno=S2.cno where S1.degree<S2.degree1;
33、查询所有任课教师的Tname和Depart.
select tname,depart from teachers natural join courses ;
34 查询所有未讲课的教师的Tname和Depart.
select tname,depart from teachers where tno not in (select tno from courses);
35、查询至少有2名男生的班号。
select class from students where ssex ="boy" group by class having count(*)>=2;
两次分组 一次班号一次学生 可以使用自连接查询
select class from students where ssex="boy" group by class having count(*)>=2;
select distinct A.class from students A join students B on A.class=B.class where A.ssex="boy" and B.ssex="boy";
36、查询Student表中不姓名不已“F”开头的同学记录。 查询不是以F开头的
select * from students where sname not rlike "[F]\w*";
37、查询Student表中最大和最小的Sbirthday日期值。
select max(sbirthday),min(sbirthday) from students;
38、以班号和年龄从大到小的顺序查询Student表中的全部记录。
select * from students order by class desc,sbirthday asc;
39、查询“男”教师及其所上的课程。
select cname from courses C join teachers T on C.tno=T.tno where T.tsex ="boy";
40、查询最高分同学的Sno、Cno和Degree列。
select * from scores having degree=any(select max(degree) from scores ) ;
41、查询和“Flank”同性别的所有同学的Sname.
select B.sname from students A join students B on A.ssex=B.ssex where A.sname="Flank";
42、查询和“Flank”同性别并同班的同学Sname.
select B.sname from students A join students B on A.ssex=B.ssex where A.sname="Flank" and A.class=B.class ;
43、查询所有选修“计算机导论”课程的“男”同学的成绩表 连接三个表,后写判断条件即可
select Sc.* from scores Sc join courses C on Sc.cno = C.cno join students S on S.sno = Sc.sno where S.ssex="boy" and C.cname="computer intorduction";
mysql查询题集
最新推荐文章于 2024-07-20 22:17:47 发布