1、创建表

表一:student学生use)
1、create table student(
sno varchar(20) primary key not null comment'学号(主码)',
sname varchar(20) not null comment'学生姓名',
ssex varchar(20) not null comment'学生性别',
sbirthday datetime comment'学生出生年月',
class varchar(20) comment'学生所在班级'
);
表(二)Course(课程表)
create table course(
cno varchar(20) primary key not null comment'课程号(主码)',
cname varchar(20) not null comment'课程名称',
tno varchar(20) not null comment'教工编号'
);
表(三)Score(成绩表)
create table score(
id int primary key auto_increment comment'主键自增',
sno varchar(20) not null comment'学号',
cno varchar(20) not null comment'课程号',
degree Decimal(4,1) comment'成绩'
);
表四 teacher(教师表)
create table teacher(
tno varchar(20) primary key not null comment'教工编号(主码)',
tname varchar(20) not null comment'教工姓名',
tsex varchar(20) not null comment'教工性别',
tbirthday datetime comment'教工出生年月',
prof varchar(20) comment'职称',
depart varchar(20) not null comment'教工所在部门'
);

2、在表中添加数据

学生表数据的插入:
insert into student values
('','曾华','男','1977-09-01',''),
('','匡明','男','1977-09-01',''),
('','王丽','女','1977-09-01',''),
('','李军','男','1977-09-01',''),
('','王芳','女','1977-09-01',''),
('','陆君','男','1977-09-01','');
课程表数据的插入:
insert into course values
('3-105','计算机导论',''),
('3-245','操作系统',''),
('6-166','数字电路',''),
('9-888','高等数学','');
成绩表数据的插入:
select cno from score group by cno; #找出这个表中所有的班级
insert into score(sno,cno,degree) values
('','3-245',''),
('','3-245',''),
('','3-245',''),
('','3-105',''),
('','3-105',''),
('','3-105',''),
('','3-105',''),
('','3-105',''),
('','3-105',''),
('','6-166',''),
('','6-166',''),
('','6-166','');
教师表数据的插入:
insert into teacher values
('','李诚','男','1958-12-02','副教授','计算机系'),
('','张旭','男','1969-03-12','讲师','电子工程系'),
('','王萍','女','1972-05-05','助教','计算机系'),
('','刘冰','女','1977-08-14','助教','电子工程系');

3、SELECT查询

