IT评测·应用市场-qidao123.com

标题: MySQL必学!!!(练习题50道精选) [打印本页]

作者: 美食家大橙子    时间: 2024-8-19 05:14
标题: MySQL必学!!!(练习题50道精选)
一、 数据表介绍

数据表介绍
–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);
复制代码
三、练习题

  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';
复制代码

  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;
复制代码

  1. > select DISTINCT Student.*
  2.     -> from Student,SC
  3.     -> where Student.SId=SC.SId
  4.     -> ;
复制代码

  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.     -> );
复制代码

  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.     -> );
复制代码

  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;
复制代码

  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;
复制代码

  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;
复制代码

  1. > select *
  2.     -> from Student
  3.     -> where YEAR(Student.Sage)=1990;
复制代码

25. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩雷同时,按课程编号升序排列
  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;
复制代码

  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;
复制代码

  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企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4