mysql 查询练习


1.3 基础练习
1.查询男生、女生的人数;

select count(case when gender='男' then 1 end) '男',count(case when gender='女' then 1 end) '女' from student ;

2.查询姓“张”的学生名单;

select sid,gender,sname from student where sname LIKE '张%';

3.课程平均分从高到低显示

select student_id,sname,avg(num) from score join
student on score.student_id =student.sid
group by student_id DESC;

4.查询有课程成绩小于60分的同学的学号、姓名;

select  b.sid ,num ,sname from score a LEFT JOIN student b ON a.student_id=b.sid where num<60 order by num desc;

5.查询至少有一门课与学号为1的同学所学课程相同的同学的学号和姓名;

select distinct student_id,sname from score join student on score.student_id=student.sid where course_id in (select course_id
FROM score
where student_id=1);

6.查询出只选修了一门课程的全部学生的学号和姓名;

select student_id,num,b.sname from
score a LEFT JOIN student b on a.student_id =b.sidgroup by student_id having count(num)=1;

7.查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;

select cname '科目', min(num) '最低',max(num) '最高'
from score a join course b on a.course_id=b.cid group by course_id ;

8.查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;

select  a.student_id, c.sname,a.num from score a join (select * from score where course_id=2) b on a.student_id=b.student_id
join student c on a.student_id=c.sid
where a.course_id=1 AND a.num>b.num;

9.查询“生物”课程比“物理”课程成绩高的所有学生的学号;

select a.student_id from score a join (select * from score where course_id=2) b
on a.student_id=b.student_id
where a.course_id=1 and a.num>b.num;

10.查询平均成绩大于60分的同学的学号和平均成绩;

select student_id ,avg(num) from score group by student_id having avg(num)>60 ORDER BY avg(num)

11.查询所有同学的学号、姓名、选课数、总成绩;

select  student_id '学号',sname '姓名',count(course_id)'选课数',sum(num)'总成绩' from score join student on score.student_id=student.sid group by student_id;

12.查询姓“李”的老师的个数;

select count(tname) from teacher where tname like '李%'

13.查询没学过“张磊老师”课的同学的学号、姓名;

select student_id,sname from
score join student on score.student_id=student.sid where student_id not in
(select student_id from score join teacher on score.course_id=teacher.tid where tname='张磊老师')

14.查询学过“1”并且也学过编号“2”课程的同学的学号、姓名;

select a.student_id,c.sname from score a  join
(select * from score where course_id=2 GROUP BY student_id) b
on a.student_id= b.student_id join student c on a.student_id=c.sid where a.course_id=1

15.查询学过“李平老师”所教的所有课的同学的学号、姓名;

select student_id,sname from score join student onscore.student_id=student.sid where course_id in(select tid from teacher where tid=2)

1.4 进阶练习
1.查询没有学全所有课的同学的学号、姓名;

select student_id,sname from score
join student on score.student_id=student.sidgroup by student_id having count(student_id)!=4

2.查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;

select * from score where course_idin(select course_id from score where student_id=2)
group by student_id
having count(student_id)=3 and student_id !=2

3.删除学习“李平”老师课的SC表记录;

delete from score where course_id=2;

5.按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;

select student_id,
sum(case when course_id=1 then num end) '生物',
sum(case when course_id=2 then num end) '物理',
sum(case when course_id=3 then num end) '体育',
sum(case when course_id=4 then num end) '美术',
count(course_id) '有效课数',
round(avg(num),2) '有效平均分'from score group by student_id

6.查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;

select course_id ,max(num),min(num)from score group by course_id;

7.按各科平均成绩从低到高和及格率的百分数从高到低顺序;

select course_id, round(avg(num),2) as pj,CONCAT((round((SUM(case when num>60 then 1 end)/count(student_id))*100,2)),'%') as bfb from score group by course_id
order by pj asc,bfb desc

8.查询各科成绩前三名的记录:(不考虑成绩并列情况)

