mysql之练习题4
准备表:
create table class(cid int primary key auto_increment,
caption char(5) not null unique); INSERT into class(caption)values('三年二班'),('一年三班'),('三年一班'); CREATE table student(sid int primary key auto_increment,
sname char(6) not null,
gender enum('男','女','male','female') not null,
class_id int(4) not null,
foreign key(class_id) references class(cid)
on delete CASCADE
on update cascade); insert into student(sname,gender,class_id)values
('钢蛋','女',1),('铁锤','女',1),('山炮','男',2); create table teacher(tid int primary key auto_increment,
tname char(6) not null); insert into teacher(tname)values('波多'),('苍空'),('饭岛'); create table course(cid int primary key auto_increment,
cname CHAR(5) not null unique,
teacher_id int not null,
foreign key(teacher_id) references teacher(tid)
on delete CASCADE
on update cascade); insert into course(cname,teacher_id)values('生物',1),('体育',1),('物理',2); create table score(sid int primary key auto_increment,
student_id int not null,
foreign key(student_id) references student(sid)
on delete cascade on update cascade,
course_id int not null,
foreign key(course_id) references course(cid)
on delete cascade on update cascade,
number int(4) not null); insert into score(student_id,course_id,number)values(1,1,60),(1,2,59),(2,2,100); SELECT * from class;
show CREATE table class;
select * from student;
show create table student;
SELECT * from teacher;
show create table teacher;
select * from course;
show create table course;
select * from score;
show create table score;
开始练习:
1、查询所有的课程的名称以及对应的任课老师姓名
SELECT cname,tname from course inner join teacher ON course.teacher_id = teacher.tid; 2、查询学生表中男女生各有多少人
select gender,COUNT(sid) from student GROUP BY gender; 3、查询物理成绩等于100的学生的姓名
SELECT sname from student where sid in (
SELECT student_id from score where course_id = (SELECT cid from course where cname = '物理') and num = 100
); 4、查询平均成绩大于八十分的同学的姓名和平均成绩 方法1:
SELECT student.sname,t1.avg_num from student inner join
(SELECT student_id,AVG(num) avg_num from score GROUP BY student_id
HAVING avg(num) > 80) as t1
on student.sid = t1.student_id; 方法2:
select * from student where sid in (
select student_id from score group by student_id
having avg(num)>80
); 5、查询所有学生的学号,姓名,选课数,总成绩
SELECT student.sid,student.sname,t1.course_num,t1.total_num from student inner JOIN
(SELECT
student_id,
count(course_id) course_num,
sum(num) total_num
FROM
score
GROUP BY
student_id) as t1
on student.sid = t1.student_id; 6、 查询姓李老师的个数
方法1:
SELECT COUNT(1) from teacher where tname like '李%'; 方法2:
select count(t1) from (
select tname t1 from teacher where tname LIKE '李%'
)as t 7、 查询没有报李平老师课的学生姓名
SELECT
sname
FROM
student
WHERE
sid NOT IN (
SELECT
student_id
FROM
score
WHERE
course_id IN (
SELECT
cid
FROM
course
WHERE
teacher_id = (
SELECT
tid
FROM
teacher
WHERE
tname = '李平老师'
)
)
); 8、 查询物理课程比生物课程高的学生的学号
SELECT t1.student_id from
(SELECT student_id,num from score where course_id = (
SELECT cid from course where cname = '物理'
)) as t1
inner join
(SELECT student_id,num from score where course_id = (
SELECT cid from course where cname = '生物'
)) as t2
on t1.student_id = t2.student_id
where t1.num > t2.num; 9、 查询没有同时选修物理课程和体育课程的学生姓名
方法1:
SELECT sname from student where sid in (
SELECT student_id from score LEFT JOIN course
on score.course_id = course.cid
WHERE course.cname in ('物理','体育')
GROUP BY student_id
HAVING count(sid) < 2
); 方法2:
select sname from student where sid not in (
SELECT s1.student_id from (
select student_id from score where course_id =(
SELECT cid from course where cname ='体育')) s1
INNER JOIN (
select student_id from score where course_id =(
SELECT cid from course where cname ='物理')) s2
on s1.student_id=s2.student_id); 10、查询挂科超过两门(包括两门)的学生姓名和班级
方法1::
SELECT sname,caption from student LEFT JOIN class
on student.class_id = class.cid
where student.sid in (
SELECT student_id from score where num < 60 GROUP BY student_id
HAVING COUNT(course_id) >= 2
)
; 方法2:
select s.sname,class.caption from class INNER JOIN
(select * from student where sid in (
select student_id from score GROUP BY student_id
having student_id>=2)) s
on s.class_id=class.cid; 11 、查询选修了所有课程的学生姓名
select sname from student where sid in (
select student_id from score GROUP BY student_id
having count(sid)=(
select count(cid) from course)) 12、查询李平老师教的课程的所有成绩记录
方法1:
SELECT * from score where course_id in (
SELECT cid from course inner JOIN teacher
on course.teacher_id = teacher.tid
WHERE tname = '李平老师'
); 方法2:
select num from score WHERE course_id in (
select cid from course where teacher_id=(
select tid from teacher where tname='李平老师')); 13、查询全部学生都选修了的课程号和课程名
SELECT ss.s1,ss.s2,course.cid,course.cname from
(select student.sid s1,student.sname s2,score.course_id s3
from student INNER JOIN score
on student.sid=score.student_id ) ss
INNER JOIN course
on ss.s3=course.cid; 14、查询每门课程被选修的次数
方法1:
SELECT course.cname,t1.count_student FROM course
INNER JOIN
(
SELECT course_id,count(student_id) count_student from score GROUP BY course_id
) as t1
ON course.cid = t1.course_id; 方法2:
select course.cname,COUNT(score.sid)
from course INNER JOIN score
on course.cid=score.course_id
group by score.course_id; 15、查询只选修了一门课程的学生姓名和学号
select sid,sname from student where sid in(
select student_id from score GROUP BY student_id
having count(sid)=1); 16、查询所有学生考出的总成绩并按从高到低排序(成绩去重)
方法1:
SELECT DISTINCT sum(num) sum_num from score group by student_id
ORDER BY sum_num desc; 方法2:
select student.sname,avg(score.num) avg_num from
student INNER JOIN score on student.sid=score.student_id
GROUP BY student_id ORDER BY avg_num desc; 17、查询平均成绩大于85的学生姓名和平均成绩
方法1:
SELECT student.sname,t1.avg_num from student inner join
(
SELECT student_id,avg(num) avg_num from score GROUP BY student_id having avg(num) > 85
) as t1
on student.sid = t1.student_id; 方法2:
select student.sname,avg(score.num) from student INNER JOIN score
on student.sid=score.student_id
GROUP BY score.student_id
having avg(score.num)>85; 18、查询生物成绩不及格的学生姓名和对应生物分数
方法1:
SELECT sname,t1.num from student
INNER JOIN
(
SELECT student_id,num from score LEFT JOIN course
on score.course_id = course.cid
where course.cname = '生物' and score.num < 60
) as t1
on student.sid = t1.student_id; 方法2:
select student.sname,ss.num from student INNER JOIN(
select * from score where course_id=(
select cid from course where cname='生物') and num<60) ss
on ss.student_id=student.class_id; 19、查询在所有选修了李平老师课程的学生中,这些课程(李平老师的课程,不是所有课程)平均成绩最高的学生姓名
select sname from student where sid in(
select student_id from score where course_id in(
select cid from course where teacher_id=(
select tid from teacher where tname='李平老师'))
GROUP BY student_id
HAVING avg(num)=(
select avg(num) from score where course_id in(
select cid from course where teacher_id=(
select tid from teacher where tname='李平老师'))
GROUP BY student_id order by avg(num) desc
limit 1)) 20、查询每门课程成绩最好的前两名学生姓名 SELECT * from score ORDER BY course_id,num desc; #取得课程编号与第一高的成绩:course_id,first_num
SELECT course_id,max(num) first_num from score GROUP BY course_id; #取得课程编号与第二高的成绩:course_id,second_num
SELECT score.course_id,max(num) second_num from score LEFT JOIN (
SELECT course_id,max(num) first_num from score GROUP BY course_id ) as t1
on score.course_id = t1.course_id
where score.num < t1.first_num
GROUP BY score.course_id
; #链表得到一张新表,新表包含课程编号与这门课程前两名的成绩分数 select t1.course_id,t1.first_num,t2.second_num from (SELECT course_id,max(num) first_num from score GROUP BY course_id) as t1 inner join (SELECT score.course_id,max(num) second_num from score LEFT JOIN (
SELECT course_id,max(num) first_num from score GROUP BY course_id ) as t1
on score.course_id = t1.course_id
where score.num < t1.first_num
GROUP BY score.course_id) as t2 on t1.course_id = t2.course_id; #取前两名学生的编号 SELECT score.course_id,score.student_id from score LEFT JOIN (
select t1.course_id,t1.first_num,t2.second_num from (SELECT course_id,max(num) first_num from score GROUP BY course_id) as t1 inner join (SELECT score.course_id,max(num) second_num from score LEFT JOIN (
SELECT course_id,max(num) first_num from score GROUP BY course_id ) as t1
on score.course_id = t1.course_id
where score.num < t1.first_num
GROUP BY score.course_id) as t2 on t1.course_id = t2.course_id ) as t3 on score.course_id = t3.course_id
where score.num >= t3.second_num and score.num <= t3.first_num
; SELECT t4.course_id,student.sname from student inner join
(
SELECT score.course_id,score.student_id from score LEFT JOIN (
select t1.course_id,t1.first_num,t2.second_num from (SELECT course_id,max(num) first_num from score GROUP BY course_id) as t1 inner join (SELECT score.course_id,max(num) second_num from score LEFT JOIN (
SELECT course_id,max(num) first_num from score GROUP BY course_id ) as t1
on score.course_id = t1.course_id
where score.num < t1.first_num
GROUP BY score.course_id) as t2 on t1.course_id = t2.course_id ) as t3 on score.course_id = t3.course_id
where score.num >= t3.second_num and score.num <= t3.first_num
) as t4
on student.sid = t4.student_id
ORDER BY t4.course_id
; select student.sname,t.course_id,t.num from student INNER JOIN
(
select
s1.student_id,s1.course_id,s1.num,
(select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 0,1) as first_num,
(select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 1,1) as second_num
from
score as s1
) as t
on student.sid = t.student_id
where t.num in (t.first_num,t.second_num)
ORDER BY t.course_id
; SELECT sid from score as s1 ;
mysql之练习题4的更多相关文章
- 【Python全栈-后端开发】MySQL数据库-练习题
MySQL数据库-练习题 一.表关系 请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 3.查询平均成绩大于60分的同学的学号 ...
- MYSQL经典练习题,熟悉DQL
MYSQL经典练习题 (本练习题可让你熟悉DQL,快速的上手DQL) 首先,先在数据库中建立基本数据库以及表项: DROP DATABASE IF EXISTS `test`; CREATE DATA ...
- MySQL经典练习题
表名和字段 –1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_id,c_name,t_id ...
- day41 mysql 学习 练习题 重要*****
MySQL 练习题[二1.表如下: 收获和注意点:***** #1 GROUP by 可以放到where s_id in ()条件局后边 GROUP BY s_id having 详见题12 #2 做 ...
- mysql 及练习题
if() 函数的用法 IF(expr1,expr2,expr3),如果expr1的值为true,则返回expr2的值,如果expr1的值为false, mysql,'女','男') as sex fr ...
- MySQL--python关联MySQL、练习题
1.python关联MySQL pymysql: 安装:pip3 install pymysql 1.0:连接到数据库中 import pymysql conn = pymysql.connect( ...
- MySQL经典练习题及答案,常用SQL语句练习50题
表名和字段 –1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_id,c_name,t_id ...
- 四、MYSQL数据练习题
我的MYSQL版本是mysql-5.7.24-winx64,每天练习5道习题. 如果有错误或者更优的解决方法,欢迎大家指出,谢谢!! 一.测试表格 --1.学生表Student(Sid,Sname,S ...
- mysql基础练习题
一.表关系 请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 /* Navicat MySQL Data Transfer Source Server : mysql5.7.1 Sour ...
- 一道很好的mysql面试练习题,having综合应用
写一条SQL语句,求出2门以及2门以上不及格的科目平均分 >要出现2门以及2门以上的学科不及格 >计算该考生所有学科的平均分,不单是,不及格的那几门 #创建表: create table ...
随机推荐
- Python+Selenium学习--访问连接
场景 web UI测试里最简单也是最基本的事情就是访问1个链接了. 在python的webdrive中,访问url时应该使用get方法. 代码 #!/usr/bin/env python # -*- ...
- mysql中插入序列表
利用序列表来实现列转行:CREATE TABLE sequence(id INT UNSIGNED NOT NULL auto_increment PRIMARY KEY)往里面插入数据 INSERT ...
- 解决vue-router嵌套路由(子路由)在history模式下刷新无法渲染页面的问题
一. 异常描述: 本来使用的是vue-router的hash模式,但是hash模式下url需要带“#”符号,不仅看起来不舒服,而且有些场景下是会破坏路由中的"#"(微信分享页面就会 ...
- C++ map中使用erase应该注意到的问题
注意:此程序在win环境下会出现上述描述的问题:在mac环境下第一种方式是正常运行的.Map.erase有3个重载函数: void erase(iterator position); size_typ ...
- ubuntu下安装redis及常用操作
reids是一个内存数据库,具有存取速度快,使用简单等优点.我们常常在分布式或者负载均衡的系统中使用它来缓存一些公用的且不是很大量的数据,比如session或者各类token(比如微信的access_ ...
- html5新添加的表单类型和属性
email类型: <input type="email"> url类型: <input type="url"> date类型: < ...
- 任意格式视频转MP4格式
下载ffmpeg解压,提取ffmpeg.exe 在mmfpeg.exe目录下新建批处理,内容如下 @echo off title 正在转换,mp4转换完成自动关闭 ffmpeg -i %1 -y -q ...
- 9.29 h5日记
1.CSS中哪些属性可以继承? font系列 text系列 color line-height 2.border-radius的值 值的顺序是左上 右上 右下 左下 则 border-radius:5 ...
- 2.git使用之git fetch和git push的区别
. git fetch:相当于是从远程获取最新版本到本地,不会自动merge git fetch origin master git log -p master..origin/master git ...
- RoCE vs iWARP
两种以太网 RDMA 协议: iWARP 和 RoCE 转载 2017年03月08日 16:10:09 1510 http://weibo.com/p/1001603936363903889917?m ...