select [all | distinct] 字段或表达式列表 [from子句] [where子句] [group by子句] [having子句] [order by子句] [limit子句];

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 * from score where degree>60 and degree<80;
5、 查询Score表中成绩为85,86或88的记录。
select * from score where degree in (85,86,88);
6、 查询Student表中“95031”班或性别为“女”的同学记录。
select * from student where class like '' or ssex like '女';
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 group by class;
10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
select * from score order by degree desc limit 1;
下面这个使用的连接查询
select max(degree) from score #先写出score的最高分
select * from score where degree = (select max(degree) from score);
select sno,cno from score where degree = (select max(degree) from score);
11、 查询每门课的平均成绩。
select cno,count(*),avg(degree) from score group by cno having count(*); 多条查询
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select cno,avg(degree) from score where cno like '3-105' and cno like '3%';
select * from score group by degree;
select cno,count(*),avg(degree) from score where cno like '3%' group by cno having count(*) >= 5;
select cno,count(*),avg(degree) from score where cno like '3%' group by cno having count(*) >= 5;
13、查询分数大于70,小于90的Sno列。
select sno,degree from score where degree>70 and degree<90;
select group_concat(sno) from score where degree>70 and degree<90;
14、查询所有学生的Sname、Cno和Degree列。(多表查询)
student.sname,course.cno,score.degree
select student.sname,course.cno,score.degree from student,course,score;
select sname,cno,degree from student join score on student.sno = score.sno;
15、查询所有学生的Sno、Cname和Degree列。
student.sno,course.cname,score.degree
select student.sno,course.cname,score.degree from student,course,score;
select sno,cname,degree from score join course on course.cno = score.cno;
16、查询所有学生的Sname、Cname和Degree列。
student.sname,course.cname,score.degree;
select student.sname,course.cname,score.degree from student,course,score;
select student.sname,course.cname,score.degree from student,course,score where sname between '李军' and '王丽';
select student.sname,cname,degree from student join score on student.sno = score.sno join course on course.cno = score.cno;
17、查询“95033”班学生的平均分。
select sno from student where class = '';
select avg(degree) from score where sno in(select sno from student where class = '');
18、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
select degree from score where sno = '' and cno = '3-105';
select sno,degree from score where degree > (select degree from score where sno = '' and cno = '3-105');
19、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
select cno from score where (cno = '3-245' and cno = '3-105') or (cno = '3-245' and cno = '6-166') or (cno = '3-245' and cno = '6-166'); 这是个错误的
select * from Score a where Sno in (select Sno from Score group by Sno having count(*)>1 ) and Degree not in (select max(Degree) from Score b where b.Cno = a.Cno)
select * from Score a where Sno in (select Sno from Score group by Sno having count(*)>1 ) and Degree not in (select max(Degree) from Score a where Sno in (select Sno from Score group by Sno having count(*)>1 ))
20、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select degree from score where sno = '' and cno = '3-105';
select * from score where degree > (select degree from score where sno = '' and cno = '3-105');
21、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
select sbirthday from student where sno = '';
select sno,sname,sbirthday from student where sbirthday = (select sbirthday from student where sno = '');
22、查询“张旭“教师任课的学生成绩(姓名)。
select tno from teacher where tname = '张旭'; #找出教师编号
select cno from course where tno = (select tno from teacher where tname = '张旭'); #找出课程编号
select sno,degree from score where cno = (select cno from course where tno = (select tno from teacher where tname = '张旭'));
select student.sno,degree,sname from score join student on score.sno = student.sno where student.sno in (select sno from score where cno = (select cno from course where tno = (select tno from teacher where tname = '张旭')));
23、查询考计算机导论的学生成绩
select cno from course where cname = '计算机导论'; #找到课程编号3-105
select sno,degree from score where cno = (select cno from course where cname = '计算机导论');
24、查询李诚老师教的课程名称
select tno from teacher where tname = '李诚'; ##找到教师编号
select cname from course where tno = (select tno from teacher where tname = '李诚');
25、教高等数学的老师是哪个系的
select tno from course where cname = '高等数学';
select depart from teacher where tno = (select tno from course where cname = '高等数学');
26、查询选修某课程的同学人数多于5人的教师姓名。
select cno,count(*) from score group by cno having count(*)>=5; #找出课程编号
select tno from course where cno = (select cno from score group by cno having count(*)>=5);
select tname from teacher where tno = (select tno from course where cno = (select cno from score group by cno having count(*)>=5));
27、查询95033班和95031班全体学生的记录。
select * from student group by class having count(*);
select * from student order by class desc;
28、查询成绩表中存在有85分以上成绩的课程Cno.
select cno,degree from score where degree>85;
29、查询出“计算机系“教师所教课程的成绩表。
select tno,tname from teacher where depart = '计算机系' #查出教师编号
select cno from course where tno in (select tno from teacher where depart = '计算机系'); #查出课程编号
select sno,cno,degree from score where cno in (select cno from course where tno in (select tno from teacher where depart = '计算机系'));
30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的 Cno、Sno和Degree.
select max(degree) from score where cno = '3-245'; #先把选修编号为3-245课程的同学的最高成绩查询出来
select cno,sno,degree from score where cno = '3-105' and degree > (select max(degree) from score where cno = '3-245');
31、查询所有教师和同学的name、sex和birthday.
select tname as name,tsex as sex,tbirthday as birthday from teacher
union
select sname,ssex,sbirthday from student;
32、查询所有“女”教师和“女”同学的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 = '女';
33、查询所有成绩比3-105课程平均成绩低的同学的成绩表。
select avg(degree) from score where cno = '3-105';
select degree from score where degree < (select avg(degree) from score where cno = '3-105');
34、查询所有任课教师的Tname和Depart.
select tname,depart from teacher;
35、查询所有未讲课的教师的Tname和Depart. 
select tno from course group by tno; #找出有课的老师的编号
select tname,depart from teacher where not exists(select tno from course group by tno);
36、查询至少有2名男生的班号。#####################################################3
select ssex,class from student where ssex = '男' group by class;
select class from student where exists ((select ssex,class from student where ssex = '男' group by class) * 2);
37、查询Student表中不姓“王”的同学记录。
select sname from student where sname like '王%'
select sname from student where sname not in (select sname from student where sname like '王%');
38、查询Student表中每个学生的姓名和年龄。
select sname,
select floor(datediff(curdate(),@birthday)/365.2422)
39、查询Student表中最大和最小的Sbirthday日期值。
select max(sbirthday) as '最大日期' , min(sbirthday) as '最小日期' from student;
update student set sbirthday = '1995-07-11' where sno = '';
update student set sbirthday = '1820-05-01' where sno = '';
40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
select * from student order by class desc,sbirthday desc;
41、查询“男”教师及其所上的课程。
select tno from teacher where tsex = '男';
select cname from course where tno in (select tno from teacher where tsex = '男');
42、查询最高分同学的Sno、Cno和Degree列。
select max(degree) from score
select score.sno,cno,degree from student
join
score
on student.sno = score.sno
where degree = (select max(degree) from score);
43、查询和“李军”同性别的所有同学的Sname.
select ssex from student where sname = '李军';
select sname from student where ssex = (select ssex from student where sname = '李军');
44、查询和“李军”同性别并同班的同学Sname.
select class from student where sname = '李军';
select sname from student where ssex = (select ssex from student where sname = '李军') and class = (select class from student where sname = '李军');
45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
select cno from course where cname = '计算机导论'; #根据课程表找到课程编号
select sno from score where cno = (select cno from course where cname = '计算机导论'); #根据课程编号找到成绩表里面的学生编号
select sname from student where sno in (select sno from score where cno = (select cno from course where cname = '计算机导论')) and ssex = '男';

