练习:导入hellodb.sql生成数据库

(1) 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄;

MariaDB [hellodb]> select Name,Age from students where Age>25 and Gender='M';

(2) 以ClassID为分组依据,显示每组的平均年龄;

MariaDB [hellodb]> select ClassID,avg(age) as avg_age from students group by ClassID;

(3) 显示第2题中平均年龄大于30的分组及平均年龄;

MariaDB [hellodb]> select ClassID,avg(age) as avg_age from students group by ClassID having avg_age>30;

(4) 显示以L开头的名字的同学的信息;

MariaDB [hellodb]> select * from students where Name like 'L%';

MariaDB [hellodb]> select * from students where Name rlike '^L';

(5) 显示TeacherID非空的同学的相关信息;

MariaDB [hellodb]> select * from students where TeacherID is NOT NULL;

(6) 以年龄排序后,显示年龄最大的前10位同学的信息;

MariaDB [hellodb]> select * from students order by Age desc limit 10;

(7) 查询年龄大于等于20岁,小于等于25岁的同学的信息;用三种方法;

MariaDB [hellodb]> select * from students where age>=20 and age<=25;

MariaDB [hellodb]> select * from students where age between 20 and 25;

练习:导入hellodb.sql,以下操作在students表上执行

1、以ClassID分组,显示每班的同学的人数;

MariaDB [hellodb]> select classid,count(name) from students group by classid;

2、以Gender分组,显示其年龄之和;

MariaDB [hellodb]> select gender,sum(age) from students group by gender;

3、以ClassID分组,显示其平均年龄大于25的班级;

select classid from students group by classid having avg(age) > 25;

4、以Gender分组,显示各组中年龄大于25的学员的年龄之和;

MariaDB [hellodb]> select gender,sum(age) from (select age,gender from students where age>25) as t group by gender;

练习:导入hellodb.sql,完成以下题目:

1、显示前5位同学的姓名、课程及成绩;

MariaDB [hellodb]> select name,course,score from (select * from students limit 5) as t,courses,scores where t.stuid=scores.stuid and scores.courseid=courses.courseid;

2、显示其成绩高于80的同学的名称及课程;

select name,course,score from students,(select * from scores where score > 80) as s,courses where students.stuid=s.stuid and s.courseid=courses.courseid;

3、求前8位同学每位同学自己两门课的平均成绩,并按降序排列;

select name,a from (select * from students limit 8) as s,(select stuid,avg(score) as a from scores group by stuid) as c where s.stuid=c.stuid order by a desc;

4、显示每门课程课程名称及学习了这门课的同学的个数;

select course,count(name) from (select name,course from students,courses,scores where students.stuid=scores.stuid and scores.courseid=courses.courseid) as a group by course;

select courses.course,count(stuid) from scores left join courses on scores.courseid=courses.courseid group by scores.courseid;

思考:

1、如何显示其年龄大于平均年龄的同学的名字?

MariaDB [hellodb]> select name from students where age > (select avg(age) from students);

2、如何显示其学习的课程为第1、2,4或第7门课的同学的名字?

MariaDB [hellodb]> select name from students,(select distinct stuid from (select * from scores where courseid in (1,2,4,7)) as a) as b where b.stuid=students.stuid;

select students.name from students,(select distinct stuid from scores where courseid in (1,2,4,7))as s where s.stuid=students.stuid;

3、如何显示其成员数最少为3个的班级  的同学中年龄大于同班同学平均年龄的同学?

MariaDB [hellodb]> select student.name,student.age,student.classid,second.avg_age from (select students.name as name ,students.age as age,students.classid as classid from students left join (select count(name) as num,classid as classid from students group by classid having num>=3) as first on first.classid=students.classid) as student,(select avg(age) as avg_age,classid as classid from students group by classid) as  second where student.age>second.avg_age and student.classid=second.classid;

4、统计各班级中年龄大于全校同学平均年龄的同学。

select name,age,classid from students where age > (select avg(age) as a from students);

