# 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数 -- 第一小题

• ## 学生表

• ### student(Sid,Sname,Sage,Ssex)

• #### 创建学生表表,添加对应的测试数据,测试数据如下

```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' , '女');```

• ## 课程表

• ### course(Cid,Cname,Tid)

• #### 创建学课程表,添加对应的测试数据,测试数据如下

• ```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');```

• ## 教师表

• ### teacher(Tid,Tname)

• #### 创建学教师表,添加对应的测试数据,测试数据如下

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

• ## 成绩表

• ### sc(Sid,Cid,score)

• #### 创建学成绩表,添加对应的测试数据,测试数据如下

• ```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 "课程比" 02 "课程成绩高的学生的信息及课程分数

满足条件的 sc《成绩表》中，

1：获得同一个学生，

2：该学生的01课程比02课程高！！！

3：需要展示该学生的信息，及其01与02的分数

即：将满足以上三个条件的信息，在一张表中展示出来。

#### 1、找出有01成绩的同学成绩信息

`SELECT * from sc WHERE sc.CId='01'`

#### -- 2、找出有02成绩的同学成绩信息

`SELECT * from sc WHERE sc.CId='02'`

#### -- 3、以上两种结果需要满足一定条件（1）SId要一致【同一人】（2）且01.score>02.score

`SELECT* FROM (SELECT * from sc WHERE sc.CId='01')as a,(SELECT * from sc WHERE sc.CId='02')as b WHERE a.Sid=b.Sid and a.score>b.score;`

#### -- 4.学生01_score和02_score成绩信息对比表

`SELECT * from student RIGHT JOIN (SELECT a.SId,a.score 01_score,b.score 02_score FROM (SELECT * from sc WHERE sc.CId='01')as a,(SELECT * from sc WHERE sc.CId='02')as b WHERE a.SId=b.SId AND a.score>b.score) r ON student.SId=r.SId`