(select student_id,course_id ,num from score where course_id=1 order by num desc limit 3)
union
(select student_id,course_id ,num from score where course_id=2 order by  num desc limit 3)
union
(select student_id,course_id ,num from score where course_id=3 order by  num desc limit 3)
UNION
(select student_id,course_id ,num from score where course_id=4 order by  num desc limit 3);

9.查询每门课程被选修的学生数;

select case when course_id=1 then '生物'
when course_id=2 then '物理'
when course_id=3 then '体育'
when course_id=4 then '美术'
end  as '科目'
, count(student_id)  from score group by course_id

10.查询同名同姓学生名单,并统计同名人数;

select sname,count(*) from student GROUP BY sname HAVING count(*)>1

11.查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;

select course_id,avg(num) from score GROUP BY course_id order by avg(num) asc,course_id DESC

12.查询平均成绩大于85的所有学生的学号. 姓名和平均成绩;

select student_id,sname,round(avg(num),2) from score join student on score.student_id=student.sid group by student_id having avg(num)>85

13.查询课程名称为“生物”,且分数低于60的学生姓名和分数;

select sname,num from score join student on score.student_id=student.sid where course_id=1 and num<60

14.查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;

select student_id,sname from score join student on score.student_id=student.sid where course_id=3 AND num>80

15.求选了课程的学生人数

select count(distinct student_id) as '学生人数' from score

16.查询选修“杨艳”老师所授课程的学生中,成绩最高的学生姓名及其成绩;

select  course_id,sname, max(num)
from score a join student b on a.student_id=b.sid
GROUP BY a.student_id HAVING a.course_id=3

17.查询各个课程及相应的选修人数;

select count(*) from score GROUP BY course_id

18.查询不同课程但成绩相同的学生的学号、课程号、学生成绩;

select student_id,course_id,count(num) from score GROUP BY student_id,course_id having count(num)>1

19.查询每门课程成绩最好的前两名;

(select * from score where course_id=1 ORDER BY num desc LIMIT 2)
union
(select * from score where course_id=2 ORDER BY num desc LIMIT 2)
union
(select * from score where course_id=3 ORDER BY num desc LIMIT  2)
union
(select * from score where course_id=4  ORDER BY num desc LIMIT 2)

20.检索至少选修两门课程的学生学号;

select student_id,count(course_id) from score group by student_id having count(*)>1

21.查询全部学生都选修的课程的课程号和课程名;

select course_id,cname from score join course on score.course_id=course.cid group by course_id having count(course_id)>0

22.查询没学过“朱云海老师”老师讲授的任一门课程的学生姓名;

select sname from score join student on score.student_id=student.sid
GROUP BY student_id
having  student_id not in (select student_id from score where course_id=4)

23.查询两门以上不及格课程的同学的学号及其平均成绩;

select student_id,sname,sum(case when num<60 then 1 else 0 end) num6 from score a
join   student b on a.student_id=b.sidgroup by student_id having num6>=2

24.检索“004”课程分数小于60,按分数降序排列的同学学号;

select * from score  where course_id=4 and num<60 order by num desc;

25.删除“002”同学的“001”课程的成绩;

delete from score where student_id=2 AND course_id=1;

