前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mysql 小练习

Mysql 小练习

作者头像
江小白
修改2019-05-27 16:26:17
1.9K0
修改2019-05-27 16:26:17
举报

设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。表结构及数据如下,请完成题目。

表(一)Student (学生表) :

代码语言:javascript
复制
属性名    数据类型    可否为空    含 义
Sno    varchar (20)    否    学号(主码)
Sname    varchar (20)    否    学生姓名
Ssex    varchar (20)    否    学生性别
Sbirthday    datetime    可    学生出生年月
Class    varchar (20)    可    学生所在班级

表(二)Course(课程表):

代码语言:javascript
复制
属性名    数据类型    可否为空    含 义
Cno    varchar (20)    否    课程号(主码)
Cname    varchar (20)    否    课程名称
Tno    varchar (20)    否    教工编号(外码)

表(三)Score(成绩表):

代码语言:javascript
复制
属性名    数据类型    可否为空    含 义
Sno    varchar (20)    否    学号(外码))
Cno    varchar (20)    否    课程号(外码)
Degree    Decimal(4,1)    可    成绩

主码:Sno+ Cno

表(四)Teacher(教师表):

代码语言:javascript
复制
属性名    数据类型    可否为空    含 义
Tno    varchar (20)    否    教工编号(主码)
Tname    varchar (20)    否    教工姓名
Tsex    varchar (20)    否    教工性别
Tbirthday    datetime    可    教工出生年月
Prof    varchar (20)    可    职称
Depart    varchar (20)    否    教工所在部门

表1-2数据库中的数据:

表(一)Student:

代码语言:javascript
复制
Sno    Sname    Ssex    Sbirthday    class
108    曾华    男    1977-09-01    95033
105    匡明    男    1975-10-02    95031
107    王丽    女    1976-01-23    95033
101    李军    男    1976-02-20    95033
109    王芳    女    1975-02-10    95031
103    陆君    男    1974-06-03    95031

表(二)Course:

代码语言:javascript
复制
Cno    Cname    Tno
3-105    计算机导论    825
3-245    操作系统    804
6-166    数字电路    856
9-888    高等数学    831

表(三)Score:

代码语言:javascript
复制
Sno    Cno    Degree
103    3-245    86
105    3-245    75
109    3-245    68
103    3-105    92
105    3-105    88
109    3-105    76
101    3-105    64
107    3-105    91
108    3-105    78
101    6-166    85
107    6-166    79
108    6-166    81

表(四)Teacher:

代码语言:javascript
复制
Tno    Tname    Tsex    Tbirthday    Prof    Depart
804    李诚    男    1958-12-02    副教授    计算机系
856    张旭    男    1969-03-12    讲师    电子工程系
825    王萍    女    1972-05-05    助教    计算机系
831    刘冰    女    1977-08-14    助教    电子工程系

#建学生信息表student

代码语言:javascript
复制
create table student(
sno varchar(20) not null primary key,
sname varchar(20) not null,
ssex varchar(20) not null,
sbirthday datetime,
class varchar(20)
);

#建立教师表

代码语言:javascript
复制
create table teache
(
tno varchar(20) not null primary key,
tname varchar(20) not null,
tsex varchar(20) not null,
tbirthday datetime,
prof varchar(20),
depart varchar(20) not null
);

#建立课程表course

代码语言:javascript
复制
create table course
(
cno varchar(20) not null primary key,
cname varchar(20) not null,
tno varchar(20) not null,
foreign key(tno) references teacher(tno)
);

#建立成绩表

代码语言:javascript
复制
create table score
(
sno varchar(20) not null primary key,
foreign key(sno) references student(sno),
cno varchar(20) not null,
foreign key(cno) references course(cno),
degree decimal
);

#添加学生信息

代码语言:javascript
复制
insert into student values('108','曾华','男','1977-09-01','95033');
insert into student values('105','匡明','男','1975-10-02','95031');
insert into student values('107','王丽','女','1976-01-23','95033');
insert into student values('101','李军','男','1976-02-20','95033');
insert into student values('109','王芳','女','1975-02-10','95031');
insert into student values('103','陆君','男','1974-06-03','95031');

#添加教师表

