Mysql 练习题 及 答案

Mysql 练习题 及 答案 –1.学生表Student(S,Sname,Sage,Ssex)–S学生编号,Sname学生姓名,Sage出生年月,Ssex学生性别–2.课程表 Course(C,Cname,T)–C–课程编号,Cname课程名称,T教师编号–3.教师表 Teacher(T,Tname)–T教师编号,Tname教师姓名–4.成绩表 SC(S,C,score)–S学生…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

 –1.学生表
Student(S,Sname,Sage,Ssex) –S 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
–2.课程表 
Course(C,Cname,T) –C —课程编号,Cname 课程名称,T 教师编号
–3.教师表 
Teacher(T,Tname) –T 教师编号,Tname 教师姓名
–4.成绩表 
SC(S,C,score) –S 学生编号,C 课程编号,score 分数
 

--创建测试数据
create table Student(
S varchar(10),
Sname varchar(10),
Sage datetime,
Ssex nvarchar(10)
) ;

insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');

create table SC(
S varchar(10),
C varchar(10),
score decimal(18,1)
);

insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
create table Course(
C varchar(10),
Cname varchar(10),
T varchar(10)
);

insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');

create table Teacher(
T varchar(10),
Tname varchar(10)
);

insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

–1、查询”01″课程比”02″课程成绩高的学生的信息及课程分数

SELECT a.*
,b.score AS '01分数'
,c.score AS '02分数'
FROM student a
INNER JOIN sc b
ON a.s=b.s AND b.c='01'
INNER JOIN sc c
ON a.s=c.s AND c.c='02'
WHERE b.score > c.score ;

–2、查询“01”课程比“02”课程成绩低的学生的信息及课程分数
–3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

SELECT a.s
,a.sname
,AVG(b.score) AS avgnum
FROM student a
INNER JOIN sc b
ON a.s=b.s
GROUP BY a.s,a.sname
HAVING AVG(b.score)>60 ;

–4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
–5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

–6、查询姓老师的数量 

SELECT COUNT(1) FROM teacherWHERE tname LIKE '李%' ;

–7、查询学过张三老师授课的同学的信息 

SELECT a.*
FROM student a
INNER JOIN sc b
ON a.s=b.s
INNER JOIN course c
ON b.c=c.c
INNER JOIN teacher d
ON c.t=d.t
WHERE d.tname='张三'
GROUP BY 1,2,3,4 ;

--方法2
SELECT a.*
FROM student a
LEFT JOIN (
       SELECT a.*
       FROM student a
       INNER JOIN sc b
       ON a.s=b.s
       INNER JOIN course c
       ON b.c=c.c
       INNER JOIN teacher d
       ON c.t=d.t
       WHERE d.tname='张三'
       GROUP BY 1,2,3,4
)b
ON a.s=b.s
WHERE b.s IS NOT NULL ;

–8、查询没学过张三老师授课的同学的信息 

SELECT a.*
FROM student a
LEFT JOIN sc b
ON a.s=b.s
WHERE NOT EXISTS(
              SELECT *
              FROM course aa
              INNER JOIN teacher b
              ON aa.t=b.t
              INNER JOIN sc c
              ON aa.c=c.c
              WHERE b.tname='张三'
              AND c.s=a.s
       )
GROUP BY 1,2,3,4 ;

–9、查询学过编号为“01”并且也学过编号为“02”的课程的同学的信息

SELECT a.*
FROM student a
INNER JOIN sc b
ON a.s=b.s AND b.c='01'
INNER JOIN sc c
ON a.s=c.s AND c.c='02' ;

–10、查询学过编号为“01”但是没有学过编号为“02”的课程的同学的信息

select *
from student a
left join sc b
on a.s=b.s and b.c='01'
left join sc c
on a.s=c.s and c.c='02'
where b.c='01' and c.c is null ;

–11、查询没有学全所有课程的同学的信息 

SELECT a.*
FROM student a
LEFT JOIN sc b
ON a.s=b.s
LEFT JOIN (SELECT COUNT(1) anumFROM course) c
ON 1=1
GROUP BY 1,2,3,4
HAVING MAX(c.anum)>COUNT(b.c) ;

–12、查询至少有一门课与学号为“01”的同学所学相同的同学的信息 

SELECT a.*
FROM student a
INNER JOIN sc b
ON a.s=b.s
WHERE EXISTS(
    SELECT 1 FROM sc WHERE s='01' AND c=b.c
)
GROUP BY 1,2,3,4 ;

–13、查询和“01”号的同学学习的课程完全相同的其他同学的信息 

SELECT a.s,a.sname,a.sage,a.ssex
FROM (SELECT a.*,COUNT(b.c) ASsnum
       FROM student a
       INNER JOIN sc b
       ON a.s=b.s
       WHERE EXISTS(
           SELECT 1 FROM scWHERE s='01' AND c=b.c
       )

       GROUP BY 1,2,3,4)a
