1数据准备

#创建数据库school
drop database if exists school;
create database if not exists school;
#设置当前数据库
use school;
#学生表
create table if not exists student(
sno varchar(3) primary key comment'学生编号',
sname varchar(4) not null comment'姓名',
ssex varchar(2) not null comment'性别',
sbirthday date comment'生日',
class varchar(5) not null comment'班级代号'
);
#教师表
create table if not exists teacher(
tno varchar(3) primary key comment'教师编号',
tname varchar(4) not null comment'姓名',
tsex varchar(2) not null comment'性别',
tbirthday date not null comment'生日',
prof varchar(6) comment'职称',
depart varchar (30) comment'部门'
);
#课程表
create table if not exists course(
cno varchar(5) primary key comment'课程编号',
cname varchar(10) not null comment'课程名称',
tno varchar(10) not null comment'老师编号'
);
#成绩表
create table if not exists score(
sno varchar(3) not null comment'学生编号',
cno varchar(5) not null comment'课程编号',
degree double(10,1) not null comment'成绩',
primary key(sno,cno)
);
#创建外键
/*course*/
alter table course add constraint 
foreign key (tno) references teacher(tno);
/*score*/
alter table score add constraint 
foreign key (sno) references student(sno);
alter table score add constraint 
foreign key (cno) references course(cno);
#插入数据
#student
insert into student(sno,sname,ssex,sbirthday,class) values
(108,'曾华','男','1977-09-01',95033),
(105,'匡明','男','1975-10-02',95031),
(107,'王丽','女','1976-01-23',95033),
(101,'李军','男','1976-02-20',95033),
(109,'王芳','女','1975-02-10',95031),
(103,'陆军','男','1974-06-03',95031);

#teacher
insert into teacher(tno,tname,tsex,tbirthday,prof,depart) values
(804,'李诚','男','1958-12-02','副教授','计算机系'),
(856,'张旭','男','1975-03-12','讲师','电子工程系'),
(825,'王萍','女','1976-05-05','助教','计算机系'),
(831,'刘冰','女','1976-08-14','助教','电子工程系');

#course
insert into course(cno,cname,tno) values
('3-105','计算机导论',825),
('3-245','操作系统',804),
('6-166','数据电路',856),
('9-888','高等数学',831);

#score
insert into score(sno,cno,degree) values
(103,'3-245',86),
(105,'3-245',75),
(109,'3-245',68),
(103,'3-105',92),
(105,'3-105',88),
(109,'3-105',76),
(101,'3-105',64),
(107,'3-105',91),
(108,'3-105',78),
(101,'6-166',85),
(107,'6-166',79),
(108,'6-166',81);

2.练习题目及答案

#子查询,由内到外
#相关子查询类似连接自己产生的临时表再查询
# 完成以下题目
use school;
# 1、 查询student表的所有记录。
select * from student;
# 2、 查询student表中的所有记录的sname、ssex和class列。
select sname,ssex,class from student;
# 3、 查询教师所有的部门即不重复的depart列。
select distinct depart from teacher;
# 4、 查询score表中成绩在60到80之间的所有记录。>=60 and <=80
select * from score where degree between 60 and 80;
# 5、 查询score表中成绩为85,86或88的记录。or,or
select * from score where degree in(85,86,88);
# 6、 查询student表中“95031”班或性别为“女”的同学记录。in
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,degree desc;
# 9、 查询“95031”班的学生人数。
select count(class) from student where class='95031';
# 10、查询score表中的最高分的学生学号和课程号。排序取1不科学
select sno,cno from score where degree=(
select max(degree) from score);
# 11、查询‘3-105’号课程的平均分。
select avg(degree) from score where cno='3-105';
# 12、查询score表中-至少有5名-学生选修的并以3开头的课程的平均分数。
select cno,avg(degree) from score where  cno in
(
select cno from score group by cno having count(cno)>=5 and cno like '3%'
);
# 13、查询最低分大于70,最高分小于90的sno列。-------------------------------------------------------------------------------------------------
select sno,max(degree),min(degree) from score group by sno
having max(degree)<90 and min(degree)>70;
# 14、查询所有学生的sname、cno和degree列。
select sname,cno,degree from student
join score
on student.sno=score.sno;
# 15、查询所有学生的sno、cname和degree列。
select sno,degree,cname from score
join course
on score.cno=course.cno
;
# 16、查询所有学生的sname、cname和degree列。
select sname,degree,cname from student
join score
on student.sno=score.sno
join course
on score.cno=course.cno
;
# 17、查询“95033”班所选课程的平均分。
select avg(degree) from student 
join score
on student.sno=score.sno
where class='95033';
/*18、假设使用如下命令建立了一个grade表:
create table grade(
low numeric(3,0),
upp numeric(3,0),
ranker 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');
commit;
现查询所有同学的sno、cno和ranker列。*/------------------------------------------------------------------------------------------------
select sno,cno,ranker from score,grade 
select sno,cno,ranker from score,grade 
select sno,cno,degree,
(select ranker from grade where degree>=low and degree<=upp) as ranker#不可行,如89.5
 from score;

