sql 综合练习

一、准备数据

1、创建学生表

1 create table Student(SID varchar(10),Sname nvarchar(10),Sage datetime,Ssex nvarchar(10));

1.1向学生表中插入数据

1 insert into Student values('01' , '赵雷' , '1990-01-01' , '');
2 insert into Student values('02' , '钱电' , '1990-12-21' , '');
3 insert into Student values('03' , '孙风' , '1990-05-20' , '');
4 insert into Student values('04' , '李云' , '1990-08-06' , '');
5 insert into Student values('05' , '周梅' , '1991-12-01' , '');
6 insert into Student values('06' , '吴兰' , '1992-03-01' , '');
7 insert into Student values('07' , '郑竹' , '1989-07-01' , '');
8 insert into Student values('08' , '王菊' , '1990-01-20' , '');

2、创建课程表

1 create table Course(CID varchar(10),Cname nvarchar(10),TID varchar(10));

2.1向课程表中插入数据

1 insert into Course values('01' , '语文' , '02');
2 insert into Course values('02' , '数学' , '01');
3 insert into Course values('03' , '英语' , '03');

3、 创建教师表

1 create table Teacher(TID varchar(10),Tname nvarchar(10));

3.1向教师表中插入数据

1 insert into Teacher values('01' , '张三');
2 insert into Teacher values('02' , '李四');
3 insert into Teacher values('03' , '王五');

4、 创建成绩表

1 create table SC(SID varchar(10),CID varchar(10),score decimal(18,1));

4.1向成绩表中插入数据

 1 insert into SC values('01' , '01' , 80);
 2 insert into SC values('01' , '02' , 90);
 3 insert into SC values('01' , '03' , 99);
 4 insert into SC values('02' , '01' , 70);
 5 insert into SC values('02' , '02' , 60);
 6 insert into SC values('02' , '03' , 80);
 7 insert into SC values('03' , '01' , 80);
 8 insert into SC values('03' , '02' , 80);
 9 insert into SC values('03' , '03' , 80);
10 insert into SC values('04' , '01' , 50);
11 insert into SC values('04' , '02' , 30);
12 insert into SC values('04' , '03' , 20);
13 insert into SC values('05' , '01' , 76);
14 insert into SC values('05' , '02' , 87)
15 insert into SC values('06' , '01' , 31);
16 insert into SC values('06' , '03' , 34);
17 insert into SC values('07' , '02' , 89);
18 insert into SC values('07' , '03' , 98);

二、练习

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

1.1查询同时存在"01"课程和"02"课程的情况

1 select a.*
2 ,b.score '01课程成绩'
3 ,c.score '02课程成绩'
4 from student a 
5 join sc b on a.`SID`=b.`SID`
6 join sc c on b.`SID`=c.`SID`
7 where b.`CID`='01' and c.`CID`=02 and b.score>c.score

结果:

-- 1.2、查询同时存在"01"课程和"02"课程的情况和存在"01"课程但可能不存在"02"课程的情况(不存在时显示为null)(以下存在相同内容时不再解释)

1 select a.* 
2 , b.score 课程01的分数
3 ,c.score 课程02的分数 
4 from Student a
5 left join SC b on a.SID = b.SID and b.CID = '01'
6 left join SC c on a.SID = c.SID and c.CID = '02'
7 where b.score > isnull(c.score)

结果:

 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数

2.1查询同时存在"01"课程和"02"课程的情况

1 select a.*
2         ,b.score '课程01的分数'
3         ,c.score '课程02的分数'
4 from student a 
5 join sc b on a.`SID`=b.`SID`
6 join sc c on a.`SID`=c.`SID`
7 where b.`CID`='01' and c.`CID`='02' and b.score<c.score 

结果:

2.2、查询同时存在"01"课程和"02"课程的情况和不存在"01"课程但存在"02"课程的情况

1 select a.*
2         ,b.score 课程01的分数
3         ,c.score 课程02的分数
4 from student a 
5 left join sc b on a.`SID`=b.`SID` and b.`CID`='01'
6 left join sc c on a.`SID`=c.`SID` and c.`CID`='02'
7 where isnull(b.score)<c.score 