INNER JOIN (SELECTa.*,COUNT(b.c) AS anum
       FROM student a
       INNER JOIN sc b
       ON a.s=b.s
       GROUP BY 1,2,3,4)b
ON a.s=b.s
INNER JOIN (SELECT COUNT(1) ASnum1 FROM sc WHERE s='01')c
ON 1=1
WHERE a.snum=b.anum ANDa.snum=c.num1 ;

--方法二

SELECT a.*
       ,COUNT(b.c) AS anum
       ,SUM(CASE WHEN EXISTS(SELECT 1 FROM sc WHERE s='01' AND c=b.c)THEN 1 ELSE 0 END) AS snum
       ,MAX(c.num1) AS num1
FROM student a
INNER JOIN sc b
ON a.s=b.s
INNER JOIN (SELECT COUNT(1) ASnum1 FROM sc WHERE s='01')c
ON 1=1
GROUP BY 1,2,3,4
HAVING anum=snum AND anum=num1 ;

–14、查询没学过张三老师讲授的任一门课程的学生姓名 

SELECT a.*
FROM student a
LEFT JOIN(
       SELECT a.s
       FROM student a
       LEFT JOIN sc b
       ON a.s=b.s
       LEFT JOIN course c
       ON b.c=c.c
       LEFT JOIN teacher d
       ON c.t=d.t AND d.tname='张三'
       WHERE d.t IS NOT NULL
       )b
ON a.s=b.s
WHERE b.s IS NULL ;

–15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 

SELECT a.s
       ,a.sname
       ,AVG(b.score)
FROM student a
INNER JOIN sc b
ON a.s=b.s
GROUP BY 1,2
HAVING SUM(CASE WHENb.score>=60 THEN 0 ELSE 1 END)>=2 ;

–16、检索“01”课程分数小于60,按分数降序排列的学生信息

SELECT a.*
       ,b.score
FROM student a
INNER JOIN sc b
ON a.s=b.s
WHERE
  b.c='01'
  AND b.score < 60
ORDER BY b.score DESC ;

–17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

SELECT a.*
       ,SUM(CASE WHEN b.c='01' THEN b.scoreELSE 0 END) AS s01
       ,SUM(CASE WHEN b.c='02' THEN b.scoreELSE 0 END) AS s02
       ,SUM(CASE WHEN b.c='03' THEN b.score ELSE0 END) AS s03
       ,AVG(CASE WHEN b.score IS NULL THEN 0 ELSE b.score END) avs
FROM student a
LEFT JOIN sc b
ON a.s=b.s
GROUP BY 1,2,3,4
ORDER BY avs DESC ;

–18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

SELECT a.c
       ,a.cname
       ,MAX(b.score)
       ,MIN(b.score)
       ,AVG(b.score)
       ,SUM(CASE WHEN b.score>=60 THEN 1 ELSE 0 END)/COUNT(1)
       ,SUM(CASE WHEN b.score>=70 AND b.score<80 THEN 1 ELSE 0 END)/COUNT(1)
       ,SUM(CASE WHEN b.score>=80 AND b.score<90 THEN 1 ELSE 0 END)/COUNT(1)
       ,SUM(CASE WHEN b.score>=90 THEN 1 ELSE 0 END)/COUNT(1)
FROM course a
INNER JOIN sc b
ON a.c=b.c
GROUP BY 1,2 ;

–19、按各科成绩进行排序,并显示排名

SET @rn:=0;
SELECT a.*,@rn:=@rn+1
FROM(
    SELECT a.*,b.score
    FROM course a
    INNER JOIN sc b
    ON a.c=b.c
    WHERE a.c='01'
    ORDER BY b.c,score DESC
)a ;

–20、查询学生的总成绩并进行排名

SELECT a.*
       ,COUNT(b.c)+1 asall
FROM sc a
LEFT JOIN sc b
ON a.c=b.c AND a.score < b.score
GROUP BY 1,2,3
ORDER BY a.c,asall ;

–21、查询不同老师所教不同课程平均分从高到低显示 

SELECT a.*
       ,b.cname
       ,AVG(c.score) ascore
FROM teacher a
INNER JOIN course b
ON a.t=b.t
INNER JOIN sc c
ON b.c=c.c
GROUP BY 1,2,3
ORDER BY ascore DESC ;

–22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩

SELECT a.*,b.cname,b.score
FROM student a
INNER JOIN (
       SELECT a.*
              ,c.cname
              ,COUNT(b.c)+1 AStp
       FROM sc a
       LEFT JOIN sc b
       ON a.c=b.c AND a.score<b.score
       LEFT JOIN course c
       ON a.c=c.c
       GROUP BY 1,2,3,4
       HAVING COUNT(b.c)+1 IN(2,3)
       ORDER BY a.c,tp)b