select sno,cno,degree,
case
    when degree>=90 then 'a'
    when degree>=80 then 'b'
    when degree>=70 then 'c'
    when degree>=60 then 'd'
    else 'e'
end as ranker
from score;
# 19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
select * from score where cno='3-105' and degree>(
select max(degree) from score where sno='109' and cno='3-105'#cno='3-105'可无
);
# 20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。----相关子查询
select sno,min(degree) from score group by sno having count(cno)>1;#xxxxx
select sno,degree from score order by sno,degree#参照,非答案

select * from score s
where degree<>
(select max(degree) from score
where sno=s.sno)
order by sno,cno;

#表连接------
select score.* from score join
(select sno,max(degree) as maxdegree from score group by sno) t
on score.sno=t.sno
where degree<>maxdegree
order by sno,cno;

# 21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select * from score where degree>(
select max(degree) from score where cno='3-105' and sno='109'
);#可不max,但是保险
# 22、查询和学号为108的同学同年出生的所有学生的sno、sname和sbirthday列。
select sno,sname,sbirthday from student where year(sbirthday)=(
select year(sbirthday) from student where sno='108'
);#and sno!='108'可剔除本人
# 23、查询“张旭“教师任课的学生成绩。
select sname,student.sno,degree from student 
join score
on student.sno=score.sno
join course
on score.cno=course.cno
join teacher
on course.tno=teacher.tno
where tname='张旭';

#子查询
select * from score where cno in
(select cno from course where tno in
(select tno from teacher where tname='张旭')
);
# 24、查询选修某课程的同学人数多于5人的教师姓名。
select tname from student 
join score
on student.sno=score.sno
join course
on score.cno=course.cno
join teacher
on course.tno=teacher.tno
#group by score.cno having count(score.cno)>5;
group by tname having count(tname)>5;
# 25、查询95033班和95031班全体学生的记录。
select * from student 
join score
on student.sno=score.sno
join course
on score.cno=course.cno
join teacher
on course.tno=teacher.tno
where class in('95033','950031');
# 26、查询存在有85分以上成绩的课程cno.
select distinct score.cno from score
join course
on score.cno=course.cno
where degree>85;

select distinct cno from score
where degree>85;
# 27、查询出“计算机系“教师所教课程的成绩表。
select sname,student.sno,degree,tname from student 
join score
on student.sno=score.sno
join course
on score.cno=course.cno
join teacher
on course.tno=teacher.tno
where depart='计算机系';

select * from score where cno in
(
select cno from course where tno in(
select tno from teacher where depart='计算机系')
);

# 28、查询“计算机系”与“电子工程系“不同职称的教师的tname和prof。
select tname,prof from teacher where depart in('计算机系','电子工程系');#错误理解
select tname,prof from teacher where depart='计算机系' and prof not in
(
select prof from teacher where depart='电子工程系'
)
union
select tname,prof from teacher where depart='电子工程系' and prof not in
(
select prof from teacher where depart='计算机系'
);
# 29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的成绩
# 同学的cno、sno和degree,并按degree从高到低次序排序。
select * from score where cno='3-105' and degree>(
select min(degree) from score where cno='3-245'
) order by degree desc
;
# 30、查询选修编号为“3-105”且成绩高于所有选修编号为“3-245”课程成绩
# 的同学的cno、sno和degree.
select cno,sno,degree from score where cno='3-105' and degree>(
select max(degree) from score where cno='3-245'
) order by degree desc
;
# 31、查询所有教师和同学的name、sex和birthday.
select sname 'name',ssex 'sex',sbirthday 'birthday' from student
union
select tname,tsex,tbirthday from teacher;
# 32、查询所有“女”教师和“女”同学的name、sex和birthday.
select sname 'name',ssex 'sex',sbirthday 'birthday' from student where ssex='女'
union
select tname,tsex,tbirthday from teacher where tsex='女';
# 33、查询成绩比该课程平均成绩低的同学的成绩表。-----------------------------------------------------------------------连接临时表
select sname,degree,cname from  student 
join score
on student.sno=score.sno
join course
on score.cno=course.cno
where degree<(
select avg(degree) from score group by sno,cno
);#错的

