之前发现自己写sql不怎么得心应手,总是百度零零散散的学习一下,所以在空闲的时候自己就专门找一下mysql的强化题敲一下练习一下,简要记录一下,sql写着写着就会越来越熟练,总之要自己加油!

表结构

学生表:Student(SNO,Sname,Sage,Sex)
课程表:Course(CNO,Cname,TNO)
成绩表:SC(SNO,CNO,score)
教师表:Teacher(TNO,Tname)

建表语句

create table student(
id int primary key auto_increment comment '主键id',
sno int not null comment '学号',
sname varchar(100) comment '姓名',
age int comment '年龄',
sex int not null comment '性别0男1女'
)comment= '学生表' create table course(
id int primary key auto_increment comment '课程表主键id',
cno int not null comment '课程号',
cname varchar(100) comment '课程名',
tno int not null comment '教师编号'
)comment = '课程表' create table sc(
id int primary key auto_increment comment '成绩表主键id',
sno int not null comment '学号',
cno int not null comment '课程号',
score int not null comment '分数'
)comment = '成绩表' create table teacher(
id int primary key auto_increment comment '教师表主键id',
tno int not null comment '教师编号',
tname varchar(100) comment '教师姓名'
)comment = '教师表'
#1.查询“3001”课程比“3002”课程成绩高的所有学生的学号
select a.sno from
(select sno,score from sc where cno=3001)a,
(select sno,score from sc where cno=3002)b
where a.score>b.score and a.sno=b.sno #2.查询平均成绩大于60分的同学的学号和平均成绩
select
sno,
AVG(score) avg_score
from sc
group by sno
having avg_score>60 #3.查询所有同学的学号、姓名、选课数、总成绩
select
student.sno,
student.sname,
count(sc.cno),
sum(sc.score)
from student
left join sc on student.sno = sc.sno
group by student.sno #4.查询姓“李”的教师数
select count(distinct(teacher.tname)) num
from teacher where teacher.tname like '李%' #5.查询没有学过“李老师”课程的同学的学号、姓名
select
student.sno,
student.sname
from student
where student.sno not in
(
select sc.sno from sc
inner join course on sc.cno = course.cno
inner join teacher on teacher.tno = course.tno
where teacher.tname = '李老师'
) #6.查询学过“王老师”所有课程的学生的学号、姓名
select student.sno,student.sname
from student where student.sno in
(
select sc.sno
from sc
inner join course on sc.cno = course.cno
inner join teacher on course.tno = teacher.tno
where teacher.tname = '王老师'
group by sc.sno
having count(sc.sno)=(
select COUNT(course.cno) from teacher
inner join course on teacher.tno = course.tno
where teacher.tname = '王老师'
)
) #7.查询学过“3001”也学过“3004”课程的学生的学号、姓名
select student.sno,student.sname
from student
inner join sc on student.sno = sc.sno
where cno = 3001
and student.sno in (
select sc.sno from sc where sc.cno=3004
) select student.sno,student.sname
from student
inner join sc a on student.sno = a.sno
where cno = 3001
and EXISTS
(
select b.sno from sc b where a.sno = b.sno and b.cno=3004
) #8.查询所有课程成绩小于60的同学的学号、姓名
select stu.sno,stu.sname
from student stu
where stu.sno not in(
select sc.sno from sc where sc.score >= 60 group by sc.sno
) #9.查询没有学全所有课的同学的学号、姓名
select student.sno,student.sname
from student
inner join sc on student.sno = sc.sno
group by sc.sno having count(*)<
(
select count(cno) from course
) #10.查询至少有一门课与学号为“1001”同学所学相同的学生的学号和姓名
select student.sno,student.sname
from student
inner join sc on student.sno = sc.sno
where sc.sno != 1001
and sc.cno in(
select sc.cno from sc where sc.sno = 1001
)
group by student.sno #11.查询和“1001”号的同学学习的课程完全相同的其他同学学号和姓名
select sc.sno
from sc where sc.sno != 1002 and cno in
(select cno from sc where sc.sno = 1002)
group by sno having count(*) =
(select count(*) from sc where sno = 1002) #12.删除学习“李老师”课的sc表记录
delete sc
from sc
inner join course on course.cno = sc.cno
inner join teacher on teacher.tno = course.tno
where teacher.tname = '李老师' #13.向sc表中插入一些记录,这些记录要求符合以下条件;没有上过编号“3004”课程的同学学号、3001号课程的平均成绩
insert into sc(sno,cno,score)
select sno,3001,(SELECT AVG(score) from sc where cno = 3001)
from student where sno not in
(
select sno from sc where cno = 3003
) #14.按平均成绩从高到低显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示:学生id,语文,数学,英语,有效课程数,有效平均分
select
s.sno as '学生id',
(select s1.score from sc s1 where s1.sno = s.sno and s1.cno = 3001) as '语文',
(select s2.score from sc s2 where s2.sno = s.sno and s2.cno = 3002) as '数学',
(select s3.score from sc s3 where s3.sno = s.sno and s3.cno = 3003) as '英语',
count(cno) as '有效课程数',
AVG(score) as '有效平均分'
from sc s group by s.sno order by '有效平均分' #15.查询各科成绩最高和最低的分:以如下的形式显示,课程id,最高分,最低分
select
s.cno as '课程id',
(select max(score) from sc s1 where s1.cno = s.cno) as '最高分',
(select min(score) from sc s2 where s2.cno = s.cno) as '最低分'
from sc s group by s.cno #16.按各科平均成绩从低到高和及格率的百分数从高到低顺序
select
s.cno as '课程id',
course.cname as '课程名',
AVG(s.score) as avgScore,
(select count(*) from sc s1 where s.cno = s1.cno and s1.score>60)/count(sno)*100 as rate
from sc s
inner join course on course.cno = s.cno
group by s.cno
order by avgScore asc,rate desc #17.查询“3001”课程成绩在第2名到3名的学生的各科成绩
select
student.sname,course.cname,sc.score
from course
inner join sc on course.cno = sc.cno
and course.cno = 3001
inner join student on student.sno = sc.sno
order by sc.score desc
limit 1,2 #18.查询出只选修两门课程的全部学生的学号和姓名
select student.sno,student.sname
from student
inner join sc on student.sno = sc.sno
group by student.sno having count(student.sno)=2