代码语言:javascript
复制
insert into teacher values('804','李诚','男','1958-12-02','副教授','计算机系');
insert into teacher values('856','张旭','男','1969-03-12','讲师','电子工程系');
insert into teacher values('825','王萍','女','1972-05-05','助教','计算机系');
insert into teacher values('831','刘冰','女','1977-08-14','助教','电子工程系');

#添加课程表

代码语言:javascript
复制
insert into course values('3-105','计算机导论','825');
insert into course values('3-245','操作系统','804');
insert into course values('6-166','数字电路','856');
insert into course values('9-888','高等数学','831');

#添加成绩表

代码语言:javascript
复制
insert into score values('103','3-245','86');
insert into score values('105','3-245','75');
insert into score values('109','3-245','68');
insert into score values('103','3-105','92');
insert into score values('105','3-105','88');
insert into score values('109','3-105','76');
insert into score values('103','3-105','64');
insert into score values('105','3-105','91');
insert into score values('109','3-105','78');
insert into score values('103','6-166','85');
insert into score values('105','6-166','79');
insert into score values('109','6-166','81');

1、 查询Student表中的所有记录的Sname、Ssex和Class列。

#单表查询

代码语言:javascript
复制
select Sname,Ssex,Class from Student;

2、 查询教师所有的单位即不重复的Depart列。

#单表查询

代码语言:javascript
复制
select distinct Depart from teacher;

3、 查询Student表的所有记录。

#单表查询

代码语言:javascript
复制
select * from student

4、 查询Score表中成绩在60到80之间的所有记录。

#单表查询

代码语言:javascript
复制
select * from Score where Degree between 60 and 80;

5、 查询Score表中成绩为85,86或88的记录。

#单表查询

代码语言:javascript
复制
select * from Score where Degree in (85,86,88);

6、 查询Student表中“95031”班或性别为“女”的同学记录。

#单表查询

代码语言:javascript
复制
select * from Student where Class= '95031' or Ssex='女';

7、 以Class降序查询Student表的所有记录。

#单表查询

代码语言:javascript
复制
select * from Student order by Class desc;

8、 以Cno升序、Degree降序查询Score表的所有记录。

#单表查询

代码语言:javascript
复制
select * from Score order by Cno asc,Degree desc;

9、 查询“95031”班的学生人数。

#单表查询

代码语言:javascript
复制
select count(*) from Student where Class = '95031';

10、查询Score表中的最高分的学生学号和课程号。(子查询或者排序)

#单表查询

代码语言:javascript
复制
select Sno,Cno from Score where Degree = (
select max(Degree) from Score);
select Sno,Cno from Score order by Degree desc limit 0,1;

11、 查询每门课的平均成绩。

#单表查询

代码语言:javascript
复制
select Cno,avg(Degree) from Score group by Cno;

12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

#单表查询

代码语言:javascript
复制
select Cno,avg(Degree) from Score 
where Cno like '3%'
group by Cno 
having count(Sno) >=5;

代码语言:javascript
复制
select avg(Degree) from score where Cno like '3%' and Cno in (select Cno from score group by Cno having count(*)>=5);

13、查询分数大于70,小于90的Sno列。

#单表查询

代码语言:javascript
复制
select Sno from Score where Degree >70 and Degree < 90;

14、查询所有学生的Sname、Cno和Degree列。

#多表查询

#可以用where,内连接,子查询

代码语言:javascript
复制
select Sname,Cno,Degree from Student,Score where Student.Sno=Score.Sno;

代码语言:javascript
复制
select Sname,Cno,Degree from Student inner join Score
on Student.Sno=Score.Sno;

15、查询所有学生的Sno、Cname和Degree列。

#多表查询

#同上

代码语言:javascript
复制
select Sno,Cname,Degree from Score,Course where Score.Cno=Course.Cno;

16、查询所有学生的Sname、Cname和Degree列。

#多表查询

#同上

代码语言:javascript
复制
select Sname,Cname,Degree from student,course,score where student.Sno=score.Sno and course.Cno=score.Cno;

代码语言:javascript
复制
select Sname,Cname,Degree from Student inner join Score on Student.Sno=Score.Sno inner join 
Course on Score.Cno=Course.Cno;

17、查询“95033”班学生的平均分。

#多表查询

#用where,内连接,子查询

代码语言:javascript
复制
select avg(Degree) from Score,Student where Student.Sno=Score.Sno and Class='95033';

