Mysql学习笔记4(查询语句)

准备:创建以下表格

学生表Student
学号
姓名
性别
出生年月日
所在班级

create table student(
    sno varchar(20) primary key,
    sname varchar(20) not null,
    ssex varchar(10) not null,
    sbirthday datetime,
    class varchar(20)
);

教师表Teacher
教师编号
教师名字
教师性别
出生年月日
职称
所在部门

create table teacher(
    tno varchar(20) primary key,
    tname varchar(20) not null,
    tsex varchar(10) not null,
    tbirthday datetime,
    prof varchar(20) not null,
    depart varchar(20) not null
);

课程表Course
课程号
课程名称
教师编号

create table course(
    cno varchar(20) primary key,
    cname varchar(20) not null,
    tno varchar(20) not null,
    foreign key(tno) references teacher(tno)
);

成绩表Score
学号
课程号
成绩

create table score(
    sno varchar(20) not null,
    cno varchar(20) not null,
    degree decimal,
    foreign key(sno) references student(sno),
    foreign key(cno) references course(cno),
    primary key(sno, cno)
);

添加数据

# 添加学生信息
insert into student values('101', '曾华', '男', '1977-09-01', '95033');
insert into student values('102', '匡明', '男', '1975-10-02', '95031');
insert into student values('103', '王丽', '女', '1976-01-23', '95033');
insert into student values('104', '李军', '男', '1976-02-20', '95033');
insert into student values('105', '王芳', '女', '1975-02-10', '95031');
insert into student values('106', '陆君', '男', '1974-06-03', '95031');
insert into student values('107', '王尼玛', '男', '1976-02-20', '95033');
insert into student values('108', '张全蛋', '女', '1975-02-10', '95031');
insert into student values('109', '赵铁柱', '男', '1974-06-03', '95031');

# 添加教师表
insert into teacher values('804', '李诚', '男', '1958-12-02', '副教授',  '计算机系');
insert into teacher values('856', '张旭', '男', '1969-03-12', '讲师',  '电子工程系');
insert into teacher values('825', '王萍', '女', '1972-05-05', '助教',  '计算机系');
insert into teacher values('831', '刘冰', '女', '1977-08-14', '助教',  '电子工程系');

# 添加课程表
insert into course values('3-105', '计算机导论', '825');
insert into course values('3-245', '操作系统', '804');
insert into course values('6-166', '数字电路', '856');
insert into course values('9-888', '高等数学', '831');

# 添加成绩表
insert into score values('103', '3-105', '92');
insert into score values('103', '3-245', '86');
insert into score values('103', '6-166', '85');
insert into score values('105', '3-105', '88');
insert into score values('105', '3-245', '75');
insert into score values('105', '6-166', '79');
insert into score values('109', '3-105', '76');
insert into score values('109', '3-245', '68');
insert into score values('109', '6-166', '81');

查询练习

1.查询student表的所有记录

select * from student;
# * 取所有列
1.png

2.查询student表的所有记录的sname、ssex和class列

select sname, ssex, class from student;
2.png

3.查询教师所有的单位,即不重复的depart列

排除重复: distinct

select  distinct depart from teacher;
3.png

4.查询score表中成绩在60-80之间的所有记录

查询区间: between...and...
注:不同数据库对边界问题处理不一

select * from score where degree between 60 and 80;
#方法二:使用运算符比较
select * from score where degree > 60 and where degree < 80; 
4.png

5.查询score表中成绩为85,86或88的记录

表示或者关系的查询: in

select * from score where degree in(85, 86, 88);
5.png

6.查询student表中“95031”班或性别为“女”的同学记录

不同字段的“或”: or

select * from student where class='95031' or ssex='女';
6.png

7.以class降序查询student表中的所有记录

升序:默认
降序:desc

select * from student order by class desc; 
# 删掉desc默认为升序
7.png

8.以cno升序、degree降序查询score表的所有记录

select * from score order by cno asc, degree desc;
8.png