select score.* from score
join(
select cno,avg(degree) as avgdegree from score
group by cno) t
on score.cno=t.cno
where degree<avgdegree;

# 34、查询所有任课教师的tname和depart(所教课程有成绩的)--内连接可
select distinct tname,depart from score#错的
join course
on score.cno=course.cno
join teacher
on course.tno=teacher.tno

select tname,depart from teacher
where tno in
(
select tno from course join score
on course.cno=score.cno
)

# 35  查询所有未讲课的教师的tname和depart(所教课程没有成绩的)
select distinct tname,depart from score#
left join course
on score.cno=course.cno
left join teacher
on course.tno=teacher.tno
where degree is null;

select tname,depart from teacher
where tno not in
(
select tno from course join score
on course.cno=score.cno
)
# 36、查询至少有2名男生的班号。----记住count里面不为空意义和值都一样
select class from student where ssex='男'
group by class having count(*)>1;
# 37、查询student表中不姓“王”的同学记录。
select * from student where sname not like '王%';
# 38、查询student表中每个学生的姓名和年龄。
select sname,year(current_date)-year(sbirthday) from student;
# 39、查询student表中最大和最小的sbirthday的日期值。
select max(sbirthday),min(sbirthday) from student;
# 40、以班号和年龄从大到小的顺序查询student表中的全部记录。
select * from student;
select * from student order by class desc,sbirthday;
# 41、查询“男”教师及其所上的课程。
select tname,cname from course
join teacher
on course.tno=teacher.tno
where tsex='男';

select * from course where tno in
(select tno from teacher where 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='李军'
)
and sname<>'李军'
;
# 44、查询和“李军”同性别并同班的同学sname.
select sname from student where ssex=(
select ssex from student where sname='李军'
)
and
class=(
select class from student where sname='李军'
)
and sname<>'李军'
;
# 45、查询所有选修“计算机导论”课程的“男”同学的成绩表
select sname,ssex,degree,cname from student 
join score
on student.sno=score.sno
join course
on score.cno=course.cno
where course.cname='计算机导论' and ssex='男';

select * from score where cno=
(select cno from course where cname='计算机导论')
and sno in
(select sno from student where ssex='男')

3.易错

#连接
/*
student连score
score连course
course连teacher
*/
#内连接   外连接加left,或者right在join之前
from student 
join score
on student.sno=score.sno
join course
on score.cno=course.cno
join teacher
on course.tno=teacher.tno

# 13、查询最低分大于70,最高分小于90的sno列。分组查询
select sno,max(degree),min(degree) from score group by sno
having max(degree)<90 and min(degree)>70;
69
#现查询所有同学的sno、cno和ranker列。笛卡尔积,临时表
select sno,cno,ranker from score,grade 
select sno,cno,degree,
(select ranker from grade where degree>=low and degree<=upp) as ranker#不可行,如89.5
 from score;

select sno,cno,degree,
case
    when degree>=90 then 'a'
    when degree>=80 then 'b'
    when degree>=70 then 'c'
    when degree>=60 then 'd'
    else 'e'
end as ranker
from score;
142
# 31、查询所有教师和同学的name、sex和birthday.@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@小瑕疵
select sname,ssex,sbirthday from student
union
select tname,tsex,tbirthday from teacher;
# 32、查询所有“女”教师和“女”同学的name、sex和birthday.
select sname,ssex,sbirthday from student where ssex='女'
union
select tname,tsex,tbirthday from teacher where tsex='女';
# 33、查询成绩比该课程平均成绩低的同学的成绩表。-----------------------------------------------------------------------连接临时表
select score.*,t.* from score join
(
select cno,avg(degree) 'avg1' from score group by cno
) t
on score.cno=t.cno
where degree<avg1
;
# 34、查询所有任课教师的tname和depart(所教课程有成绩的)--内连接可
select distinct tname,depart from score#
join course
on score.cno=course.cno
join teacher
on course.tno=teacher.tno
# 35  查询所有未讲课的教师的tname和depart(所教课程没有成绩的)
select distinct tname,depart from score#
left join course
on score.cno=course.cno
left join teacher
on course.tno=teacher.tno
where degree is null;

