mysql查询题集

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 查询练习(50题) MySQL 查询练习(50题) I 1 -- 建表 1 1.1 -- 学生表 1 1.2 -- 课程表 1 1.3 -- 教师表 1 1.4 -- 成绩表 2 1.5 -- 插入学生表测试数据 2 1.6 -- 课程表测试数据 2 1.7 -- 教师表测试数据 2 1.8 -- 成绩表测试数据 2 2 -- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数 3 3 -- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数 4 4 -- 3、查询平均成绩大于等于85分的同学的学生编号和学生姓名和平均成绩 4 5 -- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩-- (包括有成绩的和无成绩的) 4 6 -- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩 5 7 -- 6、查询"李"姓老师的数量 5 8 -- 7、查询学过"张三"老师授课的同学的信息 5 9 -- 8、查询没学过"张三"老师授课的同学的信息 5 10 -- 9、查询学过编号为"01"并且也学过编号为"02"的课程同学的信息 6 11 -- 10、查询学过编号为"01"但是没有学过编号为"02"的课程同学的信息 6 12 -- 11、查询没有学全所有课程同学的信息 6 13 -- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息 6 14 -- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息 7 15 -- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名 7 16 -- 15、查询两门及其以上不及格课程同学的学号,姓名及其平均成绩 7 17 -- 16、检索"01"课程分数小于60,按分数降序排列的学生信息 7 18 -- 17.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 8 19 -- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 8 20 -- 19、按各科成绩进行排序,并显示排名(实现不完全) 9 21 -- 20、查询学生的总成绩并进行排名 10 22 -- 21、查询不同老师所教不同课程平均分从高到低显示 10 23 -- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩 10 24 -- 23、统计各科成绩各分数段人数课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比 11 25 -- 24、查询学生平均成绩及其名次 11 26 -- 25、查询各科成绩前三名的记录 12 27 -- 26、查询每门课程选修的学生数 12 28 -- 27、查询出只有两门课程的全部学生的学号和姓名 12 29 -- 28、查询男生、女生人数 12 30 -- 29、查询名字中含有"风"字的学生信息 12 31 -- 30、查询同名同性学生名单,并统计同名人数 12 32 -- 31、查询1990年出生的学生名单 13 33 -- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 13 34 -- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩 13 35 -- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数 13 36 -- 35、查询所有学生的课程及分数情况; 13 37 -- 36、查询任何一门课程成绩在70分以上的姓名课程名称和分数; 14 38 -- 37、查询不及格的课程 14 39 --38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名; 14 40 -- 39、求每门课程的学生人数 14 41 -- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩 14 42 -- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 15 43 -- 42、查询每门功成绩最好的前两名 15 44 -- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数查询结果按人数降序排列, 15 45 -- 44、检索至少选修两门课程的学生学号 15 46 -- 45、查询选修了全部课程的学生信息 15 47 -- 46、查询各学生的年龄 16 48 -- 47、查询本周过生日的学生 16 49 -- 48、查询下周过生日的学生 16 50 -- 49、查询本月过生日的学生 16 51 -- 50、查询下月过生日的学生 16
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值