代码语言:javascript
复制
select avg(Degree) from Score inner join Student on Student.Sno=Score.Sno where Class='95033';

代码语言:javascript
复制
select avg(Degree)  from Score where Sno in (select Sno from Student
where Class='95033');

18、 假设使用如下命令建立了一个grade表:

代码语言:javascript
复制
create table grade(low  int(3),upp  int(3),rank  char(1))
insert into grade values(90,100,’A’)
insert into grade values(80,89,’B’)
insert into grade values(70,79,’C’)
insert into grade values(60,69,’D’)
insert into grade values(0,59,’E’)

现查询所有同学的Sno、Cno和rank列。

#多表查询

代码语言:javascript
复制
select Sno,Cno,rank from Score,grade where Degree between low and upp;

19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。

#单表查询

#109同学,选修是3-105课的

代码语言:javascript
复制
select * from score where Cno='3-105' and degree>(select max(degree ) from Score where Sno='109' and Cno='3-105' );

#109同学,没有选修3-105课

代码语言:javascript
复制
select * from Score where Cno = '3-105' and Degree > (
select max(Degree) from Score where Sno ='109')and degree<( select max(degree ) from Score where sno in (select Sno from score group by Sno having count(*)>1));

#选了多门课程并且是这个课程下不是最高分的

代码语言:javascript
复制
select * from score a where Sno in (select Sno from score group by Sno having count(*)>1) and degree<( select max(degree ) from Score b where b.cno = a.cno);

21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。

#单表查询

代码语言:javascript
复制
select * from Score where Degree >(
select Degree from Score where Cno = '109' and Course = '3-105');

22、查询和学号为108、101的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

#单表查询

代码语言:javascript
复制
select Sno,Sname,Sbirthday from Student where year(Sbirthday) in (
select year(Sbirthday) from Student where Sno='108' or '101');

23、查询“张旭“教师任课的学生成绩。

#多表查询

#用where,内连接,子查询

代码语言:javascript
复制
select Sno,Degree from Score where Cno in (
select Cno from Course where Tno in(
select Tno from Teacher where Tname = '张旭'));

代码语言:javascript
复制
select Sno,Degree from Score,Course,Teacher where
Score.Cno=Course.Cno and Course.Tno=Teacher.Tno and Tname = '张旭' ;

代码语言:javascript
复制
select Sno,Degree from Score inner join Course on Score.Cno=Course.Cno inner join Teacher on Teacher.Tno=Course.Tno where Tname ='张旭' ;

24、查询选修某课程的同学人数多于5人的教师姓名。

#多表查询

#用where,内连接,子查询

代码语言:javascript
复制
select Tname from Teacher where Tno in (
select Tno from Course where Cno in (
select Cno from Score group by Cno having count(Sno)>5));

代码语言:javascript
复制
select Tname from Teacher,  Course where Teacher.Tno=Course.Tno and Course.Cno =(select Cno from Score group by Cno having count(*)>5);

25、查询95033班和95031班全体学生的记录。

#多表查询

#用where,内连接,子查询

代码语言:javascript
复制
select * from Student,Score,Course where Student.Sno=Score.Sno and Score.Cno=Course.Cno and class = '95033' or '95031';

26、 查询存在有85分以上成绩的课程Cno。

代码语言:javascript
复制
select Cno from Score where Degree >85;

27、查询出“计算机系“教师所教课程的成绩表。

#多表查询

#用where,内连接,子查询

代码语言:javascript
复制
select * from Score where Cno in (
select Cno from Course where Tno in (
select Tno from Teacher where Depart = '计算机系'));

#先找交集再not in

代码语言:javascript
复制
select Score.Sno,Score.Cno,Score.Degree from Score,Course,Teacher where Score.Cno=Course.Cno and Course.Tno=Teacher.Tno and Depart = '计算机系';

代码语言:javascript
复制
select Score.Sno,Score.Cno,Score.Degree from Score inner join Course on Score.Cno=Course.Cno inner join Teacher on Teacher.Tno =Course.Tno where Depart ='计算机系';

28、查询“计算 机系”与“电子工程系“不同职称的教师的Tname和Prof。