MYSQL数据库练习题操作(select)大全的更多相关文章

  1. Mysql数据库常用操作语句大全

    零.用户管理: 1.新建用户: >CREATE USER name IDENTIFIED BY 'ssapdrow'; 2.更改密码: >SET PASSWORD FOR name=PAS ...

  2. DBA必备:MySQL数据库常用操作和技巧

    DBA必备:MySQL数据库常用操作和技巧 2011-02-25 15:31 kaduo it168 字号:T | T MySQL数据库可以说是DBA们最常见和常用的数据库之一,为了方便大家使用,老M ...

  3. C语言对mysql数据库的操作

    原文:C语言对mysql数据库的操作 这已经是一相当老的话题.不过今天我才首次使用,把今天的一些体会写下来,也许能给一些新手带来一定的帮助,更重要的是供自己今后忘记的怎么使用而进行查阅的! 我们言归正 ...

  4. 【Python全栈-后端开发】MySQL数据库-练习题

    MySQL数据库-练习题 一.表关系 请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 3.查询平均成绩大于60分的同学的学号 ...

  5. Mysql数据库基础操作

    Mysql数据库基础操作 在mysql数据库中开启使用tab键补全功能 1)修改主配置文件/etc/mysql/my.cnf(mysql和mariadb目录有些不同) vim /etc/mysql/m ...

  6. django 中连接mysql数据库的操作步骤

    django中连接mysql数据库的操作步骤: 1 settings配置文件中 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mys ...

  7. MySQL 数据库高级操作 (配图)

    MySQL数据库高级操作 1.一键部署mysql 数据库 2.数据表高级操作 3.数据库用户管理 4.数据库用户授权 1.首先一键部署mysql 数据库 : 可以看我之前的博客 https://www ...

  8. zabbix数据库mariadb从服务器迁移到云mysql数据库的操作

    zabbix数据库mariadb从本机迁移到云mysql数据库的操作 1.将zabbix数据库导出,并导入到云数据库中 由于数据库较大,如果直接使用shell会话中断会导致数据库导出或者导入失败,使用 ...

  9. Js操作Select大全(取值、设置选中)

    Js操作Select是很常见的,也是比较实用的. jquery操作select(取值,设置选中) 每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<s ...

随机推荐

  1. 如何有效管理Windows系统帐户权限

    权限是Windows管理的基础,当然与Windows用户关系最密切,平时接触最多的是与帐户相关的权限.对于Windows帐户权限的管理,你是否完全了解呢?下面,笔者以Winsows XP为例进行相关测 ...

  2. Lost's revenge HDU - 3341 AC自动机+DP(需要学会如何优雅的压缩状态)

    题意: 给你n个子串和一个母串,让你重排母串最多能得到多少个子串出现在重排后的母串中. 首先第一步肯定是获取母串中每个字母出现的次数,只有A T C G四种. 这个很容易想到一个dp状态dp[i][A ...

  3. 使用CEfSharp之旅(6)拦截网络请求 截取response返回

    原文:使用CEfSharp之旅(6)拦截网络请求 截取response返回 版权声明:本文为博主原创文章,未经博主允许不得转载.可点击关注博主 ,不明白的进群191065815 我的群里问 https ...

  4. CSS——div内文字的溢出部分用省略号显示

    使得div内文字的溢出部分用省略号显示,可归纳为两种解决办法,一种方法是用CSS解决,另一种方法是js解决. 一.通过CSS控制显示 div内显示一行,超出部分用省略号显示 div内显示多行,超出部分 ...

  5. vue 返回上一页

    参考:https://www.cnblogs.com/chenguiya/p/9118265.html 注:需为history模式 方法一: @click="back" back( ...

  6. java笔试之参数解析(正则匹配)

    在命令行输入如下命令: xcopy /s c:\ d:\, 各个参数如下: 参数1:命令字xcopy 参数2:字符串/s 参数3:字符串c:\ 参数4: 字符串d:\ 请编写一个参数解析程序,实现将命 ...

  7. 2019-2-17-如何在-Windows-10-中移除-Internet-Explorer-浏览器

    title author date CreateTime categories 如何在 Windows 10 中移除 Internet Explorer 浏览器 lindexi 2019-02-17 ...

  8. shell 版本号比较_用shell如何比较软件版本号的大小

    比如你想写个脚本来比较两个版本号 (如"1.2.30" 和"1.3.0")来辨认哪个版本是最新的,有可以比较两个版本号字符串的shell脚本吗? 当你写了一个s ...

  9. java.net.UnknownHostException 异常处理(转)

    在linux系统下部署Java产品的集群环境时,后台报出如下异常,系统报找不到名为“QATest242”的主机: ERROR  - Get local host name failed -com.tr ...

  10. LoadRunner如何调用外部函数

    LoadRunner如何调用外部函数 使用 VuGen 时,可以调用在外部 DLL 中定义的函数.通过从脚本调用外部函数,可以降低脚本的内存使用量以及总体运行时间.要调用外部函数,需要加载定义了该函数 ...