MySQL必学!!!(练习题50道精选)

打印 上一主题 下一主题

主题 1041|帖子 1041|积分 3123

一、 数据表介绍

数据表介绍
–1.学生表
  1. Student(SId,Sname,Sage,Ssex)
  2. --SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
复制代码
–2.课程表
  1. Course(CId,Cname,TId)
  2. --CId 课程编号,Cname 课程名称,TId 教师编号
复制代码
–3.教师表
  1. Teacher(TId,Tname)
  2. --TId 教师编号,Tname 教师姓名
复制代码
–4.成绩表
  1. --SId 学生编号,CId 课程编号,score 分数
复制代码
二、搭建情况

学生表 Student

  1. create table Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));
  2. insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
  3. insert into Student values('02' , '钱电' , '1990-12-21' , '男');
  4. insert into Student values('03' , '孙风' , '1990-12-20' , '男');
  5. insert into Student values('04' , '李云' , '1990-12-06' , '男');
  6. insert into Student values('05' , '周梅' , '1991-12-01' , '女');
  7. insert into Student values('06' , '吴兰' , '1992-01-01' , '女');
  8. insert into Student values('07' , '郑竹' , '1989-01-01' , '女');
  9. insert into Student values('09' , '张三' , '2017-12-20' , '女');
  10. insert into Student values('10' , '李四' , '2017-12-25' , '女');
  11. insert into Student values('11' , '李四' , '2012-06-06' , '女');
  12. insert into Student values('12' , '赵六' , '2013-06-13' , '女');
  13. insert into Student values('13' , '孙七' , '2014-06-01' , '女');
复制代码
科目表 Course

  1. create table Course(CId varchar(10),Cname nvarchar(10),TId varchar(10));
  2. insert into Course values('01' , '语文' , '02');
  3. insert into Course values('02' , '数学' , '01');
  4. insert into Course values('03' , '英语' , '03');
复制代码
教师表 Teacher

  1. create table Teacher(TId varchar(10),Tname varchar(10));
  2. insert into Teacher values('01' , '张三');
  3. insert into Teacher values('02' , '李四');
  4. insert into Teacher values('03' , '王五');
复制代码
成绩表 SC

  1. create table SC(SId varchar(10),CId varchar(10),score decimal(18,1));
  2. insert into SC values('01' , '01' , 80);
  3. insert into SC values('01' , '02' , 90);
  4. insert into SC values('01' , '03' , 99);
  5. insert into SC values('02' , '01' , 70);
  6. insert into SC values('02' , '02' , 60);
  7. insert into SC values('02' , '03' , 80);
  8. insert into SC values('03' , '01' , 80);
  9. insert into SC values('03' , '02' , 80);
  10. insert into SC values('03' , '03' , 80);
  11. insert into SC values('04' , '01' , 50);
  12. insert into SC values('04' , '02' , 30);
  13. insert into SC values('04' , '03' , 20);
  14. insert into SC values('05' , '01' , 76);
  15. insert into SC values('05' , '02' , 87);
  16. insert into SC values('06' , '01' , 31);
  17. insert into SC values('06' , '03' , 34);
  18. insert into SC values('07' , '02' , 89);
  19. insert into SC values('07' , '03' , 98);
复制代码
三、练习题


  • 查询" 01 “课程比” 02 "课程成绩高的学生的信息及课程分数
  1. select * from  (
  2.     ->     select t1.SId, class1, class2
  3.     ->     from
  4.     ->         (SELECT SId, score as class1 FROM SC WHERE SC.CId = '01') AS t1,
  5.     ->         (SELECT SId, score as class2 FROM SC WHERE SC.CId = '02') AS t2
  6.     ->     where t1.SId = t2.SId and t1.class1 > t2.class2
  7.     -> ) r
  8.     -> LEFT JOIN Student
  9.     -> ON Student.SId = r.SId;
复制代码

1.1 查询同时存在" 01 “课程和” 02 "课程的情况
  1. select * from      
  2. (select * from SC where SC.CId = '01') as t1,     
  3. (select * from SC where SC.CId = '02') as t2
  4. where t1.SId = t2.SId;
复制代码