代码语言:javascript
复制
select Tname,Prof from Teacher where Prof not in (
select Prof from Teacher where Prof in (select Prof from Teacher where Depart = '计算机系') and Depart = '电子工程系');
代码语言:javascript
复制
select Tname,Prof from Teacher where Prof not in (
select Prof from Teacher where Depart = '计算机系' or Depart ='电子工程系' group by Prof having count(*)>1);

select Tname,Prof from Teacher where Depart ='计算机系' and Prof not in( select Prof from Teacher where Depart ='电子工程系')

union

select Tname,Prof from Teacher where Depart ='电子工程系' and Prof not in( select Prof from Teacher where Depart ='计算机系');

29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。

select Cno,Sno,Degree from Score where Cno = '3-105' and Degree >

any(select Degree from Score where Cno = '3-245') order by Degree desc;

select Cno,Sno,Degree from Score where Cno = '3-105' and Degree >

(select min(Degree) from Score where Cno = '3-245') order by Degree desc;

#大于任何一个或者大于最小的

30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree。

select Cno,Sno,Degree from Score where Cno = '3-105' and Degree > all(select Degree from Score where Cno = '3-245');

1

31、 查询所有教师和同学的name、sex和birthday。

select Sname as '学生',Ssex,Sbirthday from Student

union

select Tname as '老师',Tsex,Tbirthday from Teacher;

32、查询所有“女”教师和“女”同学的name、sex和birthday。

select Sname,Ssex,Sbirthday from Student where Ssex = '女'

union

select Tname,Tsex,Tbirthday from Teacher where Tsex = '女';

33、 查询成绩比该课程平均成绩低的同学的成绩表。

select * from score a where degree < (

select avg(degree) from score b where b.cno=a.cno);

这是一种特殊形式的父子表连接(自连接)SQL选择查询写法。对于这种特殊的写法,数据库引擎会以特殊的方式检索父查询表里的数据。如果搞不清楚这种特殊的检索方式,我们很难从该SQL语句的表面逻辑理出个中道理。

现在我们来分拆该SQL语句里的父查询和子查询

1)语句中的父查询

select * from score a where degree<”子查询获得的一个数据值“

2)语句中的子查询

select avg(degree) from score b where a.cno=b.cno

请注意这个子查询的from子句里只有一张表 b ,但是where子句里却出现了第二张表 a ,

如果单独运行这个子查询,因为子查询没有列出表a,系统会要求输入a.cno或者直接报错,反正无法顺利执行,但是表a可以在父查询里的from子句中找到,面对这种情况数据库引擎会采取逐条取主查询记录与子查询实施比对以确定是否检出该条记录,最后汇总各次检索的结果输出整个记录集。

这个特殊的SQL语句检索过程大致如下:

取出首条记录的a.cno用作过滤,子查询里以avg函数得到该课程的平均分,主查询以分数比对平均分,满足条件保留否则抛弃(degree小于平均分的留下);

跟着判断父查询表下一条记录,处理过程相同,最后合并各次判断结果从而的到最终结果。

这种特殊的写法可以规避输出包含非分组字段,而分组不得输出非分组字段的矛盾。

34、 查询所有任课教师的Tname和Depart。

select Tname,Depart from Teacher where Tno in (

select Tno from Course);

35 、查询所有未讲课的教师的Tname和Depart。

select Tname,Depart from Teacher where Tno not in (

select Tno from Course where Cno in (

select Cno from Score ));

36、查询至少有2名男生的班号。

select Class from Student where Ssex = '男'

group by Class

having count(Ssex) >1;

37、查询Student表中不姓“王”的同学记录。

select * from Student where Sname not like '王%%';

1

38、查询Student表中每个学生的姓名和年龄。

select Sname,year(now())-year(Sbirthday) from Student;

39、查询Student表中最大和最小的Sbirthday日期值。

select max(Sbirthday),min(Sbirthday) from Student;

40、以班号和年龄从大到小的顺序查询Student表中的全部记录。

select * from Student order by Class desc,Sbirthday;

41、查询“男”教师及其所上的课程。

#需要两个表的信息用where或者连接查询

select Cno,Cname,Tname from Course,Teacher where Course.Tno=Teacher.Tno and Tsex='男';

select Cno,Cname,Tname from Course inner join Teacher on Course.Tno=Teacher.Tno where Tsex='男';

