对于这类问题,首先是建表、导入数据,然后是写sql语句,难到不难,就是题目量有点大
create database  if not exists  school;
use school;
drop table course;一、建表并逐个导入数据
1.course
create table  if not exists  course(sub_no  string,sub_name  string,teacher_id  string
)
row format delimited fields terminated by '\t';load data  local  inpath '/opt/tmp/school/course.txt' into table  course;2.score
create table if not exists  score(stu_id string,sub_no  string,result  double
)
row format delimited fields terminated by '\t';
load data  local  inpath '/opt/tmp/school/score.txt' into table  score;3.student
create table if not exists student(stu_id string,stu_name string,birthday date,gender string
)
row format delimited fields terminated by '\t';load data  local  inpath '/opt/tmp/school/student.txt' into table  student;4.teacher
create  table if not exists teacher(teacher_id string,teacher_name string
)
row format delimited fields terminated by '\t';
load data  local  inpath '/opt/tmp/school/teacher.txt' into table  teacher;二、解决综合问题
//31、查询 1990 年出生的学生名单:
create  table  student1 asselect stu_name, substr(birthday,1,4)years from student;
select  stu_name from student1 where years='1990';//32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时, 按课程编号升序排列:
select  sub_no ,round(avg(result) ,2)avg_score from score
group by sub_no
order by avg_score desc,sub_no;select s.sub_no,c.sub_name,round(avg(result),2)avg_score from score as s
left join course c on s.sub_no = c.sub_no
group by s.sub_no,c.sub_name
order by avg_score desc,s.sub_no;//33、查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩:
select stu.stu_id,stu_name,avg(result) avg_score  from student as stu
left join  score s on s.stu_id=stu.stu_id
group by  stu.stu_id,stu_name
having avg_score>=85;//34、查询课程名称为"数学",且分数低于 60 的学生姓名和分数:
select stu.stu_name,s.result from score as s
join student as stu on stu.stu_id=s.stu_id
join course c on s.sub_no = c.sub_no
where c.sub_name="数学"
and s.result<60;//35、查询所有学生的课程及分数情况:
select s2.stu_name ,c.sub_name,s.result from score as s
left join  student s2 on s.stu_id = s2.stu_id
left join course c on s.sub_no = c.sub_no;//36、查询任何一门课程成绩在 70 分以上的学生姓名、课程名称和分数
select s2.stu_name,c.sub_name,s.result from score as s
left join course c on s.sub_no = c.sub_no
left join student s2 on s.stu_id = s2.stu_id
where s.result>70;//37、查询课程不及格的学生:
select s.stu_id,s2.stu_name,c.sub_name,s.result from score as s
left join student s2 on s.stu_id = s2.stu_id
left join course c on s.sub_no = c.sub_no
where s.result<60;select distinct s.stu_id, s2.stu_name from score as s
left join student s2 on s.stu_id = s2.stu_id
where s.result<60;//38、查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名:
select s.stu_id,s2.stu_name from score as s
left join student s2 on s.stu_id = s2.stu_id
where s.result>80
and  s.sub_no='01';//39、求每门课程的学生人数:
select c.sub_name ,count(stu_id) from score as s
left join course c on s.sub_no = c.sub_no
group by c.sub_name;//40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
select s.stu_id,s2.stu_name, result from score as s
left join student s2 on s.stu_id = s2.stu_id
left join course c on s.sub_no = c.sub_no
left join teacher t on c.teacher_id = t.teacher_idwhere t.teacher_name="张三"
order by result desc
limit 1;//41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select  s1.stu_id,s1.sub_no,s2.stu_id,s2.sub_no,s1.result,s2.result from score s1,score s2
where s1.sub_no != s2.sub_no  and  s1.result=s2.result;//42、查询每门课程成绩最好的前三名:
select  sub_no,result from (
select  sub_no,result,row_number() over (partition by sub_noorder by  result desc) as rn from score) as sc
where sc.rn <=3;//43、统计每门课程的学生选修人数(超过 5 人的课程才统计):
-- – 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同, 按课程号升序排列
select sub_no,count(stu_id)  cnt from score
group by  sub_no
having cnt>5
order by cnt desc,sub_no ;//44、检索至少选修两门课程的学生学号:
select stu_id ,count(sub_no) cou from score
group by stu_id
having cou>=2;//45、查询选修了全部课程的学生信息:
select  sc.stu_id,s.stu_name,s.birthday ,s.gender,count(sub_no) cou from score as sc
left join student s on sc.stu_id = s.stu_id
group by  sc.stu_id,s.stu_name, sc.stu_id, s.birthday, s.gender
having  cou=3;//46、查询各学生的年龄(周岁): – 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
with
t1 as (select st.stu_name student_name,date_format(st.birthday,'yyyy-MM-dd') birth,date_format(current_date(),'yyyy-MM-dd') now,date_format(current_date(),'yyyy')-date_format(st.birthday,'yyyy') age from student st
)
select student_name,if(month(now)=month(birth),if(day(now)<day(birth),age-1,age),if(month(now)<month(birth),age-1,age)) age
from t1;//47、查询本周过生日的学生:
with
t1 as (select st.stu_name student_name,date_format(st.birthday,'yyyy-MM-dd') birth,
date_format(current_date(),'yyyy-MM-dd') now,dayofweek(current_date()) week_day from student st
)
select student_name from t1 where month(birth)=month(now) and (day(birth)between day(date_sub(now,week_day-1)) and day(date_sub(now,week_day-7)));//48、查询下周过生日的学生:
with
t1 as (select st.stu_name student_name,date_format(st.birthday,'yyyy-MM-dd') birth,date_format(current_date(),'yyyy-MM-dd') now,dayofweek(current_date()) week_dayfrom student st
)
select student_name from t1 where month(birth)=month(now) and (day(birth)between day(date_sub(now,week_day-8)) and day(date_sub(now,week_day-14)));//49、查询本月过生日的学生:
select stu_name from student where month(current_date())=month(birthday);//50、查询 12 月份过生日的学生:
select stu_name from student where month(birthday)="12";

