【mysql】排序方操作50题练习及其答案
1.创建数据库、相关表,并插入数据
create database homework;
use homework;
create table class_grade(gid int primary key auto_increment, gname char(16));
create table class(cid int primary key auto_increment, caption char(16), grade_id int, foreign key(grade_id) references class_grade(gid) on delete cascade on update cascade);
create table student(sid int primary key auto_increment, sname char(16), gender char(6) not null default '男', class_id int,foreign key(class_id) references class(cid) on delete cascade on update cascade);
create table teacher(tid int primary key auto_increment, tname char(16));
create table course(cid int primary key auto_increment, cname char(16), teacher_id int, foreign key(teacher_id) references teacher(tid) on delete cascade on update cascade);
create table score(sid int primary key auto_increment, student_id int,course_id int,score int unsigned, foreign key(student_id) references student(sid) on delete cascade on update cascade,foreign key(course_id) references course(cid) on delete cascade on update cascade);
create table teach2cls(tcid int primary key auto_increment, tid int, cid int, foreign key(tid) references teacher(tid), foreign key(cid) references class(cid) on delete cascade on update cascade);
insert into class_grade(gname) values('一年级'),('二年级'),('三年级');
insert into class(caption, grade_id) values('一年一班',1),('二年一班',2),('三年一班',3);
insert into student(sname, gender, class_id) values('乔丹','女',1),('艾弗森', '女', 1), ('科比', '男', 2);
insert into teacher(tname) values('张三'),('李四'),('王五');
insert into course(cname, teacher_id) values('生物',1), ('体育',1), ('物理',2);
insert into score(student_id, course_id, score) values(1,1,60),(1,2,59),(2,2,99);
insert into teach2cls(tid, cid) values(1,1),(1,2),(2,1),(3,2);
2. 查询学生总人数
select count(sid) from student;
3.查询'生物' 和 '物理' 课程成绩都及格的学生
select sid,sname from student where sid in(select student_id from score as t1 right join (select cid from course where cname in('生物', '物理')) as t2 on t1.course_id= t2.cid where score >= 60 group by student_id);
4.查询每个年级的班级数,取出班级数最多的三个年级
select * from class_grade as t1 right join(select grade_id,count(cid) as count from class group by(grade_id))as t2 on t1.gid=t2.grade_id order by count desc limit 3;
5.查询平均成绩最高和最低的学生ID和姓名以及成绩
select sid,sname,avg from student as t1 right join (
select student_id,avg(score) as avg from score group by student_id
) as t2 on t1.sid=t2.student_id
where avg in(
(select max(avg) from (select avg(score) as avg from score group by student_id) as tmax),
(select min(avg) from (select avg(score) as avg from score group by student_id) as tmin)
);
6.查询每个年级的学生人数
select gname,count(sid) from (select * from student right join class on student.class_id=class.cid) as t1
left join
(select * from class left join class_grade on class.grade_id=class_grade.gid) as t2
on t1.cid=t2.cid group by gid;
7.每位学生的学号, 姓名, 选课数, 平均成绩
select sid,sname,count(course_id),avg(score) from (select student.sid,sname,course_id,score from student left join score on student.sid=score.student_id) as t1 group by sid;
8.查询学生编号为2的学生姓名,该学生最高成绩的课程名, 成绩最低的课程名及分数
set @ssid =1;
select sname,cname,score from
(select student.sid,sname,course_id,score from score left join student on score.student_id=student.sid
where student_id=@ssid and score in(
(select max(score) from score where student_id=@ssid),
(select min(score) from score where student_id=@ssid))
)as t1 left join course as t2 on t1.course_id=t2.cid
order by score;
9.查询姓'李'的老师个数和所带班级数
set @first_name='李%';
select GROUP_CONCAT(tname),count(tname),count(cid) from teacher as t1 left join teach2cls as t2 on t1.tid=t2.tid where tname like @first_name ;
10.查询班级数小于5的年级id和年级名
select gid,gname from class_grade left join class on class_grade.gid=class.grade_id group by gid having count(cid)<5;
11.输出班级信息并增加一列显示年级的文字级别(1,2为低年级, 3,4为中年级, 5,6为高年级)
select
cid,
caption,
gname,
if((select grade_id from class where cid=t1.cid)<3,'低年级',
if((select grade_id from class where cid=t1.cid)<5,'中年级','高年级')
) as grade_level
from class as t1 left join class_grade as t2 on t1.grade_id=t2.gid;
12.张三老师2门课以上的粉丝id,名字
select student.sid,sname,count(student.sid) from student
left join (select * from (select * from teacher
right join course on teacher.tid=course.teacher_id where tname='张三') as t1
right join score on t1.cid=score.course_id) as t2 on student.sid=t2.student_id
group by student.sid having count(student.sid)>=2;
13.查询教授超过两门课的老师的ID 及 名字
select tid,tname from teacher left join course on teacher.tid=course.teacher_id group by tid having count(cid)>=2;
14.查询学过编号1 课程 和编号2课程的同学的学号与姓名
select student_id,sname from score left join student on score.student_id=student.sid group by student_id having count(course_id)>=2;
15.查询没有带过高年级的老师id和姓名
select tid,tname from (select teacher.tid,tname,cid from teacher left join teach2cls on teacher.tid=teach2cls.tid) as t1 left join class as t2 on t1.cid=t2.cid group by tid having max(grade_id)<4;
16.查询学过'张三'老师所教的所有课的同学学号、姓名
select sid,sname from (select * from teacher left join course on teacher.tid=course.teacher_id) as t1
left join (select student.sid,sname,course_id from student left join score on student.sid=score.student_id) as t2 on t1.cid=t2.course_id where tname='张三';
17.查询带超过2个班级的老师id和姓名
select teacher.tid,teacher.tname from teacher left join teach2cls on teacher.tid=teach2cls.tid group by tid having count(cid)>=2;
18.查询课程编号2的成绩比课程编号1课程低的所有同学的学号,姓名
select sid,sname from student as t1 right join (select student_id,score as score_2,(select score from score where course_id=1 and student_id=a.student_id) as score_1 from score a where course_id=2 having score_1>score) as t2 on t1.sid=t2.student_id;
19.查询所带班级数最多的老师ID和姓名
select teacher.tid,tname,count(cid) from teacher left join teach2cls on teacher.tid=teach2cls.tid group by tid order by count(cid) desc limit 1;
20.查询有成绩不及格的同学学号、姓名
select distinct student.sid,sname from student left join score on student.sid=score.student_id where score<60;
21.没有学全所有课的同学学号、姓名
select student.sid,sname,count(course_id) from student left join score on student.sid=score.student_id group by student.sid having count(course_id)<(select count(cid) from course);
22.至少有一门课与学号为 1 的同学所学相同的学生学号及姓名
select distinct student.sid,sname from student left join score on student.sid=score.student_id where course_id in (select course_id from score where student_id=1);
23.22题,其他同学
select distinct student.sid,sname from student left join score on student.sid=score.student_id where student.sid != 1 and course_id in (select course_id from score where student_id=1);
24.查询与2号同学所学习课程完全相同的同学学号及姓名 用group_concat()拼接
select student.sid,sname,group_concat(course_id) as courses from student left join score on student.sid=score.student_id where student.sid!=2 group by student.sid having courses = (select group_concat(course_id) from score group by student_id having student_id=2);
25.删除张三老师的课的score记录; (课程1,课程2的成绩记录全被删了。。。)
delete from score where course_id in(select cid from course left join teacher on course.teacher_id=teacher.tid where tname='张三');
26.向score插入一些记录,这些记录要求符合以下条件:1.没有上过编号2课程同学学号,2.插入2号课程的平均成绩
## 插入内容为: student_id=3, course_id=2,score=(avg(score))
insert into score(student_id,course_id,score) select sid,2,(select avg(score) from score group by course_id having course_id=2) from student where sid not in(SELECT student_id from score where course_id=2);
27.按平均成绩从低到高显示所有学生的“语文”“数学”“英语”三门的课程成绩,按如下内容显示:
学生ID,语文,数学,英语,有效课程数,有效平均分
select student_id as 学生ID,
(select score from score left join course on score.course_id=course.cid where cname='语文' and student_id=t1.student_id) as '语文',
(select score from score left join course on score.course_id=course.cid where cname='数学' and student_id=t1.student_id) as '数学',
(select score from score left join course on score.course_id=course.cid where cname='英语' and student_id=t1.student_id) as '英语',
count(course_id) as 有效课程数,
avg(score) as 有效平均分
from score as t1 left join course on t1.course_id=course.cid where cname in('语文','数学','英语') group by student_id order by 有效平均分 asc;
28.查询各科成绩最高分与最低分显示形式: 课程ID 最高课程 最低成绩
select
course.cid as 课程ID,
(select max(score) from score group by course_id having course_id=t.course_id) as 最高成绩,
(select min(score) from score group by course_id having course_id=t.course_id) as 最低成绩
from score as t right join course on t.course_id=course.cid group by course.cid;
29.按各科平均成绩从低到高和及格率的百分数从高到低排序
select
course.cid as 课程ID,
avg(score) as 平均成绩,
concat(ifnull((select count(student_id) from score where score > 60 group by course_id having course_id=t.course_id),0)/
(select count(student_id) from score group by course_id having course_id=t.course_id)*100
, '%') as 及格率
from score as t right join course on t.course_id=course.cid
group by course.cid
order by 平均成绩 asc ,及格率 desc;
30.课程平均分从高到低显示(现实任课老师)
select cid,cname,avg(score),tname
from teacher as t1
right join (select * from score right join course on score.course_id=course.cid) as t2
on t1.tid=t2.teacher_id
group by cid
order by avg(score) desc;
31.查询各科成绩前三名的记录(),不考虑并列的情况;
select
a.course_id as 课程ID,
a.score as 成绩,
count(a.course_id) as 排名
from score a left join score b on a.course_id=b.course_id and a.score<=b.score
group by a.course_id,a.score
having count(a.course_id)<4
order by a.course_id,a.score desc;
32.查询每门课程被选修的学生数
select cid,cname,count(student_id) as 选修学生数 from score right join course on score.course_id=course.cid group by cid;
33.查询选修了2门以上课程的全部学生的学号和姓名
select student.sid,sname from student left join score on student.sid=score.student_id group by student.sid having count(course_id)>=2;
34.查询男生女生人数,按倒序排列
select gender,count(sid) from student group by gender order by count(sid) desc;
35.查询姓张的学生名单
select * from student where sname like '张%';
36.查询同名同姓学生名单,并统计同名人数
select sname,group_concat(sid) as 学号,count(sid) as 重名人数 from student group by sname having count(sid)>1;
37.查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
select cid as 课程号,cname as 课程名,ifnull(avg(score),0) as 平均成绩 from score right join course on score.course_id=course.cid group by cid order by 平均成绩 asc,课程号 desc;
38.查询课程名称为数学且分数低于60的学生姓名与分数
select sname,score from student as t1 right join (select * from score right join course on score.course_id=course.cid where cname = '数学' and score<60) as t2 on t1.sid=t2.student_id;
39.查询课程编号为3 且课程成绩在80分以上的学生学号及姓名
select student.sid,sname from student left join score on student.sid=score.student_id where course_id=3 and score>=80;
40.求选修了课程的学生人数
select count(distinct sid) from score;
41.查询选修王五老师所教授课程的学生中成绩最高和最低的学生姓名及其成绩
select * from (select '最低成绩',sname,score from score left join student on score.student_id=student.sid where course_id in(
select cid from course left join teacher on course.teacher_id=teacher.tid where tname='王五') order by score limit 1) as a
union
select * from (select '最高成绩',sname,score from score left join student on score.student_id=student.sid where course_id in(
select cid from course left join teacher on course.teacher_id=teacher.tid where tname='王五') order by score desc limit 1) as b;
42.查询各个课程及其选修人数
select cname,count(student_id) from score right join course on score.course_id=course.cid group by cname;
43.查询课程不同但成绩相同的学生的学号、课程号、学生成绩
select distinct a.student_id as 学号,a.course_id as 课程号, a.score as 成绩
from score a left join score b on a.score = b.score
where a.course_id != b.course_id;
44.查询每门课程成绩最好的前两名学生id 和姓名
select course_id as 课程编号,
sid as 学号,
sname as 姓名
from student right join (select a.course_id,a.student_id,a.score,count(a.course_id) from score a left join score b on a.course_id=b.course_id and a.score<b.score group by a.course_id,a.student_id,a.score having count(a.course_id)<2 order by course_id) as t on student.sid=t.student_id order by course_id;
45.检索至少选修2门课程的学生学号
select student_id from score group by student_id having count(course_id)>=2;
46.查询没有学生选修的课程号及课程名
select cid,cname from score right join course on score.course_id=course.cid group by cid having count(student_id)<1;
47.查询没有带过任何班级的老师id 和姓名
select teacher.tid,tname from teacher left join teach2cls on teacher.tid=teach2cls.tid group by teacher.tid having count(cid)<1;
48.查询2门以上课程超过80分的同学id 及其平均成绩
select student_id,avg(score) from score where student_id in (select student_id from score where score > 80 group by student_id having count(course_id)>=2) group by student_id;
49 检索 3 号课程分数小于60的同学学号,按分数降序排列
select student_id,score from score where course_id=3 and score<60 order by score desc;
50 删除编号为2的同学的1课程成绩
delete from score where student_id=2 and course_id=1;
51 查询同时修了物理课和生物课的学生id和姓名
select student.sid,sname from student right join score on student.sid=score.student_id where course_id in(select cid from course where cname in('物理','生物')) group by student.sid having count(course_id)=2;
【mysql】排序方操作50题练习及其答案的更多相关文章
- Mysql综合练习作业50题
#作业库create database db8 charset utf8; #年级表create table class_grade(gid int not null primary key auto ...
- Java基础语法面试题50题整理(带答案)
嗯,之前某些原因整理了这份面试题,加油生活 (: 0,嗯,先做简单的,那个不对() 1,int [] sy = {0,9,2,3}; 2,int [] sy1 = new int[4]; 3,in ...
- MySQL练习50题
介绍一个学习SQL的网站:https://sqlbolt.com/ 习题来源于网络,SQL语句是自己的练习答案,部分参考了网络上的答案. 花了一晚上的时间做完,个人认为其中的难点有:分组提取前几名的数 ...
- sql语句练习50题(Mysql版-详加注释)
表名和字段 1.学生表 Student(s_id,s_name,s_birth,s_sex) --学生编号,学生姓名, 出生年月,学生性别 2.课程表 Course(c_id, ...
- 数据库 MySQL 之 数据操作
数据库 MySQL 之 数据操作 一.MySQL数据类型介绍 MySQL支持多种类型,大致可以分为四类:数值.字符串类型.日期/时间和其他类型. ①二进制类型 bit[(M)] 二进制位(101001 ...
- MySQL之表操作
1 创建表 2 查看表结构 3 数据类型 4 表完整性约束 5 修改表 6 复制表 7 删除表 一创建表 语法: create table 表名( 字段名1 类型[(宽度) 约束条件], 字段名2 ...
- Mysql学习---基础操作学习2
基本数据类型 Mysql基本数据类型:二进制,数值[整数,小数].字符串[定长,变长]. 二进制数据.时间和枚举集合 bit[(M)] 二进制位(101001),m表示二进制位的长度(1-64),默认 ...
- mysql 四 表操作
表介绍 表相当于文件,表中的一条记录就相当于文件的一行内容,不同的是,表中的一条记录有对应的标题,称为表的字段 id,name,qq,age称为字段,其余的,一行内容称为一条记录 本节重点: 1 创建 ...
- 转:sql 经典50题--可能是你见过的最全解析
题记:从知乎上看到的一篇文章,刚好最近工作中发现遇到的题目与这个几乎一样,可能就是从这里来的吧.^_^ 里面的答案没有细看,SQL求解重在思路,很多时候同一种结果可能有多种写法,比如题中的各科成绩取前 ...
随机推荐
- .net core 配置swagger遇到的坑
Swagger能成为最受欢迎的REST APIs文档生成工具之一,有以下几个原因: Swagger 可以生成一个具有互动性的API控制台,开发者可以用来快速学习和尝试API. Swagger 可以生成 ...
- 使用WebViewJavascriptBridge与UIWebView交互
使用WebViewJavascriptBridge与UIWebView交互 https://github.com/marcuswestin/WebViewJavascriptBridge 核心的地方: ...
- Proxyee-down的下载与安装教程
源代码在:GitHub_proxyee-down 为了节约读者的时间,我把需要的资源文件打包好,百度云链接在下面 Proxyee-down最新版为2.54(2018.8.9更新) 最新版下载地址:链接 ...
- Linux系统重要的开机自启动的服务
重要的开机自启动的服务 1.sshd 连接Linux服务器是需要用到的服务程序 2.rsyslog 操作日志的一种机制 系统日志:/var/log/message ...
- 获取 docker 容器(container)的 ip 地址
获取单个IP docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-ID> 获取所有容器IP doc ...
- 第 6 章 C控制语句:循环
6.16.3 使用嵌套循环,按下面格式打印字母: F FE FED FEDC FEDCB FEDCBA #include <stdio.h> int main() { ; ); row ! ...
- September 19th 2017 Week 38th Tuesday
Live boldly. Push yourself. Don't settle. 勇敢生活,突破自我,永不设限! Don't indulge in the past, whether it was ...
- gamit安装
需要准备的文件: 默认已安装好虚拟机和Ubuntu系统 1.输入用户名密码,进入Ubuntu10.04桌面.按下“Ctrl+Alt+T”,进入终端: 2.在终端输入“sudo gedit /etc/a ...
- HDFS下载数据机制的底层分析
HDFS下载数据机制的底层分析 Hadoop中的RPC(Remote Procedure Call)框架 hadoop中结点间的通信采用的是RPC. RPC框架的实现机制图解: 从hdfs下载数据的源 ...
- 遇见C++ Lambda
转自:https://www.cnblogs.com/allenlooplee/archive/2012/07/03/2574119.html 遇见C++ Lambda Written by Alle ...