ON a.s=b.s ;

–23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比 

SELECT a.c
       ,a.cname
       ,SUM(CASE WHEN b.score<=100 AND b.score>85 THEN 1 ELSE 0 END) AS '[100-85]'
       ,SUM(CASE WHEN b.score<=85  AND b.score>70 THEN 1 ELSE 0 END) AS '[85-70]'
       ,SUM(CASE WHEN b.score<=70  AND b.score>60 THEN 1 ELSE 0 END) AS '[70-60]'
       ,SUM(CASE WHEN b.score<=60  AND b.score>0 THEN 1 ELSE 0 END) AS '[60-0]'   
       ,SUM(CASE WHEN b.score<=100 AND b.score>85 THEN 1 ELSE 0 END)/COUNT(1) AS '[100-85]%'
       ,SUM(CASE WHEN b.score<=85  AND b.score>70 THEN 1 ELSE 0 END)/COUNT(1) AS '[85-70]%'
       ,SUM(CASE WHEN b.score<=70  AND b.score>60 THEN 1 ELSE 0 END)/COUNT(1) AS '[70-60]%'
       ,SUM(CASE WHEN b.score<=60  AND b.score>0 THEN 1 ELSE 0 END)/COUNT(1) AS '[60-0]%'
FROM course a
INNER JOIN sc b
ON a.c=b.c
GROUP BY 1,2 ;

–24、查询学生平均成绩及其名次 

SELECT a.*
       ,COUNT(b.s)+1
FROM (
       SELECT a.*,AVG(CASE WHEN b.score IS NULL THEN 0 ELSE b.scoreEND) AS ascore
       FROM student a
       LEFT JOIN sc b
       ON a.s=b.s
       GROUP BY 1,2,3,4
      )a
LEFT JOIN(
       SELECT a.*,AVG(CASE WHEN b.score IS NULL THEN 0 ELSE b.scoreEND) AS ascore
       FROM student a
       LEFT JOIN sc b
       ON a.s=b.s
       GROUP BY 1,2,3,4
      )b
ON a.ascore<b.ascore
GROUP BY 1,2,3,4,5 ;

–25、查询各科成绩前三名的记录

SELECT a.*,COUNT(b.c)+1 ASascore
FROM sc a
LEFT JOIN sc b
ON a.c=b.c AND a.score<b.score
GROUP BY 1,2,3
HAVING ascore<=3
ORDER BY a.c,ascore ;

–26、查询每门课程被选修的学生数 

SELECT a.*
       ,COUNT(b.s)
FROM course a
LEFT JOIN sc b
ON a.c=b.c
GROUP BY 1,2,3 ;

–27、查询出只有两门课程的全部学生的学号和姓名  

SELECT a.*
       ,COUNT(b.c)
FROM student a
LEFT JOIN sc b
ON a.s=b.s
GROUP BY 1,2,3,4
HAVING COUNT(b.c)=2 ;

–28、查询男生、女生人数 
–29、查询名字中含有字的学生信息
–30、查询同名同性学生名单,并统计同名人数 

SELECT sname
       ,ssex
       ,COUNT(1)
FROM student
GROUP BY 1,2
HAVING COUNT(1) > 1 ;

–31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime) 

SELECT * FROM student WHERE YEAR(sage)=1990 ;

–32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号

SELECT a.*
       ,AVG(b.score) ascore
FROM course a
LEFT JOIN sc b
ON a.c=b.c
GROUP BY 1,2,3
ORDER BYascore DESC,a.c ;

–33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩 

SELECT a.*
       ,AVG(b.score) ascore
FROM student a
LEFT JOIN sc b
ON a.s=b.s
GROUP BY 1,2,3,4
HAVING ascore>=85 ;

–34、查询课程名称为数学,且分数低于60的学生姓名和分数 

SELECT c.*
FROM course a
LEFT JOIN sc b
ON a.c=b.c
LEFT JOIN student c
ON b.s=c.s
WHERE a.cname='数学'
AND b.score < 60 ;

–35、查询所有学生的课程及分数情况; 

SELECT *
FROM sc a
INNER JOIN student b
ON a.s=b.s
INNER JOIN course c
ON a.c=c.c ;

–36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数; 

SELECT b.sname
       ,c.cname
       ,a.score
FROM sc a
INNER JOIN student b
ON a.s=b.s
INNER JOIN course c
ON a.c=c.c
WHERE a.score > 70 ;

–37、查询不及格的课程
–38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名; 

SELECT a.*
FROM student a
INNER JOIN sc b
ON a.s=b.s
WHERE b.c='01' AND b.score > 80 ;