乐于奉献共享,帮助你我他!

31、查询 1990 年出生的学生名单相关推荐

  1. 31、查询1990年出生的学生名单(重点year)

    -- 31.查询1990年出生的学生名单(重点year) SELECT * FROM student WHERE YEAR(s_birth)='1990'

  2. 用python打印学生名单_Python 之 MySql 每日一练 231——查询1990年出生的学生名单...

    ** 一.表名和字段** –1. 学生表 student (s_id,s_name,s_birth,s_sex) –学生编号,学生姓名,出生年月,学生性别 –2. 课程表 course (c_id,c ...

  3. 查询1990年出生的学生名单

    建表语句点击详见 – 查询1990年出生的学生名单 SELECT st.* FROM student st WHERE st.s_birth LIKE "1990%";

  4. 【sql: 联系题 23 24】查询同名学生名单,并统计同名人数 找到同名的名字并统计个数,查询 1990 年出生的学生名单...

    题目23:查询同名学生名单,并统计同名人数 找到同名的名字并统计个数 一开始这个sql 写不出来,看了答案后好简单,也更加加深了我多count 的用法 SELECT stdentname,COUNT( ...

  5. -- 31、查询1990年出生的学生名单

    SELECT st.* FROM student st WHERE st.s_birth LIKE "1990%";

  6. mysql统计没有参加考试的学生名单_sQL SERVER,帮我编写一个存储过程,查询没有参加考试的学生名单,要求显示姓名、学号,具体请补充:...

    --学生表CREATETABLEmember(midchar(10)primarykey,mnamechar(50)notnull,);--课程表CREATETABLEcourse(fidchar(1 ...

  7. 查询1997年出生的学生mysql_Mysql学习三

    /*(1)查询全部学生的学号.姓名.性别和年龄*/ select student_id, student_name, student_sex, 2016 - year(student_birthday ...

  8. oracle怎么查询同名学生,数据库:SQL“查询同名同姓学生名单,并统计同名人数”延伸...

    /*30.查询同名同姓学生名单,并统计同名人数*/ Select Sname,count(*) as NUM from Student group by Sname having count(*)&g ...

  9. 查询同名同性学生名单,并统计同名人数

    建表语句点击详见 – 查询同名同性学生名单,并统计同名人数 SELECT st.*, COUNT(1) FROM student st GROUP BY st.s_name,st.s_sex HAVI ...

最新文章

  1. MindSpore 高阶优化器
  2. Sharepoint学习笔记 –架构系列—Sharepoint的客户端对象模型(Client Object Model)
  3. maven的环境搭建
  4. 区块链技术指南笔记(一):区块链基本概念
  5. 使用Markdown
  6. 淮阴工学院计算机学院机房,实验室开放
  7. java保存登录信息_java – 保存登录详细信息(首选项)android
  8. java难度_你们觉得java难吗?
  9. 帮助打造无障碍APP Google将自动化测试GTXiLib
  10. Matlab 用sort函数排序 二维数组
  11. cisco port-channel配置
  12. 计算机丢失deferrd.dll怎么解决,被Defer后怎么办?如何在RD调整策略绝地反击?!...
  13. 蓝桥杯-ds18b20使用(小白专享)
  14. 在java中重写方法应遵循规则的包括_蘑菇街2017校园招聘笔试题
  15. 台式机使用笔记本电脑上网解决办法。
  16. Cisco nat inside接口,outside接口,nvi接口的区别
  17. 鼠标悬停显示滚动条,移出不显示
  18. 组建一个最简单的局域网
  19. MS2109/HDMI转USB2.0高清视频采集
  20. 使用Hyper-V集群和存储功能以达到HA

热门文章

  1. java---File类笔记(简单介绍)
  2. 2021年全球化妆品阴离子表面活性剂行业调研及趋势分析报告
  3. 让子弹再飞一会:游戏中关于碰撞体积的趣闻
  4. vite使用vite-aliases插件配置路径别名
  5. 使用ScriptableObject代替部分配置表的坑点
  6. 医疗机构如何成功实施CRM?如下几点是关键因素
  7. python定时开关机的代码_python实现Windows电脑定时关机
  8. 华为确定发布鸿蒙的时间了吗,Mate40系列首发,华为鸿蒙OS手机版发布时间确定...
  9. VC++ 中主线程等待子线程结束的方法
  10. Testin发布众测平台 助开发者发现质量缺陷建立质量体系