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%'
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 course.cno = score.cno;
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,score.cno,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)
每一门的最高分:
select * from score a where sno in(select sno from score group by sno having count(*)>1) and degree<(select max(degree) from score b where b.cno = a.cno)
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人的教师姓名。
select teacher.tname, course.cname,score.cno from teacher,course,score where teacher.tno=course.tno and course.cno=score.cno and score.cno= (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 teacher
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<(select avg(degree) from score b where b.cno =a.cno)
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.
select 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 course.cname='计算机导论' and student.sno=score.sno and course.cno=score.cno
做题经验总结:
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,score.cno,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 course.cname='计算机导论' and student.sno=score.sno and course.cno=score.cno
这道题涉及到了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(select max(degree) from score) and后面加上score.sno in再写条件,否则没法加限制条件。
22题:year(sbirthday) 生日取年,例如2016-07-12,取出来的是2016。
28题:!=表示不等于
38题:year(curdate())意思是当前年
MySQL查询 45道练习题的更多相关文章
- MySQL语句45道练习题及答案
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...
- MYSQL数据库45道练习题
--第一题查询Student表中的所有记录的Sname.Ssex和Class列.select Sname,Ssex,Class from Student;--第二题查询教师所有的单位即不重复的Depa ...
- 学生选课数据库SQL语句45道练习题整理及mysql常用函数(20161019)
学生选课数据库SQL语句45道练习题: 一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四 ...
- MYSQL 45道练习题
学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表(一)~表(四)所示.用S ...
- SQL 查找 45道练习题
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...
- mysql 查询 45 道题
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...
- 补充:MySQL经典45道题型
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher). 四个表的结构分别如表1-1的表(一)~表 ...
- MySQL 查询练习记录
MySQL 查询练习记录 最近在复习mysql,在b站上找了一个感觉还不错的视频,把视频中查询练习相关的内容记录了下来,以便自己日后查阅和复习. 视频连接:https://www.bilibili.c ...
- MySQL基础普及《MySQL管理之道:性能调优、高可用与监控》
最近工作的内容涉及MySQL运维内容,陆陆续续读了几本相关的书,其中一本是<MySQL管理之道:性能调优.高可用与监控>. 内容涵盖性能调优(包括sql优化等).备份.高可用,以及读写分离 ...
随机推荐
- javaIO--字节流
流---是指的一组有序的.有气垫和重点的字节集合,是对的护具传输的总称或者抽象. 流采用缓冲区技术,当写一个数据时,系统将数据发送到缓冲区而不是外部设备(如硬盘),当读一个数据时,系统实际是从缓冲区读 ...
- (五)final修饰符
final修饰变量 final修饰符一般用于基本数据类型(int,float)或者不可变对象(String).这时候可以看作常量变量. 但是当final作用于可变数据类型时(数组,一般对象),仅仅表示 ...
- iOS开发解决 jsonModel 属性跟系统的重复
-(id)initWithDic:(NSDictionary *)dic { if (self = [super init]) { [self setValuesForKeysWithDictiona ...
- python中装饰器的原理以及实现,
python版本 3.6 1.python的装饰器说白了就是闭包函数的一种应用场景,在运用的时候我们遵循 #开放封闭原则:对修改封闭,对拓展开放 2.什么是装饰器 #装饰他人的器具,本身可以是任意可调 ...
- 在 Range 对象中,Min (14)必须小于或等于 max (-1)。
DataTable dt = ds.Tables[]; DataRow[] drs = dt.Select("Id=" + categoryID ); 解决方法:将参数用单引号阔起 ...
- spring cloud 之 客户端负载均衡 Ribbon
一.负载均衡 负载均衡(Load Balance): 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽.增加吞吐量.加强网络数据处理能力.提高网络的灵活性和可用性.其意 ...
- mysql(一) 关联查询的方式
mysql做关联查询时,一般使用join....on.....的语法. 但还有其它两种语法形式,三者的主要区别在于书写形式,其余方面并无太多差异. 如下三种形式: select * from trad ...
- 【bzoj1010】[HNOI2008]玩具装箱toy 斜率优化dp
题目描述 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1...N的N件玩具, ...
- javascript中面向对象的5种写法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- BZOJ4240 有趣的家庭菜园(贪心+树状数组)
显然相当于使序列变成单峰.给原序列每个数按位置标号,则要求重排后的序列原标号的逆序对数最少.考虑将数从大到小放进新序列,那么贪心的考虑放在左边还是右边即可,因为更小的数一定会在其两侧,与它自身放在哪无 ...