结果:

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

1 select a.SID , a.Sname 
2         , cast(avg(b.score) as decimal(18,2)) avg_score
3 from Student a , sc b
4 where a.SID = b.SID
5 group by a.SID , a.Sname
6 having cast(avg(b.score) as decimal(18,2)) >= 60
7 order by a.SID

结果:

 ①cast函数运用

CAST函数用于将某种数据类型的表达式显式转换为另一种数据类型。CAST()函数的参数是一个表达式,它包括用AS关键字分隔的源值和目标数据类型。

语法:CAST (expression AS data_type)

  • expression:任何有效的SQServer表达式。
  • AS:用于分隔两个参数,在AS之前的是要处理的数据,在AS之后是要转换的数据类型。
  • data_type:目标系统所提供的数据类型,包括bigint和sql_variant,不能使用用户定义的数据类型。

data_type类型

  • 二进制,同带binary前缀的效果 : BINARY    
  • 字符型,可带参数 : CHAR()     
  • 日期 : DATE     
  • 时间: TIME     
  • 日期时间型 : DATETIME     
  • 浮点数 : DECIMAL      
  • 整数 : SIGNED     
  • 无符号整数 : UNSIGNED
1 SELECT  CAST(NOW() AS DATE)

结果:

②decimal运用

decimal(18,3)中的“2”表示小数部分的位数,如果插入的值未指定小数部分或者小数部分不足两位则会自动补3位小数,若插入的值小数部分超过了3为则会发生截断,截取前3位小数。

“18”指的是整数部分加小数部分的总长度,也即插入的数字整数部分不能超过“18-2”位,否则不能成功插入,会报超出范围的错误。

1 select cast(1234.76543 as decimal(18,3)) 

结果:

4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩--4.1、查询在sc表存在成绩的学生信息的SQL语句。

1 select b.*    
2         ,cast(avg(a.score) as decimal(18,2)) avg_score
3 from sc a
4 left join student b on a.`SID`=b.`SID`
5 group by a.`SID`
6 having avg_score<60
7 order by avg_score desc

结果:

 4.2查询在sc表中不存在成绩的学生信息的SQL语句。

1 select a.SID 
2         , a.Sname 
3         , cast(avg(b.score) as decimal(18,2)) avg_score
4 from Student a left join sc b on a.SID = b.SID
5 group by a.SID , a.Sname
6 having ifnull(cast(avg(b.score) as decimal(18,2)),0) < 60
7 order by a.SID

结果:

5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩--5.1、查询所有有成绩的SQL。

1 select a.`SID`
2         ,a.`Sname`
3         ,count(distinct b.`CID`) 选课总数
4         ,sum(b.score) 总成绩
5 from student a
6 join sc b on a.`SID`=b.`SID`
7 group by a.`SID`

 5.2、查询所有(包括有成绩和无成绩)的SQL。

1 select a.`SID`
2         ,a.`Sname`
3         ,count(b.`CID`) 选课总数
4         ,sum(b.score) 总成绩
5 from student a
6 left join sc b on a.`SID`=b.`SID`
7 group by a.`Sname`
8 order by a.`SID` desc

 6、查询"李"姓老师的数量

方法1

1 select count(a.`Tname`) 李姓老师的数量
2 from teacher a
3 where a.`Tname` like "李%"

方法2

1 select count(Tname) 李姓老师的数量 
2 from Teacher 
3 where left(Tname,1) = ''

 7、查询学过"张三"老师授课的同学的信息

1 select distinct d.*
2 from teacher a
3 join course b on a.`TID`=b.`TID`
4 join sc c on b.`CID`=c.`CID`
5 join student d on c.`SID` = d.`SID`
6 where a.`Tname` = '张三'

 8、查询没学过"张三"老师授课的同学的信息

1 select *
2 from student m
3 where m.`SID` not in
4 (select c.`SID`
5 from teacher a
6 join course b on a.`TID`=b.`TID`
7 join sc c on b.`CID`=c.`CID`
8 where a.`Tname`='张三')

 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

方法1