42、查询最高分同学的Sno、Cno和Degree列。

select Sno,Cno,Degree from Score order by Degree desc limit 0,1;

select Sno,Cno,Degree from Score where Degree = (

select max(Degree) from Score);

43、查询和“李军”同性别的所有同学的Sname。

select Sname from Student where Ssex = (

select Ssex from Student where Sname = '李军');

44、查询和“李军”同性别并同班的同学Sname。

select Sname from Student where Ssex = (

select Ssex from Student where Sname = '李军')

and Class = (

select Class from Student where Sname = '李军');

45、查询所有选修“计算机导论”课程的“男”同学的成绩表。

select * from Score where Cno = (

select Cno from Course where Cname = '计算机导论') and

Sno in (select Sno from Student where Ssex = '男');

----------------------------------------------------------

1、查询“001”课程比“002”课程成绩高的所有学生的学号;

#自连接

select Sno from Score a

where Cno= '3-105' and

Degree > (select Degree from Score b where Cno='3-245'and a.Sno=b.Sno);

#输出两个表后连接比较

select a.Sno from

(select Sno,Degree from Score where Cno='3-105') a inner join

(select Sno,Degree from Score where Cno='3-245') b

on a.Sno=b.Sno

where a.Degree>b.Degree;

2、查询平均成绩大于60分的同学的学号和平均成绩;

select Sno,avg(Degree) from Score

group by Sno

having avg(Degree)>60;

3、查询所有同学的学号、姓名、选课数、总成绩;

select Score.Sno,Sname,count(*),sum(Degree) from Score,Student where Score.Sno=Student.Sno group by Score.Sno ;

select Score.Sno,Sname,count(*),sum(Degree) from Score inner join Student on Score.Sno=Student.Sno group by Sno;

4、查询姓“李”的老师的个数;

#还需要去重

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

5、查询没学过“叶平”老师课的同学的学号、姓名;

select Sno,Sname from Student where Sno not in (

select Sno from Score where Cno in (

select Cno from Course where Tno = (

select Tno from Teacher where Tname ='李诚')));

select Sno,Sname from Student where Sno not in(

select Score.Sno from Score inner join Course on Score.Cno=Course.Cno inner join Teacher on Course.Tno=Teacher.Tno where Tname='李诚');

6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

select Student.Sno,Sname from Student inner join Score on Student.Sno=Score.Sno where Cno='001' and Student.Sno in (select Student.Sno,Sname from Student inner join Score on Student.Sno=Score.Sno where Cno='002');

select Sno,Sname from Student where Sno in(

select Sno from Score where Cno = '001' and Sno in(

select Sno from Score where Cno='002'));

select Student.Sno,Student.Sname from Student,Score where Student.Sno=Score.Sno and Score.Sno='001' and exists( Select * from Score as SC_2 where SC_2.Sno=SC.Sno and SC_2.Sno='002');

#EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False。

7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;

select Sno,Sname from Student where Sno in (

select Sno from Score where Cno in(

select Cno from Course where Tno =(

select Tno from Teacher where Tname = '李诚'))

group by Sno

having count(*)=(select count(*) from Course,Teacher where Course.Tno=Teacher.Tno and Tname ='李诚')

);

select Student.Sno,Student.Sname from Student,Score,Course,Teacher where Student.Sno=Score.Sno

and Score.Cno=Course.Cno and Course.Tno = Teacher.Tno and Teacher.Tname='李诚'

group by Sno

having count(*) = (select count(*) from Course,Teacher where Course.Tno= Teacher.Tno and Teacher.Tname= '李诚');

8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;

select Sno,Sname from Student where Sno in (

select Sno from Score a2 where Cno='002' and Degree>

(select Degree from Score a1 where Cno='001' and a1.Sno=a2.Sno));

Select Sno,Sname from (select Student.Sno,Student.Sname,Degree,(select score from Score SC_2 where SC_2.Sno=Student.Sno and SC_2.Cno='002') Degree2 from Student,Score where Student.Sno=Score.Sno and Cno ='001') S_2 where Degree2 <Degree;

9、查询所有课程成绩小于60分的同学的学号、姓名;

#注意关键词所有课程,可以采用逆向思维

select Sno,Sname from Student where Sno not in (

select Sno from Score where Degree>60);

