mysql经典45道题_MySQL查询45道练习题
SQL查询45道练习题
1.查询Student表中的所有记录的Sname、Ssex和Class列。
select sname,ssex,class from student
2.查询教师所有的单位即不重复的Depart列。
select distinct depart from teacher
3.查询Student表的所有记录。
select * from student
4.查询Score表中成绩在60到80之间的所有记录。
select degree from score where Degree between 60 and 80
或者
select degree from score where Degree>60 and degree<80
5.查询Score表中成绩为85,86或88的记录。
select degree from score where degree=85 or degree=86 or degree=88
或者
select degree from score where degree in (85,86,88)
6.查询Student表中“95031”班或性别为“⼥”的同学记录。
select * from student where class="95031" or ssex='⼥'
7.以Class降序查询Student表的所有记录。
select * from student order by class desc
8.以Cno升序、Degree降序查询Score表的所有记录。
select * from score order by cno asc,degree desc
9. 查询“95031”班的学⽣⼈数。
select count(*) from student where class="95031"
10.查询Score表中的最⾼分的学⽣学号和课程号。(⼦查询或者排序)
排序查询:select * from score order by degree desc limit 0,1
⼦查询:select sno,cno from score where degree=(select max(degree) from score)
11.查询每门课的平均成绩。
select cno,avg(degree) from score group by cno
12.查询Score表中⾄少有5名学⽣选修的并以3开头的课程的平均分数。
select avg(degree) from score where cno=( select cno from score group by cno having count(cno)>4 and cno like '3%') select avg(degree) from score where cno like '3%' and cno in(select cno from score group by cno having count(cno)>4) select avg(degree) from score group by cno having count(cno)>4 and cno like '3%'
php编程基础与实例教程 pdf
13.查询分数⼤于70,⼩于90的Sno列。
正则匹配一张图片select sno from score where degree>70 and degree<90;
14.查询所有学⽣的Sname、Cno和Degree列。
select student.Sname,score.Cno,score.Degree from student,score where student.Sno = score.Sno;
15.查询所有学⽣的Sno、Cname和Degree列。
select course.Cname,score.sno,score.Degree from course,score where courseo = scoreo;
16.查询所有学⽣的Sname、Cname和Degree列。
select student.Sname,course.Cname,score.Degree from student,course,score where score.Cno=course.Cno and
score.Sno=student.sno
17.查询“95033”班学⽣的平均分。
select avg(degree) from score where sno in (select sno from student where class="95033");
18.假设使⽤如下命令建⽴了⼀个grade表:
create table grade(low int(3),upp int(3),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 score.Cno,score.Sno,grade.rank from score,grade where score.Degree < grade.upp and score.Degree>grade.low;
没有重名的列可以不加表名,下⾯的写法是不加表名的。
select sno,cno,rank from score,grade where degree between low and upp
19.查询选修“3-105”课程的成绩⾼于“109”号同学成绩的所有同学的记录。
select * from student where sno in(select sno from score where degree > (select degree from score where sno=109 and cno='3-105') and cno='3-105');
或者
select * from score shere cno ='3-105' and degree >(select max(degree) from score where sno='109')
或者
select * from score shere cno ='3-105' and degree >(select max(degree) from score where sno='109' and cno='3-105')
20.查询score中选学多门课程的同学中分数为⾮最⾼分成绩的记录。
全部成绩的最⾼分:
select student.sname,score.sno,scoreo,score.degree from student,score where student.sno=scor
e.sno and score.sno in(select sno from score group by sno having count(cno)>1) and score.degree not in(select max(degree) from score)
每⼀门的最⾼分:
select * from score a where sno in(select sno from score group by sno having count(*)>1) and degree
21.查询成绩⾼于学号为“109”、课程号为“3-105”的成绩的所有记录。
select * from student where sno in(select sno from score where degree > (select degree from score where sno=109 and cno='3-105') and cno='3-105');
22.查询和学号为108的同学同年出⽣的所有学⽣的Sno、Sname和Sbirthday列。
select sno,sname,sbirthday from student where year(sbirthday)= (select year(sbirthday) from student where sno=108);
23.查询“张旭“教师任课的学⽣成绩。
select degree from score where cno= (select cno from course where tno= (select tno from teacher where tname='张旭'));
24.查询选修某课程的同学⼈数多于5⼈的教师姓名。
ame, courseame,scoreo from teacher,course,score = and
courseo=scoreo and scoreo= (select cno from score group by cno having count(cno)>5)
25.查询95033班和95031班全体学⽣的记录。
红字体颜代码select distinct student.*,score.* from student,score where student.sno=score.sno
26.查询存在有85分以上成绩的课程Cno.
select distinct cno from score where degree>85
27.查询出“计算机系“教师所教课程的成绩表。
select * from score where cno in(select cno from course where tno in(select tno from teacher where
depart='计算机系'));
28.查询“计算机系”与“电⼦⼯程系“不同职称的教师的Tname和Prof
理解⼀:计算机系的教室和电⼦⼯程系的不同的。
select depart,tname,prof from teacher where prof not in(select prof from teacher where depart='电⼦⼯程系')
理解⼆:两系互不相同的。
select tname,prof from teacher where depart='计算机系' and prof not in(select prof from teacher where depart='电⼦⼯程系')
相关⼦查询:
select Tname,Prof from Teacher a where Prof not in(select Prof from Teacher b where a.Depart!=b.Depart)
29.查询选修编号为“3-105“课程且成绩⾄少⾼于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从⾼到低次序排序。
select cno,sno,degree from score a where cno='3-105' and degree>any(select degree from score b where cno='3-245' and b.sno=a.sno) order by degree desc
any 代表括号内的任意⼀个
30.查询选修编号为“3-105”且成绩⾼于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
select cno,sno,degree from score a where cno='3-105' and degree>all(select degree from score b where cno='3-245' and b.sno=a.sno) order by degree desc
all代表所有的
31.查询所有教师和同学的name、sex和birthday.
select teacher.Tname as name,teacher.Tsex as sex,teacher.Tbirthday as birthday from teachervlookup视频教程
union
select student.Sname as name,student.Ssex as sex,student.Sbirthday as birthday from student
32.查询所有“⼥”教师和“⼥”同学的name、sex和birthday.
select teacher.Tname as name,teacher.Tsex as sex,teacher.Tbirthday as birthday from teacher where tsex='⼥'
union
select student.Sname as name,student.Ssex as sex,student.Sbirthday as birthday from student where ssex='⼥'
33.查询成绩⽐该课程平均成绩低的同学的成绩表。
select * from score a where degree
34.查询所有任课教师的Tname和Depart.
select tname,depart from teacher where tno in(select tno from course where cno in(select cno from score))
35.查询所有未讲课的教师的Tname和Depart.
select tname,depart from teacher where tno not in (select tno from course where cno in (select distinct cno from score))
36.查询⾄少有2名男⽣的班号。
select distinct class from student a where (select count(ssex) from student b where ssex ='男' and a.class=b.class )>1
37.查询Student表中不姓“王”的同学记录。
select * from student where sname not like '王%'
38.查询Student表中每个学⽣的姓名和年龄。
select student.sname,(2016-year(sbirthday)) as sage from student where true
⽹上查询的year(curdate())意思是当前年
select student.sname,(year(curdate())-year(sbirthday)) as sage from student where true
39.查询Student表中最⼤和最⼩的Sbirthday⽇期值。
select min(sbirthday),max(sbirthday) from student
40.以班号和年龄从⼤到⼩的顺序查询Student表中的全部记录。
select * from student order by class desc,sbirthday asc
41.查询“男”教师及其所上的课程。
select teacher.Tname,course.Cname from teacher,course where course.Tno=teacher.Tno and tsex='男'
42.查询最⾼分同学的Sno、Cno和Degree列。
select sno,cno,degree from score where degree=(select max(degree) from score)
43.查询和“李军”同性别的所有同学的Sname.
select sname from student where ssex=(select ssex from student where sname='李军')
44.查询和“李军”同性别并同班的同学Sname.
interface not supportedselect sname from student a where ssex=(select ssex from student b where sname='李军' and a.class=b.class)
45.查询所有选修“计算机导论”课程的“男”同学的成绩表。
select student.sno,student.sname,score.degree from student,course,score where student.ssex='男' and courseame='计算机导论' and student.sno=score.sno and courseo=scoreo
做题经验总结:
1.做题过程中感觉⽐较难的是12、20、24、45题。12、20、24题能想出逻辑,后⾯的条件的写法⽐较不好写,试过很多遍才试写出来。45题直接想不出来写法。
2.对12题的理解
查询Score表中⾄少有5名学⽣选修的并以3开头的课程的平均分数。
select avg(degree) from score where cno=( select cno from score group by cno having count(cno)>5 and cno like '3%')
先查出⾄少有5名学⽣选修的并以3开头的课程,⽤到的是分组查询select 列 from 表 group by 列 having 条件,后⾯有2个条件,⽤and 连接。
3.对20题的理解(20题和24题解法相近)
查询score中选学多门课程的同学中分数为⾮最⾼分成绩的记录。
select student.sname,score.sno,scoreo,score.degree from student,score where student.sno=score.sno and score.sno
in(select sno from score group by sno having count(cno)>1) and score.degree not in(select max(degree) from score)
涉及到多个表时,可以采取这种写法。先写出想查询的列,然后写关系。注意关系的写法,后⾯2个关系为选修课程⼤于1门的和分数不属于最⾼分的,and后⾯不是直接写关系,⽽是先写需要限制关系的列,括号⾥写限制的条件。关系的写法可以认为是固定写法。
4.对45题的理解
查询所有选修“计算机导论”课程的“男”同学的成绩表。
select student.sno,student.sname,score.degree from student,course,score where student.ssex='男' and courseame='计算机导论' and student.sno=score.sno and courseo=scoreo
这道题涉及到了3张表,其关系是student表和course表都和score表有关系,但是彼此之间没有联系。最初的想法是先写出score表的degree列,再去限制关系,但是2个没有关系的表的限制关系⽆法写在⼀个表中。正确的查询⽅法是先写出这3个表的存在关系的列,让把3张表有了联系,再去写3张表的
关系。
查询最主要的是两个⽅⾯,⼀个是要查询的列与表或者表与表的关系,个⼈感觉稍微复杂点的(2个表或者2个以上的表)就可以⽤45题的解法,;⼆是写条件,条件的写法注意以下题中的写法:
6题:class=95031 or ssex='⼥' 字符串要加‘’。
10题:limit0,1跳过0条数据取1条,意思就是取第⼀条数据。
12题:where cno=( select cno from score group by cno having count(cno)>5 and cno like '3%')分组查询后⼜加⼀个条件,⽤and连接。
18题:where score.Degree < grade.upp and score.Degree>grade.low 分数可以直接⽐较
20题:and score.sno in(select sno from score group by sno having count(cno)>1) and score.degree not in(selectmysql面试题34道经典
max(degree) from score) and后⾯加上score.sno in再写条件,否则没法加限制条件。
22题:year(sbirthday) ⽣⽇取年,例如2016-07-12,取出来的是2016。
28题:!=表⽰不等于
38题:year(curdate())意思是当前年
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论