1.2 查询存在" 01 “课程但可能不存在” 02 "课程的情况(不存在时表现为 null )
  1. select * from
  2.     -> (select * from SC where SC.CId = '01') as t1
  3.     -> left join
  4.     -> (select * from SC where SC.CId = '02') as t2
  5.     -> on t1.SId = t2.SId;
复制代码

1.3 查询不存在" 01 “课程但存在” 02 "课程的情况
  1. > select * from SC
  2.     -> where SC.SId not in (
  3.     ->     select SId from SC
  4.     ->     where SC.CId = '01'
  5.     -> )
  6.     -> AND SC.CId= '02';
复制代码


  • 查询平均成绩大于等于 60 分的同砚的学生编号和学生姓名和平均成绩
  1. > select Student.SId, Student.Sname, r.ss from Student right join(
  2.     ->       select SId, AVG(score) AS ss from SC
  3.     ->       GROUP BY SId
  4.     ->       HAVING AVG(score)> 60
  5.     -> )r on Student.SId = r.SId;
复制代码


  • 查询在 SC 表存在成绩的学生信息
  1. > select DISTINCT Student.*
  2.     -> from Student,SC
  3.     -> where Student.SId=SC.SId
  4.     -> ;
复制代码


  • 查询全部同砚的学生编号、学生姓名、选课总数、全部课程的总成绩(没成绩的表现为 null )
  1. > select Student.sid, Student.sname,r.coursenumber,r.scoresum
  2.     -> from Student,
  3.     -> (select SC.sid, sum(SC.score) as scoresum, count(SC.cid) as coursenumber from SC
  4.     -> group by SC.sid)r
  5.     -> where Student.sid = r.sid;
复制代码

4.1查有成绩的学生信息
  1. > select * from Student
  2.     -> where Student.sid in (select SC.sid from SC);
复制代码


  • 查询「李」姓老师的数目
  1. > select count(*)
  2.     -> from Teacher
  3.     -> where tname like '李%';
复制代码


  • 查询学过「张三」老师授课的同砚的信息
  1. > select Student.* from Student,Teacher,Course,SC
  2.     -> where
  3.     ->     Student.sid = SC.sid
  4.     ->     and Course.cid=SC.cid
  5.     ->     and Course.tid =Teacher.tid
  6.     ->     and tname = '张三';
复制代码


  • 查询没有学全全部课程的同砚的信息
  1. > select * from Student
  2.     -> where Student.sid not in (
  3.     ->   select SC.sid from SC
  4.     ->   group by SC.sid
  5.     ->   having count(SC.cid)= (select count(cid) from Course)
  6.     -> );
复制代码


  • 查询至少有一门课与学号为" 01 "的同砚所学雷同的同砚的信
  1. > select * from Student
  2.     -> where Student.sid in (
  3.     ->     select SC.sid from SC
  4.     ->     where SC.cid in(
  5.     ->         select SC.cid from SC
  6.     ->         where SC.sid = '01'
  7.     ->     )
  8.     -> );
复制代码


  • 查询和" 01 "号的同砚学习的课程 完全雷同的其他同砚的信息
  1. > SELECT a.SId, a.Sname, a.Sage, a.Ssex
  2.     -> FROM Student a, SC b
  3.     -> WHERE a.SId = b.SId AND b.CId IN (
  4.     ->     SELECT CId
  5.     ->     FROM SC
  6.     ->     WHERE SId = '01'
  7.     -> ) AND a.SId <> '01';
复制代码


  • 查询没学过"张三"老师讲授的任一门课程的学生姓名
  1. > select * from Student
  2.     ->      where Student.sid not in(
  3.     ->         select SC.sid from SC,Course,Teacher
  4.     ->         where
  5.     ->             SC.cid = Course.cid
  6.     ->              and Course.tid = Teacher.tid
  7.     ->             and Teacher.tname= "张三");