mysql 查询练习相关推荐

  1. mysql查询字段大小写结果相同,mysql大小写查询不敏感,mysql5.7查询不区分大小写解决方案。

    下面有两条sql,主键查询,在mysql中查询到的结果相同. SELECT* FROM USER WHEREid = 'EM58hdK4nXC';SELECT* FROM USER WHEREid = ...

  2. MySQL查询进阶之多表查询

    一.多表查询 1.引出 2.笛卡尔积 3. 笛卡尔积的解决方法 二.多表查询分类 1.等值连接和非等值连接 2.自连接和非自连接 3.内连接和外连接 SQL92:使用(+)创建连接 SQL99语法实现 ...

  3. smarty mysql_Smarty处理mysql查询数组

    Smarty处理mysql查询数组 MySQL的查询结果一般是一个数组,而不是所有结果集.因此我们需要将结果全部存到数组中进行处理,然后就可以很轻松的再Smarty中使用了. PHP Mysql 代码 ...

  4. MYSQL 查询数据排序数据和分组数据

    在mysql查询过程中,可以对数据进行过滤,也可以对数据进行排序,可以对数据分组,下面分别讲述排序数据和分组数据例子.1,数据的排序 使用 ORDER BYselect * from where id ...

  5. mysql查询解析过程_MySQL查询执行过程详解

    查询是用户通过设置某些查询条件,从表或其他查询中选取全部或者部分数据,以表的形式显示数据供用户浏览.查询是一个独立的.功能强大的.具有计算功能和条件检索功能的数据库对象.MySQL数据库中,MySQL ...

  6. mysql 查询某个日期时间段,每天同一时间段的数据

    mysql 查询某个日期时间段,每天同一时间段的数据: SELECT * FROM t_a01_eltable WHERE DATE_FORMAT(acqtime,'%Y-%m-%d')>='2 ...

  7. php 多条查询结果插入新表,Mysql应用MySQL查询结果复制到新表的方法(更新、插入)...

    <Mysql应用MySQL查询结果复制到新表的方法(更新.插入)>要点: 本文介绍了Mysql应用MySQL查询结果复制到新表的方法(更新.插入),希望对您有用.如果有疑问,可以联系我们. ...

  8. MySQL查询区分大小写

    2019独角兽企业重金招聘Python工程师标准>>> 问题描述: 找出用户名id为'AAMkADExM2M5NjQ2LWUzYzctNDFkMC1h'的用户的数据: select ...

  9. 100G内存下,MySQL查询200G大表会OOM么?

    文章来源:https://sourl.cn/vwDNzn 我的主机内存只有100G,现在要全表扫描一个200G大表,会不会把DB主机的内存用光? 逻辑备份时,可不就是做整库扫描吗?若这样就会把内存吃光 ...

  10. mysql 查询用户最后登陆时间_弄懂mysql:mysql的通信协议

    我准备从mysql的实现出发,将mysql好好理解一下,从他的逻辑结构一层一层出发,感受一下,所以再学第一层之前,要先对mysql整体的逻辑结构有一个初步认识 mysql逻辑架构 整体来说,MySql ...

最新文章

  1. AI一分钟 | 英伟达发布最强核弹—无人车AI芯片DRIVE Xavier;百度硅谷首次开放无人车试乘:上车前要先签免责书
  2. dex2oat 加载多次
  3. Algorithm:C+语言实现之数组相关算法(和为定值的两个数、和为定值的m个数、荷兰国旗、长度为2n的洗牌算法、任意长度数组的洗牌算法)
  4. 成功解决explicit device specific/device:GPU:0' because no supported kernel for GPU devices is available.
  5. 【已解决】R语言添加行、列,转置操作
  6. mysql 存储过程中limit
  7. C#中MSMQ消息队列测试疑问
  8. 《Python核心编程》第二版第18页第一章练习 -Python核心编程答案-自己做的-
  9. main,tmain,winmain()等函数——UNICODE - sensensen - 博客园
  10. 多目标跟踪数据集 :mot16、mot17数据集介绍
  11. 第 8 章 crontab
  12. 拓扑图是用什么软件画的?
  13. eplan2022启动报错存储空间不够或者提示没有settings权限
  14. Alexa查询名词解释
  15. python交通流预测算法_(5)基于深度学习的短时道路交通流预测
  16. 2020年安卓各大应用市场份额占比分析
  17. 4.1_[Java 输入输出]-全网最全 Java 控制台输入输出
  18. 数字0~9的手写识别
  19. 傲气雄鹰android 3dm,傲气雄鹰 重载
  20. LMK04828时钟芯片配置——配置理解

热门文章

  1. 远程桌面报错:出现身份验证错误/要求的函数不受支持
  2. “个性化视频推荐”算法的Storm实现方案
  3. Conda executable is not found
  4. Centos配置IP地址
  5. 通过i2c控制摄像机马达升降
  6. ssm框架搭建详细步骤
  7. redis的三种启动方式(后台运行)
  8. 客观赋权法——CRITIC权重法【Python实现】
  9. Linux命令:c++filt
  10. 基于UDP的可靠性传输协议-KCP简介