1 select a.*
2 from student a
3 join sc b on a.`SID`=b.`SID` and b.`CID`='01'
4 join sc c on b.`SID`=c.`SID` and c.`CID`='02'

方法2

1 select a.*
2 from student a
3 join sc  on a.`SID`=sc.`SID` and sc.`CID`='01'
4 where exists (select * from sc c 
5 where c.`SID`=sc.`SID` and c.`CID`='02')

方法3

 1 select m.* from Student m where SID in
 2 (
 3 select SID from
 4 (
 5 select * from SC where CID = '01'
 6 union all
 7 select * from SC where CID = '02'
 8 ) t group by SID having count(`SID`) = 2
 9 )
10 order by m.SID

 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

方法1

1 select a.*
2 from student a 
3 join sc b on a.`SID`=b.`SID` and b.`CID`='01'
4 where not exists(select * from sc c where c.`SID`=b.`SID` 
5 and c.`CID`='02')
6 order by a.`SID`

方法2

1 select a.*
2 from student a 
3 join sc b on a.`SID`=b.`SID` and b.`CID`='01'
4 where a.`SID`not in 
5 (select c.`SID` from sc c where b.`SID`=c.`SID` and c.`CID`='02')

11、查询没有学全所有课程的同学的信息

1 select a.*
2 from student a
3 join sc b on a.`SID`=b.`SID`
4 group by b.`SID`
5 having count(b.`CID`) !=3

1 select Student.*
2 from Student left join SC
3 on Student.SID = SC.SID
4 group by Student.SID , Student.Sname 
5 , Student.Sage , Student.Ssex having count(CID) < (select count(CID) from Course)

12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息

1 select distinct a.*
2 from student a
3 join sc b on a.`SID`=b.`SID`
4 where b.`CID` in (select c.`CID` from sc c where c.`SID`='01' )
5 and a.`SID`!='01'

13、查询和"01"号的同学学习的课程完全相同的其他同学的信息

1 select Student.* from Student 
2 where SID in
3 (select distinct SC.SID from SC where SID <> '01' and
4 SC.CID in (select distinct CID from SC where SID = '01') 
5 group by SC.SID having count(1) = (select count(1) from SC where SID='01'))

 14、查询没学过"张三"老师讲授的任一门课程的学生姓名

1 select *
2 from student m
3 where m.`Sname`not in(
4 select distinct a.`Sname`
5 from student a
6 join sc b on a.`SID`=b.`SID`
7 join course c on c.`CID` = b.`CID`
8 join teacher d on d.`TID`=c.`TID`
9 where d.`Tname`='张三')

 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

1 select a.`SID`
2         ,a.`Sname`
3         ,cast(avg(b.score) as decimal(18,2)) as 平均成绩
4 from student a 
5 join sc b on a.`SID`=b.`SID`
6 where a.`SID` in (select b.`SID`
7 from sc b  where b.score<60)
8 group by b.`SID` having count(b.`SID`)>1

16、检索"01"课程分数小于60,按分数降序排列的学生信息

1 select a.*
2         ,b.`CID`
3         ,b.score
4 from student a
5 join sc b on a.`SID`=b.`SID`
6 where b.`CID`='01' and b.score<60
7 order by b.score desc

 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩--17.1 SQL 2000 静态

 1 select a.`SID` 学号
 2         ,a.`Sname` 姓名
 3         ,max(case c.`Cname` when '语文' then b.score else null end)语文
 4         ,max(case c.`Cname` when '数学' then b.score else null end)数学
 5         ,max(case c.`Cname` when '英语' then b.score else null end)英语
 6         ,cast(avg(b.score) as decimal(18,2)) 平均分
 7 from student a
 8 left join sc b on a.`SID`=b.`SID`
 9 join course c on b.`CID`=c.`CID`
10 group by a.`SID`
11 order by 平均分 desc 

18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