9.查询“95031”班的学生人数

统计:count

select count(*) from student where class='95031';
9.png

10.查询score表中的最高分的学生学号和课程号(子查询或者排序)

select sno, cno from score where degree=(select max(degree) from score);
# 解析:
## step1:找到最高分
select max(degree) from score
## step2:找最高分的学生学号和课程号
10.png
# 使用排序的方法
select sno, cno, degree from score order by degree desc limit 0, 1;
#  limit 0, 1 取第一条
where degree=(select max(degree) from score);
# 但排序的做法有缺陷,如果存在两个最高分会识别不出

11.查询每门课的平均成绩

分组:group by

select avg(degree) from score where cno='3-105'; #计算一门课的平均成绩
select cno,avg(degree) from score group by cno;
11.png

12.查询score表中至少有2名学生选修的并以3开头的课程的平均成绩

select cno,avg(degree) ,count(*) from score 
group by cno 
having count(cno)>=2 
and cno like '3%';  # 以3开头
# 先分组后条件用having,先加条件后分组用where
# count(*) 查询有几个人
12.png

13.查询分数大于70,小于90的sno列

select sno, degree from score 
where degree>70 and degree<90;
# 或:
select sno, degree from score 
where degree between 70 and 90;
13.png

14.查询所有学生的sname、cno和degree列

select sname,cno,degree from student,score 
where student.sno=score.sno;
14.png

15.查询所有学生的sno、cname和degree列

select cno,cname from course;
+-------+-----------------+
| cno   | cname           |
+-------+-----------------+
| 3-105 | 计算机导论      |
| 3-245 | 操作系统        |
| 6-166 | 数字电路        |
| 9-888 | 高等数学        |
+-------+-----------------+
select cno,sno,degree from score;
+-------+-----+--------+
| cno   | sno | degree |
+-------+-----+--------+
| 3-105 | 103 |     92 |
| 3-245 | 103 |     86 |
| 6-166 | 103 |     85 |
| 3-105 | 105 |     88 |
| 3-245 | 105 |     75 |
| 6-166 | 105 |     79 |
| 3-105 | 109 |     76 |
| 3-245 | 109 |     68 |
| 6-166 | 109 |     81 |
+-------+-----+--------+
select sno,cname,degree from course,score 
where course.cno = score.cno;
15.png

16.查询所有学生的sname、cname和degree列

select sname,cname,degree from student,course,score 
where student.sno=score.sno 
and course.cno=score.cno;
16.png

17.查询"95031"班学生每门课的平均分

select * from student where class='95031';
+-----+-----------+------+---------------------+-------+
| sno | sname     | ssex | sbirthday           | class |
+-----+-----------+------+---------------------+-------+
| 102 | 匡明      | 男   | 1975-10-02 00:00:00 | 95031 |
| 105 | 王芳      | 女   | 1975-02-10 00:00:00 | 95031 |
| 106 | 陆君      | 男   | 1974-06-03 00:00:00 | 95031 |
| 108 | 张全蛋    | 女   | 1975-02-10 00:00:00 | 95031 |
| 109 | 赵铁柱    | 男   | 1974-06-03 00:00:00 | 95031 |
+-----+-----------+------+---------------------+-------+

select * from score where sno in 
(select sno from student where class='95031');
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 105 | 3-105 |     88 |
| 105 | 3-245 |     75 |
| 105 | 6-166 |     79 |
| 109 | 3-105 |     76 |
| 109 | 3-245 |     68 |
| 109 | 6-166 |     81 |
+-----+-------+--------+
select cno,avg(degree) from score 
where sno 
in (select sno from student where class='95031') 
group by cno; 
17.png

18.查询选修“3-105”课程的成绩高于“109”号同学“3-105”成绩的所有同学的记录

“所有选修3-105课程的同学中,哪些是高于109号同学的成绩”

select degree from score 
where sno='109' and cno='3-105';
+--------+
| degree |
+--------+
|     76 |
+--------+
select *from score where cno='3-105' and
degree>(select degree from score 
where sno='109' and cno='3-105');
18.png

