MySQL基础练习50题

题目来源:https://link.zhihu.com/?target=http%3A//blog.csdn.net/flycat296/article/details/63681089

一、练习所使用的表格格式如下:

1. Student 表

      Student(SId,Sname,Sage,Ssex)    # 其中 SId 表示学生编号,Sname 表示学生姓名,Sage 表示出生年月,Ssex 表示学生性别;

2. Course 表

      Course(CId,Cname,TId)                 # 其中 CId 表示课程编号,Cname 表示课程名称,TId 表示教师编号;

3. Teacher 表

     Teacher(TId,Tname)                        # 其中 TId 表示教师编号,Tname 表示教师姓名;

4. SC (成绩)表

     SC(SId,CId,score)                           # 其中 SId 表示学生编号,CId 表示课程编号,score 表示分数。

二、练习所使用的数据如下:

   1. Student 表

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('09' , '张三' , '2017-12-20' , '女');
insert into Student values('10' , '李四' , '2017-12-25' , '女');
insert into Student values('11' , '李四' , '2017-12-30' , '女');
insert into Student values('12' , '赵六' , '2017-01-01' , '女');
insert into Student values('13' , '孙七' , '2018-01-01' , '女');

   2.  Course 表

create table Course(CId varchar(10),Cname nvarchar(10),TId varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');

   3. Teacher 表

create table Teacher(TId varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

   4. SC 表

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);

三、题目、答案以及截图如下:

01.  查询 "01" 课程比 "02" 课程成绩高的学生的信息及课程分数。

select t1.SId,t1.Sname,t1.Sage,t1.Ssex,t2.CId,t2.score,t3.CId,t3.score from 
(select SId,Sname,Sage,Ssex from Student) as t1,
(select SId,CId,score from SC) as t2,
(select SId,CId,score from SC) as t3 
where t1.SId=t2.SId and t1.SId=t3.SId and t2.CId<t3.CId and t3.CId!='03' and t2.score>t3.score;

02. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩。

select t2.SId,t2.Sname,avgscore from 
(select SId,AVG(score) as avgscore from sc 
GROUP BY SId having AVG(score)>60) as t1, 
(select * from Student) as t2 
where t1.SId=t2.SId;

              

03. 查询在 SC 表存在成绩的学生信息

select Distinct student.*
from student,sc
where student.SId=sc.SId;

              

4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为null)

select student.SId,student.Sname,t1.sumscore,t1.coursecount from student,
(select SC.SId,sum(sc.score) as sumscore,count(sc.CId) as coursecount
from sc group by sc.sid) as t1
where student.Sid=t1.sid;

05. 查询「李」姓老师的数量

select count(*)
from Teacher
where Teacher.Tname like '李%';

    

06. 查询学过「张三」老师授课的同学的信息

/*  方法一  */
select student.* from student,
(select sc.Sid from sc,
(select course.CId from course,teacher 
where course.TId=teacher.TId and teacher.Tname='张三') as t1
where sc.cid=t1.cid) as t2
where t2.Sid=student.Sid;


/*  方法二  */
select student.*
from teacher  ,course  ,student,sc
where teacher.Tname='张三'
and   teacher.TId=course.TId
and   course.CId=sc.CId
and   sc.SId=student.SId

07. 查询没有学全所有课程的同学的信息

 

(未完待续,如果各位有发现错误或者更好的解决方案,欢迎指教)

展开阅读全文
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值