MySQL数据库语句练习(附参考答案)

一、测试数据表

①测试数据

  1. 学生表
    1.1 student(Sid,Sname,Sage,Ssex)
    1.2 Sid 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别

  2. 课程表
    2.1 course(Cid,Cname,Tid)
    2.2 Cid 课程编号,Cname 课程名称,Tid 教师编号

  3. 教师表
    3.1 teacher(Tid,Tname)
    3.2 Tid 教师编号,Tname 教师姓名

  4. 成绩表
    4.1 sc(Sid,Cid,score)
    4.2 Sid 学生编号,Cid 课程编号,score 分数

②测试要求

根据需求创建以上四张表,添加对应的测试数据,测试数据如下:

  1. 学生表
create table student(Sid varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(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' , '女');
  1. 课程表
create table course(Cid varchar(10),Cname varchar(10),Tid varchar(10));
insert into course values('01' , '语文' , '02');
insert into course values('02' , '数学' , '01');
insert into course values('03' , '英语' , '03');
  1. 教师表
create table teacher(Tid varchar(10),Tname varchar(10));
insert into teacher values('01' , '张三');
insert into teacher values('02' , '李四');
insert into teacher values('03' , '王五');
  1. 成绩表
create table sc(Sid varchar(10),Cid 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);

二、sql语句练习

  1. 查询" 01 “课程比” 02 "课程成绩高的学生的信息及课程分数
  2. 修改学号为"01"的语文成绩为100
  3. 查询同时存在" 01 “课程和” 02 "课程的情况
  4. 查询平均成绩大于70分的学生学号和姓名
  5. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
  6. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
  7. 查询 1990 年出生的学生名单
  8. 求每门课程的学生人数
  9. 查询男生、女生人数
  10. 查询名字中含有「风」字的学生信息

三、参考答案

  1. 查询" 01 “课程比” 02 "课程成绩高的学生的信息及课程分数
select a.*, b.*, c.* from (select * from sc where Cid = '01') a
left join (select * from sc where Cid = '02') b
on a.Sid = b.Sid
left join student c
on a.Sid = c.Sid
where a.score > b.score;
  1. 修改学号为"01"的语文成绩为100
update sc set score=100 where Sid="01" and Cid=(select  Cid from course where Cname="语文");
  1. 查询同时存在" 01 “课程和” 02 "课程的情况
select a.*, b.* from (select * from sc where Cid = '01') a
left join (select * from sc where Cid = '02') b
on a.Sid = b.Sid
where b.Sid is not null;
  1. 查询平均成绩大于70分的学生学号和姓名
select Sid, Sname  from student where Sid in 
(select Sid from sc group by Sid having avg( score) >= 70);
  1. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
select a.Sid, a.score, b.Sname from (
select Sid, avg(score) as score from sc group by Sid having avg(score) >= 85)a
left join student b
on a.Sid = b.Sid;
  1. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
select a.Sid, b.Sname, c.Cname, a.score from sc a
left join student b
on a.Sid = b.Sid
left join course c
on a.Cid = c.Cid
where score > 70;
  1. 查询 1990 年出生的学生名单
select * from student where year(Sage) = 1990;
  1. 求每门课程的学生人数
select Cid, count(distinct Sid) as counts from sc group by C
  1. 查询男生、女生人数
select Ssex, count(distinct Sid) as counts from student group by Ssex;
  1. 查询名字中含有「风」字的学生信息
select * from student where Sname like '%风%';

你可能感兴趣的:(MySQL数据库)