19.查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录

select * from score 
where degree>(select degree from score where
sno='109' and cno='3-105');
19.png

20.查询和学号为108、101的同学同年出生的所有学生的sno、sname和sbirthday列

select year(sbirthday) from student where sno in (108,101);
+-----------------+
| year(sbirthday) |
+-----------------+
|            1977 |
|            1975 |
+-----------------+
select sno,sname,sbirthday from student 
where year(sbirthday) in 
(select year(sbirthday) from student where sno in (108,101));
20.png

21.查询"张旭"教师任课的学生成绩

select * from teacher where tname='张旭';
+-----+--------+------+---------------------+--------+-----------------+
| tno | tname  | tsex | tbirthday           | prof   | depart          |
+-----+--------+------+---------------------+--------+-----------------+
| 856 | 张旭   | 男   | 1969-03-12 00:00:00 | 讲师   | 电子工程系      |
+-----+--------+------+---------------------+--------+-----------------+
select * from course 
where tno=(select tno from teacher where tname='张旭');
+-------+--------------+-----+
| cno   | cname        | tno |
+-------+--------------+-----+
| 6-166 | 数字电路     | 856 |
+-------+--------------+-----+
select * from score where cno=
(select cno from course 
where tno=(select tno from teacher where tname='张旭'));
21.png

22.查询选修某课程的同学人数多余5人的教师姓名

select * from score;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-105 |     92 |
| 103 | 3-245 |     86 |
| 103 | 6-166 |     85 |
| 105 | 3-105 |     88 |
| 105 | 3-245 |     75 |
| 105 | 6-166 |     79 |
| 109 | 3-105 |     76 |
| 109 | 3-245 |     68 |
| 109 | 6-166 |     81 |
+-----+-------+--------+
#发现没有多于5个的课程
#添加数据:
insert into score values ('101', '3-105', '90');
insert into score values ('102', '3-105', '91');
insert into score values ('104', '3-105', '89');

select cno from score group by cno having count(*)>5;
+-------+
| cno   |
+-------+
| 3-105 |
+-------+
select tno from course where cno=
(select cno from score group by cno having count(*)>5);
+-----+
| tno |
+-----+
| 825 |
+-----+
select tname from teacher where tno=
(select tno from course where cno=
(select cno from score group by cno having count(*)>5));
+--------+
| tname  |
+--------+
| 王萍   |
+--------+

23.查询95033班和95031班全体学生的记录

insert into student values('110', '张飞', '男', '1977-09-01', '95038');
select * from student where class in ('95033', '95031');
+-----+-----------+------+---------------------+-------+
| sno | sname     | ssex | sbirthday           | class |
+-----+-----------+------+---------------------+-------+
| 101 | 曾华      | 男   | 1977-09-01 00:00:00 | 95033 |
| 102 | 匡明      | 男   | 1975-10-02 00:00:00 | 95031 |
| 103 | 王丽      | 女   | 1976-01-23 00:00:00 | 95033 |
| 104 | 李军      | 男   | 1976-02-20 00:00:00 | 95033 |
| 105 | 王芳      | 女   | 1975-02-10 00:00:00 | 95031 |
| 106 | 陆君      | 男   | 1974-06-03 00:00:00 | 95031 |
| 107 | 王尼玛    | 男   | 1976-02-20 00:00:00 | 95033 |
| 108 | 张全蛋    | 女   | 1975-02-10 00:00:00 | 95031 |
| 109 | 赵铁柱    | 男   | 1974-06-03 00:00:00 | 95031 |
+-----+-----------+------+---------------------+-------+

24.查询存在有85分以上成绩的课程cno

select cno from score where degree>85 group by cno;
+-------+
| cno   |
+-------+
| 3-105 |
| 3-245 |
+-------+

25.查询"计算机系"所有教师所教课程的成绩表