10、查询没有学全所有课的同学的学号、姓名;

select Sno,Sname from Student where Sno not in (

select Sno from Score group by Sno

having count(*) = (

select count(distinct(Cno)) from Score));

select Sno,Sname from Student where Sno not in (

select Sno from Score group by Sno

having count(*) = (

select count(*) from Course));

select Student.Sno,Student.Sname

from Student,Score

where Student.Sno=Score.Sno group by Student.Sno,Student.Sname

having count(Cno) <(select count(Cno) from Course);

11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;

select Sno,Sname from Student where Sno in (

select Sno from Score where Cno in (

select Cno from Score where Sno = '103'));

12、查询与“1001”同学所学课程相同的其他同学学号和姓名;

#自己做的待考证

select Sno,Sname from Student where Sno in(

select Sno from Score a where Cno in (select Cno from Score where Sno='103')

group by Sno

having count(*) = (select count(*) from Score where Sno='103')

);

13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;

#这个方法出不来

update Score a set Degree = (select avg(Degree) from Score b group by Cno where a.Cno=b.Cno)

where a.Cno in (select Cno from Course,Teacher where Teacher.Tno = Course.Tno and Tname='李诚'));

#网上的方法是

update SC set score=(select avg(SC_2.score)

from SC SC_2

where SC_2.C#=SC.C# ) from Course,Teacher where Course.C#=SC.C# and Course.T#=Teacher.T# and Teacher.Tname='叶平');

update Score inner join (select Cno,avg(Degree) avgDegree from Score where Cno in

(select Cno from Course where Tno = (select Tno from Teacher where Tname = '李诚')) group by Cno) a

on Score.Cno=a.Cno

Set Score.degree = a.avgDegree;

14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;

#有可能学了同样数目但是类型不同的课程,所以用not in

select Sno,Sname from Student where Sno in (

select Sno from Score where Sno not in(

select Sno from Score where Cno not in (select Cno from Score where Sno = '103'))

group by Sno

having count(Cno) = (select count(Cno) from Score where Sno = '103'));

15、删除学习“叶平”老师课的SC表记录;

delete from Score where Cno = (

select Cno from Course where Tno = (

select Tno from Teacher where Tname='叶平'));

16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、’002’号课的平均成绩;

insert into Score (Sno,Cno,Degree)

values

(select Sno,002,(select avg(Degree) from Score where Cno ='002' ) from Student where Sno not in (select Sno from Score where Cno = '003'));

17、按平均成绩从高到低显示所有学生的“JAVA”、“C#”、“C++”三门的课程成绩,按如下形式显示: 学生ID,,C#,C++,JAVA,有效课程数,有效平均分。

select Sno as '学生ID',

(select Degree from Score a where Cno =(select Cno from Course where Cname='C#') and a.Sno=SC.Sno) as 'C#',

(select Degree from Score b where Cno =(select Cno from Course where Cname='C++') and b.Sno=SC.Sno) as 'C++',

(select Degree from Score c where Cno =(select Cno from Course where Cname='JAVA') and c.Sno=SC.Sno) as 'JAVA',

count(*) as '有效课程数',

avg(Degree) as '有效平均分'

from Score SC group by Sno

order by avg(Degree) desc;

18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;

#自己写的

select Cno as '课程ID',max(Degree) as '最高分',min(Degree) as '最低分' from Score group by Cno;

#网上的

SELECT L.Cno As 课程ID,L.score AS 最高分,R.score AS 最低分

FROM SC L ,SC AS R

WHERE L.Cno = R.Cno and

L.Degree = (SELECT MAX(IL.Degree)

FROM SC AS IL,Student AS IM

WHERE L.Cno = IL.Cno and IM.Sno=IL.Sno

GROUP BY IL.Cno)

AND

R.Score = (SELECT MIN(IR.Degree)

FROM SC AS IR

WHERE R.Cno = IR.Cno

GROUP BY IR.Cno

);

19、按各科平均成绩从低到高和及格率的百分数从高到低顺序 ;

#自己写的

select * from Score a order by

(select avg(Degree) from Score b where a.Cno=b.Cno group by Cno) asc,

(select count(*) from Score c where Degree > 60 and a.Cno=c.Cno group by Cno)/(select count(*) from Score d where a.Cno=d.Cno group by Cno) desc;

#网上的

SELECT t.C# AS 课程号,max(course.Cname)AS 课程名,isnull(AVG(score),0) AS平均成绩,100 * SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) AS 及格百分数

FROM SC T,Course

where t.C#=course.C#

GROUP BY t.C#

ORDER BY 100* SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) DESC