mysql分组查询学生平均年龄_mysql练习题相关推荐

  1. mysql分组查询学生平均年龄_MySQL学习-基础练习题

    day1 学生表操作: 1. 查询出班级205有多少个男生 2. 查询出名字为4个字的所有学生信息(编号.姓名,年龄,班级) 3. 查询出所有姓王的学生信息(编号.姓名,年龄,班级) 4. 查询出班级 ...

  2. mysql分组查询学生平均年龄_那些年我们一起做过的[分组查询]_MySQL

    分组查询 在SQL Server中使用的分组查询是ORDER BY子句,使用ORDER BY子句要同聚合函数配合使用才能完成分组查询,在SELECT查询的字段中如果字段没有使用聚合函数就必须出现在OR ...

  3. mysql分组查询学生平均年龄_8.21MySQL(四)基本查询语句及方法、连表、子查询...

    一.基本查询语句及方法 sql语句书写顺序 select id,name from emp where id > 3 and id < 6; sql语句执行顺序 from  # 确定到底是 ...

  4. mysql 连接查询两个条件_MySQL之多表查询一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习...

    一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 首先说一下,我们写项目一般都会建一个数据库,那数据库里面是不是存了好多张表啊,不可能把所有的数据都放到一张表里面,肯定要分表来存数据,这样节 ...

  5. MySQL分组查询跟聚合函数

    MySQL分组查询跟聚合函数 一.分组查询的语句 GROUP BY { <列名> | <表达式> | <位置> } [ASC | DESC] 这个语句中间{ < ...

  6. mysql分组查询和分组过滤

    mysql分组查询使用的的关键字是group by,分组过滤使用到的关键字是having.需要先分组查询才能进行分组过滤,个人理解having和where的区别有以下: 1.聚集函数count.sum ...

  7. SQL的老题目:查询学生平均成绩及其名次

    Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表 24 ...

  8. mysql 分组查询最新

    mysql分组查询最新 看到网上说到的方法和我写的都一样,也不知道有没有更好的方法,等到解答. SELECT id,group_id from (SELECT id,group_id from tab ...

  9. Mysql分组查询每组最新的一条数据(三种实现方法)

    MySQL分组查询每组最新的一条数据 前言 注意事项 准备SQL 错误查询 错误原因 方法一 方法二(适用于自增ID和创建时间排序一致) 方法三(适用于自增ID和创建时间排序一致) 总结 MAX()函 ...

最新文章

  1. linux centos rc.local 自启动无效 解决方法
  2. aws iot 连接时间_AWS IoT Core 定价
  3. java并发之线程池
  4. linux自带磁盘加密工具下载,TrueCrypt(磁盘加密工具)
  5. python常用的装饰器有哪些_python基本装饰器
  6. 2019最佳工作、平均年薪达百万!想入行,先看这10本书
  7. 风变Python8编程时,两大思维模式
  8. python入门基础2 if语句 while循环 for循环
  9. 微型计算机原理及应用论文,微机原理及应用结业论文
  10. UCINET使用经验分享
  11. The NMEA 0183 Protocol
  12. php动态字体,APP动态切换字体的实现
  13. 文档无法保存 读取文档时出现问题(135)
  14. 利用Python开发一个微信定时发送器
  15. XCODE性能测试方法
  16. OI生涯回忆录(Part7:至高一湖南集训Day3)
  17. jquery word export 导出html内容无样式或无图片问题
  18. 报表软件选型时应该知道的
  19. 机器学习-47-ML-03-Metric-based Approach Train+Test as RNN(元学习-support set和query set用于同一网络的方法)
  20. 福尔摩斯到某古堡探险

热门文章

  1. 解决“Cmake error :generator: Ninja“问题
  2. 论肱二头肌在日常生活中的锻炼的持久战|健身达人
  3. php实现多重继承,PHP5 多重继承DEMO方法
  4. Django视图层:视图函数、视图类
  5. c++ 构造函数数组_从 JS 数组操作到 V8 array.js
  6. html5 字母单词拖拽,HTML5拖拽
  7. [独家放送]Unity2019更新规划速览,将有官方的可视化编程!
  8. OpenShift 4 之 GitOps(2)用ArgoCD部署应用
  9. 边缘设备上的实时AI人员检测:在Raspberry Pi上启动SSD
  10. 结合Pandas中的多个数据集