select  tno from teacher where depart='计算机系';
+-----+
| tno |
+-----+
| 804 |
| 825 |
+-----+
select cno from course where tno in
(select  tno from teacher where depart='计算机系');
+-------+
| cno   |
+-------+
| 3-245 |
| 3-105 |
+-------+

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

26.查询"计算机系"与“电子工程系”不同职称的教师的tname和prof

求并:union

# 并集-交集,这题有点儿问题,不用纠结
select * from teacher;
+-----+--------+------+---------------------+-----------+-----------------+
| tno | tname  | tsex | tbirthday           | prof      | depart          |
+-----+--------+------+---------------------+-----------+-----------------+
| 804 | 李诚   | 男   | 1958-12-02 00:00:00 | 副教授    | 计算机系        |
| 825 | 王萍   | 女   | 1972-05-05 00:00:00 | 助教      | 计算机系        |
| 831 | 刘冰   | 女   | 1977-08-14 00:00:00 | 助教      | 电子工程系      |
| 856 | 张旭   | 男   | 1969-03-12 00:00:00 | 讲师      | 电子工程系      |
+-----+--------+------+---------------------+-----------+-----------------+
select * from teacher where depart='计算机系' and prof not in
(select prof from teacher where depart='电子工程系')
union
select * from teacher where depart='电子工程系' and prof not in
(select prof from teacher where depart='计算机系');
26.png

27.查询选修编号为“3-105”课程且成绩至少高于选修编号为“3-245”的同学的cno、sno和degree,并按degree从高到低次序排序

select * from score where cno='3-245';
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-245 |     86 |
| 105 | 3-245 |     75 |
| 109 | 3-245 |     68 |
+-----+-------+--------+
select * from score where cno='3-105';
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 101 | 3-105 |     90 |
| 102 | 3-105 |     91 |
| 103 | 3-105 |     92 |
| 104 | 3-105 |     89 |
| 105 | 3-105 |     88 |
| 109 | 3-105 |     76 |
+-----+-------+--------+
# ---至少?大于其中至少一个,any(题目有点儿问题,不用管它)
select * from score 
where cno='3-105'
and degree>any(select degree from score where cno='3-245') 
order by degree desc;
27.png

28.查询选修编号为“3-105”课程且成绩高于选修编号为“3-245”的同学的cno、sno和degree

且:all

select * from score 
where cno='3-105'
and degree>all(select degree from score where cno='3-245') ;

29.查询所有教师和同学的name、sex和birthday

别名:as

select sname as name,ssex as sex,sbirthday as birthday from student 
union 
select tname,tsex,tbirthday from teacher;
29.png

30.查询所有“女”教师和“女”同学的name、sex和birthday

select tname as name,tsex as sex,tbirthday as birthday from teacher 
where tsex='女' 
union 
select sname,ssex,sbirthday from student where ssex='女';

31.查询成绩比该课程平均成绩低的同学的成绩表

select cno,avg(degree) from score group by cno;
+-------+-------------+
| cno   | avg(degree) |
+-------+-------------+
| 3-105 |     87.6667 |
| 3-245 |     76.3333 |
| 6-166 |     81.6667 |
+-------+-------------+
select * from score as a where degree<
(select avg(degree) from score as b where a.cno=b.cno);
# 这里把score表复制一份,两份表分别为a 和b
31.png

32.查询所有任课教师的tname和depart