–39、求每门课程的学生人数 
–40、查询选修张三老师所授课程的学生中,成绩最高的学生信息及其成绩

SELECT a.*,b.score
FROM student a
INNER JOIN sc b
ON a.s=b.s
INNER JOIN(
       SELECT c.c
              ,MAX(c.score) ASmaxscore
       FROM teacher a
       INNER JOIN course b
       ON a.t=b.t
       INNER JOIN sc c
       ON b.c=c.c
       WHERE a.tname='张三'
       GROUP BY c)c
ON b.c=c.c AND b.score=c.maxscore ;

–41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 

SELECT a.s
       ,a.c
       ,a.score
FROM sc a
INNER JOIN (
       SELECT a.score
              ,b.s
              ,COUNT(1)
       FROM sc a
       INNER JOIN student b
       ON a.s=b.s
       GROUP BY a.score,b.s
       HAVING COUNT(1)>1
)b
ON a.s=b.s AND a.score=b.score ;

方法二

SELECT a.*
FROM sc a
LEFT JOIN sc b
ON a.s=b.s AND a.score=b.score AND a.c <> b.c
GROUP BY 1,2,3
HAVING COUNT(b.s) > 0 ;

–42、查询每门功成绩最好的前两名 
–43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列  

SELECT a.c
       ,COUNT(1) AS pnum
FROM sc a
GROUP BY 1
HAVING pnum > 5
ORDER BY pnum DESC,a.c ;

–44、检索至少选修两门课程的学生学号 
–45、查询选修了全部课程的学生信息 

–46、查询各学生的年龄

SELECT a.*,YEAR(CURDATE())-YEAR(a.sage)
FROM student a ;

–47、查询本周过生日的学生

SELECT a.*
FROM student a
WHERE CONCAT(MONTH(a.sage),DAY(a.sage))>=CONCAT(MONTH(SUBDATE(CURDATE(),WEEKDAY(CURDATE()))),DAY(SUBDATE(CURDATE(),WEEKDAY(CURDATE()))))
AND CONCAT(MONTH(a.sage),DAY(a.sage))<=CONCAT(MONTH(SUBDATE(CURDATE(),WEEKDAY(CURDATE())-6)),DAY(SUBDATE(CURDATE(),WEEKDAY(CURDATE())-6))) ;

–48、查询下周过生日的学生

–49、查询本月过生日的学生

SELECT a.* FROM student a WHERE MONTH(a.sage)=MONTH(CURDATE()) ;

–50、查询下月过生日的学生

SELECT a.*FROM student a WHERE MONTH(a.sage)=MONTH(CURDATE())+1 ;

 

 

有些没有答案的,是因为和上面的差不多,偷个懒。。。。 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/193219.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • datagrip 激活码最新(JetBrains全家桶)

    (datagrip 激活码最新)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.cn/100143.html…

  • python sqrt函数的使用

    python sqrt函数的使用摘自《python程序设计基础》蔡永铭主编工具:python3.764-bit官方链接:https://www.python.org/平台:Windows10sqrt函数的使用sqrt中文名:平方根在python中使用函数之前需要了解一下模块的含义。模块是一个包含所有定义的函数和变量的文件,模块可以被别的程序引入,以使用该模块中的函数等功能。因为sqrt函数在math模块中,所…

  • git拉取代码密码错误_idea提交git

    git拉取代码密码错误_idea提交gitgit提交代码1:一定要先pull,(在本地建立仓库)eclipse中点击file找到term中的pull,同步拉取远程代码,idea中tomcat旁边斜向下箭头,拉取,首次拉取要输入用户名密码,2:提交到本地仓库commit,并填写提交备注,方便查找,3:push推送远程分支,提交到git分支。常见的pull失败:冲突-多个人修改同一个文件,别人修改后自己也修改导致拉取失败,解决冲突…

    2022年10月21日
  • MVC三层架构各层含义[通俗易懂]

    MVC三层架构各层含义[通俗易懂]1.模拟架构图:2.Action/Service/DAO简介:Action是管理业务(Service)调度和管理跳转的。Service是管理具体的功能的。Action只负责管理,而Service负责实施。DAO只完成增删改查,虽然可以1-n,n-n,1-1关联,模糊、动态、子查询都可以。但是无论多么复杂的查询,dao只是封装增删改查。至于增删查改如何去实现一个功能,dao是不管…

  • 存储的基本概念谈

    公司数据库使用的是san存储,一般共享文件夹则使用的是cifs、nas;这二者的区别是什么?谈到存储,思绪的过程是存放文件》文件不在本地》文件要通过网络传输》传输后数据最终会写到磁盘上。这

    2021年12月25日
  • virsh命令详解「建议收藏」

    virsh命令详解「建议收藏」virsh虚拟机管理

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号