mysql练习---查询语句练习相关推荐

  1. mysql 查询语句 参数,mysql参数化查询语句有关问题

    mysql参数化查询语句问题 部分代码如下: using (MySqlConnection conn = new MySqlConnection(connectionString)) { conn.O ...

  2. MySQL高级查询语句——超详细,一篇就够了

    MySQL高级查询语句 一.MySQL进阶查询 1.1.按关键字排序--order by 1.2.对结果进行分组--group by 1.2.1.单个分组 1.2.2.分组结合order by使用 1 ...

  3. mysql中如何分页查询_MySQL_mysql分页原理和高效率的mysql分页查询语句,以前我在mysql中分页都是用的 l - phpStudy...

    mysql分页原理和高效率的mysql分页查询语句 以前我在mysql中分页都是用的 limit 100000,20这样的方式,我相信你也是吧,但是要提高效率,让分页的代码效率更高一些,更快一些,那我 ...

  4. Mysql高级查询语句练习

    Mysql高级查询语句练习 DROP TABLE IF EXISTS `tblcourse`; CREATE TABLE `tblcourse` ( `CourseId` varchar(3) NOT ...

  5. MySQL 基本查询语句

    MySQL基本查询语句 表示例 id name price author stock img_path 1 java从入门到放弃 80.00 国哥 7 static/img/default.jpg 2 ...

  6. 为什么MySQL做查询语句时,第一次会很慢,但是第二次,第三次就会变快

    为什么MySQL做查询语句时,第一次会很慢,但是第二次,第三次就会变快 为什么MySQL的查询事务第一次执行会很慢,第二次,第三次就会快很多呢? 在国外,有个老外这么提问 Hi, I have an ...

  7. 【SQL】MySQL的查询语句

    文章目录 SELECT语句 WHERE子句 JOIN语句 GROUP BY和HAVING ORDER BY LIMIT 其他关键字 MySQL是一种广泛使用的关系型数据库管理系统,它被广泛地应用于各种 ...

  8. MySQL数据查询语句

    MySQL数据查询语句 MySQL 表单查询是指从一张表的数据中查询所需的数据,主要有查询所有字段.查询指定字段.查询指定记录.查询空值.多条件的查询.对查询结果进行排序分组等. 查询结构 SELEC ...

  9. mysql命令查询语句

    1.单表查询 select * from student; 采用*效率低,不推荐,多用列名 一.单表查询的语法:SELECT 字段1,字段2... FROM 表名WHERE 条件GROUP BY fi ...

  10. mysql 查询语句_SQL语言mysql基础查询语句

    单表查询.条件查询.查询并排序.限制结果查询.查询并排名.分组聚合查询.······ -- DQL操作,数据基本查询语言使用-------------------------------------- ...

最新文章

  1. 如何快速设计元器件原理图库和PCB封装库?
  2. 提气!清华成立集成电路学院,专研“卡脖子”技术
  3. 动态规划算法php,php算法学习之动态规划
  4. 原子操作类AtomicInteger详解
  5. 《守墓人》主程:如何用像素风做出真实的游戏世界
  6. Difference between RawValue and FormattedValue
  7. 关于FileSystemWatcher监听文件创建
  8. (13)FPGA面试题阻塞赋值与非阻塞赋值
  9. 鼠标点击TextBox控件后清空默认字体
  10. Pytorch——3.1. 热身:Pytorch基础
  11. 使用文本编辑器+命令行的方式实现Java中的第一个程序Hello World(下)
  12. 菜鸟教程java在线编辑器_HTML 编辑器
  13. 金蝶K3工程变更操作指南
  14. php图片显示不了,如何解决php显示不了图片的问题
  15. 多渠道归因分析(Attribution):python实现Shapley Value(四)
  16. EXCEL函数LookUp, VLOOKUP,HLOOKUP应用详解(含中文参数解释)
  17. C语言编写函数求字符串长度的几种实现方法
  18. android连iphone热点超时,苹果手机开热点安卓手机连不了为什么
  19. 西瓜书-机器学习5.3 误差逆传播算法
  20. 阿里数据中台维度建模规范、维度模型设计及模型实施方法论

热门文章

  1. 4步修改blender环境HDR设置
  2. win10安装CAJViewer 错误1305
  3. “第二届金融CIO班”开学典礼隆重举行
  4. ArcGIS API for Silverlight开发入门(4):用户与地理信息之间的桥梁--GraphicsLayer
  5. ubuntu下集显做显示nvidia独显做计算的一种解决方案
  6. 三条个税专项扣除标准提高1000元,多项个税优惠政策延用至2027年底
  7. 聊聊区块链--如何投资数字货币
  8. 2012 Autodesk开发者大会和AU归来
  9. 前端依赖管理那点事儿
  10. 【赞】深圳华强北的专业乞丐