select * from teacher;
+-----+--------+------+---------------------+-----------+-----------------+
| tno | tname  | tsex | tbirthday           | prof      | depart          |
+-----+--------+------+---------------------+-----------+-----------------+
| 804 | 李诚   | 男   | 1958-12-02 00:00:00 | 副教授    | 计算机系        |
| 825 | 王萍   | 女   | 1972-05-05 00:00:00 | 助教      | 计算机系        |
| 831 | 刘冰   | 女   | 1977-08-14 00:00:00 | 助教      | 电子工程系      |
| 856 | 张旭   | 男   | 1969-03-12 00:00:00 | 讲师      | 电子工程系      |
+-----+--------+------+---------------------+-----------+-----------------+
select * from course;
+-------+-----------------+-----+
| cno   | cname           | tno |
+-------+-----------------+-----+
| 3-105 | 计算机导论      | 825 |
| 3-245 | 操作系统        | 804 |
| 6-166 | 数字电路        | 856 |
| 9-888 | 高等数学        | 831 |
+-------+-----------------+-----+
select tname,depart from teacher 
where tno in (select tno from course);
+--------+-----------------+
| tname  | depart          |
+--------+-----------------+
| 李诚   | 计算机系        |
| 王萍   | 计算机系        |
| 刘冰   | 电子工程系      |
| 张旭   | 电子工程系      |
+--------+-----------------+

33.查询至少有2名男生的班号

select class from student 
where ssex='男' 
group by class having count(*)>1;
+-------+
| class |
+-------+
| 95033 |
| 95031 |
+-------+

34.查询student表中不姓“王”的同学记录

select * from student where sname not like '王%';

35.查询student表中每个学生的姓名和年龄

年龄:当前年份-出生年份
当前年份:year(now())

select sname,year(now())-year(sbirthday) as '年龄' from student;
+-----------+--------+
| sname     | 年龄   |
+-----------+--------+
| 曾华      |     43 |
| 匡明      |     45 |
| 王丽      |     44 |
| 李军      |     44 |
| 王芳      |     45 |
| 陆君      |     46 |
| 王尼玛    |     44 |
| 张全蛋    |     45 |
| 赵铁柱    |     46 |
| 张飞      |     43 |
+-----------+--------+

36.查询student表中最大最小的sbirthday日期值

max
min

select max(sbirthday) as '最大', min(sbirthday) as '最小' from student;
+---------------------+---------------------+
| 最大                | 最小                |
+---------------------+---------------------+
| 1977-09-01 00:00:00 | 1974-06-03 00:00:00 |
+---------------------+---------------------+
#根据数字大小选取

37.以班号和年龄从大到小的顺序查询student表中的全部记录

select * from student order by class desc,sbirthday; 
37.png

38.查询"男"教师及其所上的课程

select * from course where tno in 
(select tno from teacher where tsex='男');

39.查询最高分同学的sno、cno和degree列

select sno,cno,degree from score 
where degree = (select max(degree) from score);

40.查询和“李军”同性别的所有同学的sname

select sname from student 
where ssex=(select ssex from student where sname='李军');

41.查询和“李军”同性别并同伴的同学sname

select sname from student 
where ssex=(select ssex from student where sname='李军') 
and class=(select class from student where sname='李军');

42.查询所有选修“计算机导论”课程的“男”同学的成绩表

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

43.假设使用以下命令建立了一个grade表:

create table grade(
    low int(3), 
    upp int(3), 
    grade 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和grade列

select sno,cno,grade from score,grade 
where degree between low and upp;
43.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,539评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,594评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,871评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,963评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,984评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,763评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,468评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,850评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,002评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,144评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,823评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,483评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,026评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,150评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,415评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,092评论 2 355

推荐阅读更多精彩内容

  • 最近打算采用关系型数据库来理一下公司的运营数据,先拿点东西练手找感觉。下面是几个关于学生课业的表,需要建立一个数据...
    九天朱雀阅读 981评论 0 3
  • 1).创建数据库 create database学生选课数据库 2).创建四张表 Create table Stu...
    blvftigd阅读 1,586评论 0 0
  • 学习有一段时间了,再次练习题目; #创建表 student create table student (sno v...
    艾马丫阅读 436评论 0 0
  • 原文:https://www.cnblogs.com/aqxss/p/6563625.html 一、设有一数据库,...
    名门翘楚C阅读 1,100评论 0 0
  • 常见的学生老师课程分数: 建表: CREATE TABLE students (sno VARCHAR(3) NO...
    ThomasAAnderson阅读 414评论 0 0