MYSQL语句强化练习的更多相关文章

  1. 如何根据执行计划,判断Mysql语句是否走索引

    如何根据执行计划,判断Mysql语句是否走索引

  2. 让dede运行php代码和mysql语句

    一.dede运行php代码 举例1: {dede:name runphp='yes'} $str = "hello ";@me = $str;@me .= "world& ...

  3. php代码优化,mysql语句优化,面试需要用到的

    首先说个问题,就是这些所谓的优化其实代码标准化的建议,其实真算不上什么正真意义上的优化,还有一点需要指出的为了一丁点的性能优化,甚至在代码上的在一次请求上性能提升万分之一的所谓就去大面积改变代码习惯, ...

  4. mysql语句:批量更新多条记录的不同值[转]

    mysql语句:批量更新多条记录的不同值 mysql更新语句很简单,更新一条数据的某个字段,一般这样写: 帮助 1 UPDATE mytable SET myfield = 'value' WHERE ...

  5. Thinkphp用exp表达式执行mysql语句,查询某字段不为空is not null,自动增值

    Thinkphp用exp表达式执行mysql语句,查询某字段不为空is not null,自动增值 Thinkphp 的文档经常不够完整的表达MYSQL的各种组合,is not null在thinkp ...

  6. MySQL语句进行分组后的含有字段拼接方法

    MySQL语句: SELECT GROUP_CONCAT(DISTINCT transaction_no) FROM `lm_wh_trans` GROUP BY staff_code; 如果tran ...

  7. shell脚本循环执行mysql语句

    参考资料:Shell脚本中执行mysql语句 需求:数据库里有张数据表存储的是用户对电影的评价(user_id movie_id rating time),但是我现在要每部电影的总评分. 解决方法: ...

  8. 【PHP基础】常用mySQL语句以及WampServer2.2设置数据库默认编码

    一.WampServer2.2设置数据库默认编码(此部分转自http://www.cnsecer.com/5984.html) wamp下MySQL的默认编码是Latin1,不支持中文,要支持中文的话 ...

  9. mysql语句中把string类型字段转datetime类型

    mysql语句中把string类型字段转datetime类型   在mysql里面利用str_to_date()把字符串转换为日期   此处以表h_hotelcontext的Start_time和En ...

随机推荐

  1. 微信小程序把玩(十二)text组件

    原文:微信小程序把玩(十二)text组件 通常文本设置要不在wxml中设置,再要不就是通过weml绑定在js中设置文字. wxml <view > <text>我是文本组件&l ...

  2. 用Delphi实现文件下载的几种方法(三种使用控件的方法)

    有个API就是UrlDownloadToFile.不仅如此,Delphi的一些控件也可以轻松实现下载,如NMHTTP,指定NMHTTP1.InputFileMode := ture; 指定Body为本 ...

  3. 社会不是承认有学历的人, 而是承认努力过得人, 而且是真正努力过不是穷忙的人(没有学历就要多付出一倍的努力)good

    送你一句 这就是你水平差的理由? 楼主你工资低是因为你技术不行, 不想努力然后怪罪学历, 为什么学历高的混得好, 因为学历高的人努力过, 你没学历技术还不行, 凭什么证明你努力过, 社会不是承认有学历 ...

  4. 使用VS2010再装VS2013不用再烦恼不兼容

    某些同事有时在开发过程中出现这么个问题,在使用js直接异步调用类库时,弹出错误类库不存在或者没有定义等,类似问题,这个时候可能你正在绞尽脑汁的去解决问题,明明问题不大,为什么安装VS2013后就不能打 ...

  5. Qt动画效果的幕后英雄:QTimeLine

    其实动画的本质就是在每一定时间间隔内显示一帧图像,当这个间隔较短的时候人眼就感觉不出来了,觉得看到的是连续的影像.Qt为开发动画效果的人员提供了一个很好的时间控制类QTimeLine. QTimeLi ...

  6. asp.net mvc下实现微信公众号(JsApi)支付介绍

    本文主要讲解asp.net mvc框架下公众号支付如何实现,公众号支付主要包括三个核心代码,前台调起支付js代码.对应js调用参数参数生成代码.支付成功处理代码. 一.微信支付方式介绍 微信提供了各种 ...

  7. 解决Nextcloud 无法删除目录

    1)进入维护模式 sudo -u www php /www/wwwroot/192.168.40.159/occ maintenance:mode --on 2)使用mysql命令行工具,在nextc ...

  8. vue 左右滑动效果

    个人实际开发中用到的效果问题总结出来便于自己以后开发查看调用,如果也适用其他人请随意拿走勿喷就行! vue.js是现在流行的js框架之一,vue 是一套用于构建用户界面的渐进式javascript框架 ...

  9. 程序代写, CS代写, 代码代写, CS编程代写, java代写, python代写, c++/c代写, R代写, 算法代写, web代写

    互联网一线工程师程序代写 微信联系 当天完成 查看大牛简介特色: 学霸代写,按时交付,保证原创,7*24在线服务,可加急.用心代写/辅导/帮助客户CS作业. 客户反馈与评价 服务质量:保证honor ...

  10. Spring Boot2(四):使用Spring Boot多数据源实现读写分离

    前言 实际业务场景中,不可能只有一个库,所以就有了分库分表,多数据源的出现.实现了读写分离,主库负责增改删,从库负责查询.这篇文章将实现Spring Boot如何实现多数据源,动态数据源切换,读写分离 ...