Mysql编程练习之二

Mysql编程练习之二

2.查询教师所有的单位,即不重复的depart列
select depart
from teacher
group by depart;

注://下面这个SQL是错误的
select *
from teacher
group by depart;
1.去除重复列
select distinct depart
from teacher;
5.查询student表中95031班或者性别为女的同学记录
select *
from student
where class=95031 or ssex = ‘女’;

7.以Cno升序、Degree降序查询Score表的所有记录。
错误的写法
select *
from score
order by cno asc and degree desc;(and多余)

正确的写法
select *
from score
order by cno asc , degree desc;

8.查询“95031”班的学生人数。
这里需要注意的是,count(*)是对单一字段的count,而非所有字段的count。
select count(*)
from student
where class = ‘95031’;

10.查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
1.使用子查询
select sno,cno
from score
where degree in(
select max(degree) min(degreee)
from score);

2.使用排序

11、 查询每门课的平均成绩。
select cno,avg(degree)
from score
group by cno;

12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select avg(degree)
from score
where 4<(sleect count(*) from score group by cno)
and cno like ‘%3’;

select cno,avg(degree)
from score
where cno like ‘%3’ and cno in
(select cno
from score
group by cno
having count(*)>=3);

select cno,avg(Degree)
from score
where Cno like ‘3%’ and Cno in
(select Cno from score group by Cno having count(*)>=5);
用in 不用= 是因为可能会有多个

having的使用!!
13、查询分数大于70,小于90的Sno列。
select sno,degree
from score
where grade > 70 and score<90;

14、查询所有学生的Sname、Cno和Degree列。
select sname,cno,degree
from score,student
where student.sno = score.sno;
可是上面的运行结果如下:
就是按照cno来分组了,而非是按照是sname分组了

15、 查询所有学生的Sno、Cname和Degree列。
select Sno,Cname,Degree
from Score,Course
where Score.Cno=Course.Cno;

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

select Sname,Cname,Degree
from student,course,score
where student.Sno=score.Sno and course.Cno=score.Cno;
如何理解where语句?

select Sname,Cname,Degree
from student
join score on student.Sno=score.Sno
join course on course.Cno=score.Cno;

17、查询“95033”班学生的平均分。
select sno,avg(degree)
from score
where sno in (
select sno
from student
where class = ‘95031’);

上下两个都正确

select avg(degree) as ‘class=95031’
from Score
where Sno in (select Sno from Student where Class=’95031’ );

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

create table grade(low int(3),upp int(3),rank char(1));
insert into grade values(90,100,’A’),
(80,89,’B’),
(70,79,’C’),
(60,69,’D’),
(0,59,’E’);

现查询所有同学的Sno、Cno和rank列。
select Sno,Cno,rank from Score,grade
where degree between low and upp;

不理解为什么直接可以between low and upp?
这里sno和cno,degree均是score中的表
rank 是grade中的表

19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
select student.sno,sname
from student,course,score
where score.cno=’3-105’ and score.degree >(
select max(degree)
from score
where score.cno = ‘3-105’ and score.sno = 109);

1.Unknown column ‘cno’ in ‘where clause’的错误是因为:在student表中,没有
cno字段
2.写完之后,应该思考一下语法结构是否正确,是否有冗余等

select * from score
where Cno=’3-105’ and degree>
(select max(degree )
from Score
where Sno=’109’ and Cno=’3-105’ );

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

猜你喜欢

转载自blog.csdn.net/liu16659/article/details/81189015