方法1

 1 select m.CID 课程编号 , m.Cname 课程名称 ,
 2         max(n.score) 最高分 ,
 3         min(n.score) 最低分 ,
 4         cast(avg(n.score) as decimal(18,2)) 平均分 ,
 5         cast((select count(1) from sc where sc.`CID`=m.`CID` and score>=60)*100.0/(select count(1) from sc where `CID`=m.`CID`) as decimal(18,2)) 及格率 ,
 6         cast((select count(1) from SC where CID = m.CID and score >= 70 and score < 80 )*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 中等率 ,
 7         cast((select count(1) from SC where CID = m.CID and score >= 80 and score < 90 )*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 优良率 ,
 8         cast((select count(1) from SC where CID = m.CID and score >= 90)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 优秀率
 9 from Course m , SC n
10 where m.CID = n.CID
11 group by m.CID , m.Cname
12 order by m.CID

方法2

 1 select m.CID 课程编号 , m.Cname 课程名称 ,
 2 (select max(score) from SC where CID = m.CID) 最高分 ,
 3 (select min(score) from SC where CID = m.CID) 最低分 ,
 4 (select cast(avg(score) as decimal(18,2)) from SC where CID = m.CID) 平均分 ,
 5 cast((select count(1) from SC where CID = m.CID and score >= 60)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 及格率,
 6 cast((select count(1) from SC where CID = m.CID and score >= 70 and score < 80 )*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 中等率 ,
 7 cast((select count(1) from SC where CID = m.CID and score >= 80 and score < 90 )*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 优良率 ,
 8 cast((select count(1) from SC where CID = m.CID and score >= 90)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 优秀率
 9 from Course m
10 order by m.CID

--19、按各科成绩进行排序,并显示排名--19.1 sql 2000用子查询完成

Score重复时保留名次空缺

1 select t.* , 
2 (select count(1) from SC where CID = t.CID and score > t.score) + 1 r 
3 from sc t 
4 order by t.`CID`,r 

Score重复时合并名次

1 select t.* ,
2 (select count(distinct score) from SC where CID = t.CID and score >= t.score) r 
3 from sc t order by t.`CID`,r

 20、查询学生的总成绩并进行排名--20.1 查询学生的总成绩

1 select b.*
2         ,ifnull(sum(a.score),0) 总成绩
3 from student b
4 left join sc a on a.`SID`=b.`SID`
5 group by b.`Sname`
6 order by 总成绩 desc

 20.2 查询学生的总成绩并进行排名,sql 2000用子查询完成,分总分重复时保留名次空缺和不保留名次空缺两种。

 1 select t1.* ,(select count(1) from
 2 (
 3 select m.SID 学生编号 ,
 4 m.Sname 学生姓名 ,
 5 ifnull(sum(score),0) 总成绩
 6 from Student m left join SC n on m.SID = n.SID
 7 group by m.SID , m.Sname) t2 
 8 where 总成绩 > t1.总成绩) + 1 px from
 9 (
10 select m.SID 学生编号 ,
11 m.Sname 学生姓名 ,
12 ifnull(sum(score),0) 总成绩
13 from Student m left join SC n on m.SID = n.SID
14 group by m.SID , m.Sname
15 ) t1
16 order by px

 ②

 1 select t1.* ,(select count(distinct 总成绩) from
 2 (
 3 select m.SID 学生编号 ,
 4 m.Sname 学生姓名 ,
 5 ifnull(sum(score),0) 总成绩
 6 from Student m left join SC n on m.SID = n.SID
 7 group by m.SID , m.Sname
 8 ) t2 where 总成绩 >= t1.总成绩) px from
 9 (
10 select m.SID 学生编号 ,
11 m.Sname 学生姓名 ,
12 ifnull(sum(score),0) 总成绩
13 from Student m left join SC n on m.SID = n.SID
14 group by m.SID , m.Sname
15 ) t1
16 order by px

 21、查询不同老师所教不同课程平均分从高到低显示

1 select a.`Tname`
2         ,b.`CID`
3         ,round(avg(c.score),2) 平均分
4 from teacher a 
5 join course b on a.`TID`=b.`TID`
6 join sc c on b.`CID`=c.`CID`
7 group by a.`TID`
8 order by 平均分 desc

22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩--22.1 sql 2000用子查询完成

①Score重复时保留名次空缺

1 select * from (select t.* ,
2 (select count(1) from SC where CID = t.CID and score > t.score) + 1 px 
3 from sc t) m join student a on m.SID=a.`SID`
4 where px between 2 and 3 order by m.cid , m.px

 

 ②Score重复时合并名次

1 select * from (select t.* ,
2 (select count(distinct score) from SC where CID = t.CID and score >= t.score) px
3 from sc t) m join student a on m.SID=a.`SID`
4 where px between 2 and 3 order by m.cid , m.px

 23、统计各科成绩各分数段人数:课程编号,课程名称, 100-85 , 85-70 , 70-60 , 0-60 及所占百分比 

23.1 统计各科成绩各分数段人数:课程编号,课程名称, 100-85 , 85-70 , 70-60 , 0-60

①横向显示

1 select Course.CID 课程编号 , Cname as 课程名称 ,
2 sum(case when score >= 85 then 1 else 0 end) '85-100' ,
3 sum(case when score >= 70 and score < 85 then 1 else 0 end) '70-85' ,
4 sum(case when score >= 60 and score < 70 then 1 else 0 end) '60-70' ,
5 sum(case when score < 60 then 1 else 0 end) '0-60'
6 from sc , Course
7 where SC.CID = Course.CID
8 group by Course.CID , Course.Cname
9 order by Course.CID

 ②纵向显示1(显示存在的分数段)

 1 select m.CID 课程编号 , m.Cname 课程名称 ,(
 2 case when n.score >= 85 then '85-100'
 3 when n.score >= 70 and n.score < 85 then '70-85'
 4 when n.score >= 60 and n.score < 70 then '60-70'
 5 else '0-60'
 6 end) 分数段 ,
 7 count(1) 数量
 8 from Course m , sc n
 9 where m.CID = n.CID
10 group by m.CID , m.Cname , (
11 case when n.score >= 85 then '85-100'
12 when n.score >= 70 and n.score < 85 then '70-85'
13 when n.score >= 60 and n.score < 70 then '60-70'
14 else '0-60'
15 end)
16 order by m.CID , m.Cname , 分数段

23.2 统计各科成绩各分数段人数:课程编号,课程名称, 100-85 , 85-70 , 70-60 , <60 及所占百分比

①横向显示

 1 select m.CID 课程编号, m.Cname 课程名称,
 2 (select count(1) from SC where CID = m.CID and score < 60) '0-60' ,
 3 cast((select count(1) from SC where CID = m.CID and score < 60)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 百分比 ,
 4 (select count(1) from SC where CID = m.CID and score >= 60 and score < 70) '60-70' ,
 5 cast((select count(1) from SC where CID = m.CID and score >= 60 and score < 70)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 百分比 ,
 6 (select count(1) from SC where CID = m.CID and score >= 70 and score < 85) '70-85' ,
 7 cast((select count(1) from SC where CID = m.CID and score >= 70 and score < 85)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 百分比 ,
 8 (select count(1) from SC where CID = m.CID and score >= 85) '85-100' ,
 9 cast((select count(1) from SC where CID = m.CID and score >= 85)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 百分比
10 from Course m
11 order by m.CID

②纵向显示1(显示存在的分数段)

 1 select m.CID 课程编号 , m.Cname 课程名称 , (
 2 case when n.score >= 85 then '85-100'
 3 when n.score >= 70 and n.score < 85 then '70-85'
 4 when n.score >= 60 and n.score < 70 then '60-70'
 5 else '0-60'
 6 end)分数段 ,
 7 count(1) 数量 ,
 8 cast(count(1) * 100.0 / (select count(1) from sc where CID = m.CID) as decimal(18,2)) 百分比
 9 from Course m , sc n
10 where m.CID = n.CID
11 group by m.CID , m.Cname , (
12 case when n.score >= 85 then '85-100'
13 when n.score >= 70 and n.score < 85 then '70-85'
14 when n.score >= 60 and n.score < 70 then '60-70'
15 else '0-60'
16 end)
17 order by m.CID , m.Cname , 分数段

24、查询学生平均成绩及其名次--24.1 查询学生的平均成绩并进行排名,sql 2000用子查询完成,分平均成绩重复时保留名次空缺和不保留名次空缺两种。

 1 select f1.*
 2         ,(select count(1) from (
 3 select a.*
 4         ,ifnull(cast(avg(score) as decimal(18,2)),0)平均分
 5 from student a left join sc b on a.`SID`=b.`SID`
 6 group by a.`SID`
 7 ) f where 平均分>=f1.平均分) 排名
 8 from (select a.*
 9         ,ifnull(cast(avg(score) as decimal(18,2)),0) 平均分
10 from student a left join sc b on a.`SID`=b.`SID`
11 group by a.`SID`) f1
12 order by 平均分 desc

 25、查询各科成绩前三名的记录,分数重复时不保留名次空缺,合并名次

1 select * from 
2 (select t.* ,
3 (select count(distinct score) from SC where CID = t.CID and score >= t.score) px 
4 from sc t) m 
5 where px between 1 and 3 order by m.Cid , m.px

 26、查询每门课程被选修的学生数

1 select a.`CID`,count(1) 学生数
2 from sc a 
3 group by a.`CID`

 27、查询出只有两门课程的全部学生的学号和姓名

1 select a.SID , a.Sname
2 from Student a , SC b 
3 where a.SID = b.SID
4 group by a.SID , a.Sname
5 having count(b.CID) = 2
6 order by a.SID

 28、查询男生、女生人数

1 select sum(case when Ssex = N'' then 1 else 0 end) 男生人数 ,sum(case when Ssex = N'' then 1 else 0 end) 女生人数 from student

 ②

1 select case when Ssex = N'' then N'男生人数' else N'女生人数' end 男女情况 , count(1) 人数 from student group by case when Ssex = N'' then N'男生人数' else N'女生人数' end

 29、查询名字中含有"风"字的学生信息

1 select * from student where sname like '%风%'

 30、查询同名同性学生名单,并统计同名人数

1 select Sname 学生姓名 , count(*) 人数 from Student group by Sname having count(*) > 1

31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)

1 select * from Student where year(sage) = 1990

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

1 select a.`CID`,b.`Cname`,cast(avg(a.score) as decimal(18,1)) 平均分 
2 from sc a,course b
3 where a.`CID`=b.`CID`
4 group by a.`CID`
5 order by 平均分 desc,a.`CID`

 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩

1 select a.`SID`,a.`Sname`,cast(avg(b.score) as decimal(18,1)) 平均成绩
2 from student a,sc b
3 where a.`SID`=b.`SID`
4 group by a.`Sname`
5 having 平均成绩>=85

 34、查询课程名称为"数学",且分数低于60的学生姓名和分数

1 select b.`Sname`,c.score
2 from course a,student b,sc c
3 where a.`CID`=c.`CID` and c.`SID`=b.`SID` and a.`Cname`='数学' 
4 having c.score<60

 35、查询所有学生的课程及分数情况

1 select Student.* , Course.Cname , SC.CID , SC.score
2 from Student, SC , Course
3 where Student.SID = SC.SID and SC.CID = Course.CID
4 order by Student.SID , SC.CID

 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数

1 select a.`Sname`,c.`Cname`,b.score
2 from student a,sc b,course c
3 where a.`SID`=b.`SID` and b.`CID`=c.`CID` and b.score>=70

 37、查询不及格的课程

1 select c.*,a.`CID`,b.score
2 from course a,sc b,student c
3 where a.`CID`=b.`CID`and b.`SID`=c.`SID`and b.score<60

 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名

1 select b.`SID`,b.`Sname`,a.*
2 from sc a,student b
3 where a.`SID`=b.`SID`and a.score>=80 and a.`CID`='01'

 39、求每门课程的学生人数

1 select a.`CID`,count(*) 学生人数
2 from sc a,student b
3 where a.`SID`=b.`SID`
4 group by a.`CID`

 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩--40.1 当最高分只有一个时

1 select b.*,a.score
2 from sc a,student b,course c,teacher d
3 where a.`SID`=b.`SID` and a.`CID`=c.`CID`and d.`TID`=c.`TID`and d.`Tname`like '张三'
4 order by a.score desc 
5 limit 1)

 40.2 当最高分出现多个时

1 select b.*,a.score
2 from sc a,student b,course c,teacher d
3 where a.`SID`=b.`SID` and a.`CID`=c.`CID`and d.`TID`=c.`TID`and d.`Tname`like '张三'
4 and a.score=(select a.score
5 from sc a,student b,course c,teacher d
6 where a.`SID`=b.`SID` and a.`CID`=c.`CID`and d.`TID`=c.`TID`and d.`Tname`like '张三'
7 order by a.score desc 
8 limit 1)

41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

1 select m.* from SC m 
2 ,(select CID , score from SC group by CID , score having count(1) > 1) n
3 where m.CID= n.CID and m.score = n.score 
4 order by m.CID , m.score , m.SID

1 select m.* from SC m where exists (select 1 from (select CID , score from SC group by CID , score having count(1) > 1) n
2 where m.CID= n.CID and m.score = n.score) 
3 order by m.CID , m.score , m.SID

 42、查询每门课成绩最好的前两名

1 SELECT s1.* FROM sc s1 WHERE
2 (
3 SELECT COUNT(1) FROM sc s2 WHERE
4 s1.`CID`=s2.`CID` AND s2.score>=s1.score
5 )<=2
6 ORDER BY s1.`CID`,s1.score DESC;

43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

1 select a.`CID`,b.`Cname`,count(a.`SID`) 选修人数
2 from sc a,course b
3 where a.`CID`=b.`CID`
4 group by a.`CID`
5 having 选修人数>5
6 order by 选修人数 desc,a.`CID`

44、检索至少选修两门课程的学生学号

1 select a.*,count(*) 课程数
2 from student a,sc b
3 where a.`SID`=b.`SID`
4 group by a.`SID`
5 having 课程数>=2

 45、查询选修了全部课程的学生信息

①根据数量来完成

1 select student.* from student where SID in
2 (select SID from sc group by SID 
3 having count(1) = (select count(1) from course))

②使用双重否定来完成

 1 select t.* from student t 
 2 where t.SID not in
 3 (
 4 select distinct m.SID from
 5 (
 6 select SID , CID from student , course
 7 ) m 
 8 where not exists 
 9 (select 1 from sc n where n.SID = m.SID and n.CID = m.CID)
10 )

③使用双重否定来完成

 1 select t.* from student t 
 2 where not exists(select 1 from
 3 (
 4 select distinct m.SID from
 5 (
 6 select SID , CID from student , course
 7 ) m where not exists 
 8 (select 1 from sc n where n.SID = m.SID and n.CID = m.CID)
 9 ) k where k.SID = t.SID
10 )

 46、查询各学生的年龄--46.1 只按照年份来算

1 select student.*,date_format(now(),'%Y')-date_format(student.Sage,'%Y') as age from student;

1 select a.*,year(now())-year(a.`Sage`) 年龄
2 from student a

46.2 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

1 select a.*, 
2 year(a.`Sage`),
3 (case when dayofyear(now())<dayofyear(a.`Sage`) 
4  then year(now())-year(a.`Sage`)-1
5  else year(now())-year(a.`Sage`) 
6  end)'年龄'
7 from student a

47、查询本周过生日的学生

1 select a.*
2 from student a
3 where abs(dayofyear(now())-dayofyear(a.`Sage`))<7 and dayofyear(now())-dayofyear(a.`Sage`)<0

48、查询下周过生日的学生

1 select a.*
2 from student a
3 where abs(dayofyear(now())-dayofyear(a.`Sage`))<=14 
4 and 7<abs(dayofyear(now())-dayofyear(a.`Sage`)) 
5 and dayofyear(now())-dayofyear(a.`Sage`)<0

49、查询本月过生日的学生

1 select a.*,month(a.Sage) 月份
2 from student a
3 where month(a.Sage)=month(now())

50、查询下月过生日的学生

1 select a.*,month(a.Sage) 月份
2 from student a
3 where month(a.Sage)=month(now())+1

posted on 2019-12-18 18:43  LiErRui  阅读(496)  评论(0)    收藏  举报

导航