复制代码


  • 查询两门及其以上不合格课程的同砚的学号,姓名及其平均成绩
  1. select Student.SId, Student.Sname,b.avg
  2.     -> from Student RIGHT JOIN
  3.     -> (select sid, AVG(score) as avg from SC
  4.     ->     where sid in (
  5.     ->               select sid from SC
  6.     ->               where score<60
  7.     ->               GROUP BY sid
  8.     ->               HAVING count(score)>1)
  9.     ->     GROUP BY sid) b on Student.sid=b.sid;
复制代码


  • 检索" 01 "课程分数小于 60,按分数降序排列的学生信息
  1. > select Student.*, SC.score from Student, SC
  2.     -> where Student.sid = SC.sid
  3.     -> and SC.score < 60
  4.     -> and cid = "01"
  5.     -> ORDER BY SC.score DESC;
复制代码


  • 按平均成绩从高到低表现全部学生的全部课程的成绩以及平均成绩
  1. > select *  from SC
  2.     -> left join (
  3.     ->     select sid,avg(score) as avscore from SC
  4.     ->     group by sid
  5.     ->     )r
  6.     -> on SC.sid = r.sid
  7.     -> order by avscore desc;
复制代码


  • 查询各科成绩最高分、最低分和平均分:
    以如下情势表现:课程 ID,课程 name,最高分,最低分,平均分,合格率,中等率,优良率,良好率
    合格为>=60,中等为:70-80,优良为:80-90,良好为:>=90
    要求输出课程号和选修人数,查询结果按人数降序排列,若人数雷同,按课程号升序排列
  • 按各科成绩举行排序,并表现排名, Score 重复时保存名次空缺
  1. > select
  2.     -> SC.CId ,
  3.     -> max(SC.score)as 最高分,
  4.     -> min(SC.score)as 最低分,
  5.     -> AVG(SC.score)as 平均分,
  6.     -> count(*)as 选修人数,
  7.     -> sum(case when SC.score>=60 then 1 else 0 end )/count(*)as 及格率,
  8.     -> sum(case when SC.score>=70 and SC.score<80 then 1 else 0 end )/count(*)as        中等率,
  9.     -> sum(case when SC.score>=80 and SC.score<90 then 1 else 0 end )/count(*)as        优良率,
  10.     -> sum(case when SC.score>=90 then 1 else 0 end )/count(*)as 优秀率
  11.     -> from SC
  12.     -> GROUP BY SC.CId
  13.     -> ORDER BY count(*)DESC, SC.CId ASC
  14.     -> ;
复制代码


  • 查询学生的总成绩,并举行排名,总分重复时保存名次空缺
  1. [/code] [list=1]
  2. [*] 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
  3. [*] 查询各科成绩前三名的纪录
  4. [/list] [code]> select * from SC
  5.     -> where (
  6.     -> select count(*) from SC as a
  7.     -> where SC.cid = a.cid and SC.score<a.score
  8.     -> )< 3
  9.     -> order by cid asc, SC.score desc;
复制代码


  • 查询每门课程被选修的学生数
  1. > select cid, count(sid) from SC
  2.     -> group by cid;
复制代码


  • 查询出只选修两门课程的学生学号和姓名
  1. > select Student.sid, Student.sname from Student
  2.     -> where Student.sid in
  3.     -> (select SC.sid from SC
  4.     -> group by SC.sid
  5.     -> having count(SC.cid)=2
  6.     -> );
复制代码


  • 查询男生、女生人数
  1. > select ssex, count(*) from Student
  2.     -> group by ssex;
复制代码


  • 查询名字中含有「风」字的学生信息
  1. > select *
  2.     -> from Student
  3.     -> where Student.Sname like '%风%'
  4.     -> ;
复制代码


  • 查询同名同性学生名单,并统计同名人数
  1. > select sname, count(*) from Student
  2.     -> group by sname
  3.     -> having count(*)>1;
复制代码


  • 查询 1990 年出生的学生名单
  1. > select *
  2.     -> from Student
  3.     -> where YEAR(Student.Sage)=1990;
复制代码

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

  • 查询平均成绩大于等于 85 的全部学生的学号、姓名和平均成绩
  • 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
  1. > select Student.sname, SC.score from Student, SC, Course
  2.     -> where Student.sid = SC.sid
  3.     -> and Course.cid = SC.cid
  4.     -> and Course.cname = "数学"
  5.     -> and SC.score < 60;