20、查询如下课程平均成绩和及格率的百分数(用”1行”显示): 企业管理(001),马克思(002),OO&UML (003),数据库(004)

SELECT SUM(CASE WHEN C# ='001' THEN score ELSE 0 END)/SUM(CASE C# WHEN '001' THEN 1 ELSE 0 END) AS 企业管理平均分,

100 * SUM(CASE WHEN C# = '001' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = '001' THEN 1 ELSE 0 END) AS 企业管理及格百分数,

SUM(CASE WHEN C# = '002' THEN score ELSE 0 END)/SUM(CASE C# WHEN '002' THEN 1 ELSE 0 END) AS 马克思平均分,

100 * SUM(CASE WHEN C# = '002' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = '002' THEN 1 ELSE 0 END) AS 马克思及格百分数,

SUM(CASE WHEN C# = '003' THEN score ELSE 0 END)/SUM(CASE C# WHEN '003' THEN 1 ELSE 0 END) AS UML平均分,

100* SUM(CASE WHEN C# = '003' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = '003' THEN 1 ELSE 0 END) AS UML及格百分数,

SUM(CASE WHEN C# = '004' THEN score ELSE 0 END)/SUM(CASE C# WHEN '004' THEN 1 ELSE 0 END) AS 数据库平均分,

100 * SUM(CASE WHEN C# = '004' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = '004' THEN 1 ELSE 0 END) AS 数据库及格百分数

FROM SC;

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

SELECT max(Z.T#) AS 教师ID,MAX(Z.Tname) AS 教师姓名,C.C# AS 课程ID,MAX(C.Cname) AS 课程名称,AVG(Score) AS 平均成绩

FROM SC AS T,Course AS C ,Teacher AS Z

where T.C#=C.C# and C.T#=Z.T#

GROUP BY C.C#

ORDER BY AVG(Score) DESC

22、查询如下课程成绩第 3 名到第 6 名的学生成绩单:企业管理(001),马克思(002),UML (003),数据库(004) [学生ID],[学生姓名],企业管理,马克思,UML,数据库,平均成绩

SELECT DISTINCT top 3

SC.S# As 学生学号,

Student.Sname AS 学生姓名 ,

T1.score AS 企业管理,

T2.score AS 马克思,

T3.score AS UML,

T4.score AS 数据库,

ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) as 总分

FROM Student,SC LEFT JOIN SC AS T1

ON SC.S# = T1.S# AND T1.C# = '001'

LEFT JOIN SC AS T2

ON SC.S# = T2.S# AND T2.C# = '002'

LEFT JOIN SC AS T3

ON SC.S# = T3.S# AND T3.C# = '003'

LEFT JOIN SC AS T4

ON SC.S# = T4.S# AND T4.C# = '004'

WHERE student.S#=SC.S# and

ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0)

NOT IN

(SELECT

DISTINCT

TOP 15 WITH TIES

ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0)

FROM sc

LEFT JOIN sc AS T1

ON sc.S# = T1.S# AND T1.C# = 'k1'

LEFT JOIN sc AS T2

ON sc.S# = T2.S# AND T2.C# = 'k2'

LEFT JOIN sc AS T3

ON sc.S# = T3.S# AND T3.C# = 'k3'

LEFT JOIN sc AS T4

ON sc.S# = T4.S# AND T4.C# = 'k4'

ORDER BY ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) DESC);

23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]

SELECT SC.C# as 课程ID, Cname as 课程名称

,SUM(CASE WHEN score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS [100 - 85]

,SUM(CASE WHEN score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS [85 - 70]

,SUM(CASE WHEN score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS [70 - 60]

,SUM(CASE WHEN score < 60 THEN 1 ELSE 0 END) AS [60 -]

FROM SC,Course

where SC.C#=Course.C#

GROUP BY SC.C#,Cname;

24、查询学生平均成绩及其名次

SELECT 1+(SELECT COUNT( distinct 平均成绩)

FROM (SELECT S#,AVG(score) AS 平均成绩

FROM SC

GROUP BY S#

) AS T1

WHERE 平均成绩 > T2.平均成绩) as 名次,

S# as 学生学号,平均成绩

FROM (SELECT S#,AVG(score) 平均成绩

FROM SC

GROUP BY S#

) AS T2

ORDER BY 平均成绩 desc;

25、查询各科成绩前三名的记录:(不考虑成绩并列情况)

SELECT t1.S# as 学生ID,t1.C# as 课程ID,Score as 分数

FROM SC t1

WHERE score IN (SELECT TOP 3 score

FROM SC

WHERE t1.C#= C#

ORDER BY score DESC

)

ORDER BY t1.C#;

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

select c#,count(S#) from sc group by C#;

27、查询出只选修了一门课程的全部学生的学号和姓名

select SC.S#,Student.Sname,count(C#) AS 选课数

from SC ,Student

where SC.S#=Student.S# group by SC.S# ,Student.Sname having count(C#)=1;

28、查询男生、女生人数

Select count(Ssex) as 男生人数 from Student group by Ssex having Ssex='男';

Select count(Ssex) as 女生人数 from Student group by Ssex having Ssex='女';

29、查询姓“张”的学生名单

SELECT Sname FROM Student WHERE Sname like '张%';

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

select Sname,count(*) from Student group by Sname having count(*)>1;

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

select Sname, CONVERT(char (11),DATEPART(year,Sage)) as age

from student

where CONVERT(char(11),DATEPART(year,Sage))='1981';

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

Select C#,Avg(score) from SC group by C# order by Avg(score),C# DESC ;

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

select Sname,SC.S# ,avg(score)

from Student,SC

where Student.S#=SC.S# group by SC.S#,Sname having avg(score)>85;

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

Select Sname,isnull(score,0)

from Student,SC,Course

where SC.S#=Student.S# and SC.C#=Course.C# and Course.Cname='数据库'and score <60;

35、查询所有学生的选课情况;

SELECT SC.S#,SC.C#,Sname,Cname

FROM SC,Student,Course

where SC.S#=Student.S# and SC.C#=Course.C# ;

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

SELECT distinct student.S#,student.Sname,SC.C#,SC.score

FROM student,Sc

WHERE SC.score>=70 AND SC.S#=student.S#;

37、查询不及格的课程,并按课程号从大到小排列

select c# from sc where scor e <60 order by C# ;

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

select SC.S#,Student.Sname from SC,Student where SC.S#=Student.S# and Score>80 and C#='003';

39、求选了课程的学生人数

select count(*) from sc;

40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩

select Student.Sname,score

from Student,SC,Course C,Teacher

where Student.S#=SC.S# and SC.C#=C.C# and C.T#=Teacher.T# and Teacher.Tname='叶平' and SC.score=(select max(score)from SC where C#=C.C# );

41、查询各个课程及相应的选修人数

select count(*) from sc group by C#;

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

select distinct A.S#,B.score from SC A ,SC B where A.Score=B.Score and A.C# <>B.C# ;

43、查询每门功成绩最好的前两名

SELECT t1.S# as 学生ID,t1.C# as 课程ID,Score as 分数

FROM SC t1

WHERE score IN (SELECT TOP 2 score

FROM SC

WHERE t1.C#= C#

ORDER BY score DESC

)

ORDER BY t1.C#;

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

select C# as 课程号,count(*) as 人数

from sc

group by C#

order by count(*) desc,c#

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

select S#

from sc

group by s#

having count(*) > = 2

46、查询全部学生都选修的课程的课程号和课程名

select C#,Cname

from Course

where C# in (select c# from sc group by c#)

47、查询没学过“叶平”老师讲授的任一门课程的学生姓名

select Sname from Student where S# not in (select S# from Course,Teacher,SC where Course.T#=Teacher.T# and SC.C#=course.C# and Tname='叶平');

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

select S#,avg(isnull(score,0)) from SC where S# in (select S# from SC where score <60 group by S# having count(*)>2)group by S#;

49、检索“004”课程分数小于60,按分数降序排列的同学学号

select S# from SC where C#='004'and score <60 order by score desc;

50、删除“002”同学的“001”课程的成绩

delete from Sc where S#='001'and C#='001';

---------------------

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档