复制代码


  • 查询全部学生的课程及分数情况(存在学生没成绩,没选课的情况)
  1. select Student.sname, cid, score from Student
  2. left join SC
  3. on Student.sid = SC.sid;
复制代码


  • 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
  1. > select Student.sname, Course.cname,SC.score from Student,Course,SC
  2.     -> where SC.score>70
  3.     -> and Student.sid = SC.sid
  4.     -> and SC.cid = Course.cid;
复制代码


  • 查询不合格的课程
  1. > select DISTINCT SC.CId
  2.     -> from SC
  3.     -> where SC.score <60;
复制代码


  • 查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
  1. > select Student.sid,Student.sname
  2.     -> from Student,SC
  3.     -> where cid="01"
  4.     -> and score>=80
  5.     -> and Student.sid = SC.sid;
复制代码


  • 求每门课程的学生人数
  1. > select SC.CId,count(*) as 学生人数
  2.     -> from SC
  3.     -> GROUP BY SC.CId;
复制代码


  • 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
  1. l> select Student.*, SC.score, SC.cid from Student, Teacher, Course,SC
  2.     -> where Teacher.tid = Course.tid
  3.     -> and SC.sid = Student.sid
  4.     -> and SC.cid = Course.cid
  5.     -> and Teacher.tname = "张三"
  6.     -> order by score desc
  7.     -> limit 1;
复制代码


  • 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
  1. > select Student.*, SC.score, SC.cid from Student, Teacher, Course,SC
  2.     -> where Teacher.tid = Course.tid
  3.     -> and SC.sid = Student.sid
  4.     -> and SC.cid = Course.cid
  5.     -> and Teacher.tname = "张三"
  6.     -> and SC.score = (
  7.     ->     select Max(SC.score)
  8.     ->     from SC,Student, Teacher, Course
  9.     ->     where Teacher.tid = Course.tid
  10.     ->     and SC.sid = Student.sid
  11.     ->     and SC.cid = Course.cid
  12.     ->     and Teacher.tname = "张三"
  13.     -> );
复制代码


  • 查询差别课程成绩雷同的学生的学生编号、课程编号、学生成绩
  • 查询每门功成绩最好的前两名
  1. [/code] [list=1]
  2. [*]统计每门课程的学生选修人数(超过 5 人的课程才统计)。
  3. [/list] [code]> select SC.cid, count(sid) as cc from SC
  4.     -> group by cid
  5.     -> having cc >5;
复制代码


  • 检索至少选修两门课程的学生学号
  1. > select sid, count(cid) as cc from SC
  2.     -> group by sid
  3.     -> having cc>=2;
复制代码


  • 查询选修了全部课程的学生信息
  1. [/code] [list=1]
  2. [*]查询各学生的年岁,只按年份来算
  3. [/list] [code]SELECT s.SId, s.SName
  4. FROM Student s
  5. INNER JOIN SC sc ON s.SId = sc.SId
  6. WHERE sc.Score > 80 AND sc.CId != '01';
复制代码


  • 按照出生日期来算,当前月日 < 出生年月的月日则,年岁减一
  1. > select Student.SId as 学生编号,Student.Sname  as  学生姓名,
  2.     -> TIMESTAMPDIFF(YEAR,Student.Sage,CURDATE()) as 学生年龄
  3.     -> from Student
  4.     -> ;
复制代码


  • 查询本周过生日的学生
  1. select *
  2. from Student
  3. where WEEKOFYEAR(Student.Sage)=WEEKOFYEAR(CURDATE());
复制代码


  • 查询下周过生日的学生
  1. select *
  2. from Student
  3. where WEEKOFYEAR(Student.Sage)=WEEKOFYEAR(CURDATE())+1;
复制代码


  • 查询本月过生日的学生
  1. l> select *
  2.     -> from Student
  3.     -> where MONTH(Student.Sage)=MONTH(CURDATE());
复制代码


  • 查询下月过生日的学生
  1. > select *
  2.     -> from Student
  3.     -> where MONTH(Student.Sage)=MONTH(CURDATE())+1;
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

美食家大橙子

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表