MySQL基础练习2

查询操作练习

注意:查询中所涉及到的表格请查看MySQL基础练习1

1.以下题目仅需提供一种sql语句

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

2.查询最低分大于70,最高分小于90的Sno列。

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

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

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

6.查询“95033”班所选课程的平均分。

2.以下题目均需提供两种sql语句

1.查询选修课成绩为A等的学生信息。

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

3.查询grade中选学一门以上课程的同学中分数为非最高分成绩的记录。

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

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

6.查询“张旭“教师任课的学生成绩。

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

8.查询95033班和95031班全体学生的记录。

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

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

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

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

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

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

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

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

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

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

19.查询至少有2名男生的班号。

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

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

22.查询Student表中最大和最小的Sbirthday日期值。

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

24.查询“男”教师及其所上的课程。

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

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

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

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

29.查询计算机系教师所教课程成绩为B等的课程信息。

30.查询成绩在C等以上的学生的所在班级。

31.查询班里B等以上学生人数最多的班级信息。

参考答案

1.以下题目仅需提供一种sql语句

1.语句如下,

select cno,count(sno) num,avg(degree) from grade group by cno having num >= 5;

2.语句如下,

select sno from grade where degree between 70 and 90;

3.语句如下,

select s.sno,g.cno,g.degree from student s,grade g where s.sno=g.sno;

4.语句如下,

select g.sno,c.cname,g.degree from grade g,course c where g.cno=c.cno;

5.语句如下,

select s.sname,c.cname,g.degree from student s,course c,grade g where s.sno=g.sno and g.cno=c.cno;

6.语句如下,

select a.class,avg(degree) from (select s.class,g.degree from student s,grade g where s.sno=g.sno) a group by a.class having class='95033';

2.以下题目均需提供两种sql语句

1.语句如下,

select * from student s,grade g where degree between (select down from rank where rank='A') and (select up from rank where rank='A') and s.sno=g.sno;

或者,

select * from student s left join grade g on s.sno=g.sno where degree between (select down from rank where rank='A') and (select up from rank where rank='A');

2.语句如下,

select distinct s.* from student s,grade g where s.sno=g.sno and degree>(select degree from grade where sno='109' and cno='3-105');

或者,

select distinct s.* from student s left join grade g on s.sno=g.sno where degree>(select degree from grade where sno='109' and cno='3-105');

3.语句如下,

select * from grade where degree not in (select max(degree) from grade group by sno having count(cno)>1);

或者,

select * from grade where degree not in (select max(degree) from grade group by sno having count(cno)>1) group by degree;

4.语句如下,

select s.* from student s,grade g where s.sno=g.sno and degree>(select degree from grade where sno='109' and cno='3-105') and g.cno='3-105';

或者,

select s.* from student s left join grade g on s.sno=g.sno where degree>(select degree from grade where sno='109' and cno='3-105') and g.cno='3-105';

5.语句如下,

select sno,sname,sbirthday from student where year(sbirthday) <> (select year(sbirthday) from student where sno='109') and sno<>'109';

6.语句如下,

select g.sno,g.degree from grade g,course c,teacher t where g.cno=c.cno and c.tno=t.tno and t.tname='张旭';

或者,

select g.sno,g.degree from grade g left join (select c.*,t.tname from course c left join teacher t on c.tno=t.tno) tmp on g.cno=tmp.cno where tmp.tname='张旭';

7.语句如下,

select t.tname from teacher t,course c where t.tno=c.tno and c.cno in (select cno from grade group by cno having count(sno)>5);

或者,

select t.tname from teacher t left join course c on t.tno=c.tno where c.cno in (select cno from grade group by cno having count(sno)>5);

8.语句如下,

select * from student where class='95033' or class='95031' order by class;

9.语句如下,

select distinct cno from grade where degree>85;

10.语句如下,

select g.* from grade g,course c,teacher t where g.cno=c.cno and c.tno=t.tno and t.depart='计算机系';

或者,

select g.* from grade g left join (select c.cno,t.depart from course c left join teacher t on c.tno=t.tno) tmp on g.cno=tmp.cno where tmp.depart='计算机系';

11.语句如下,

select a.tname,a.prof,b.tname,b.prof from teacher a,teacher b where a.prof<>b.prof;

或者,

select a.tname,a.prof,b.tname,b.prof from teacher a left join teacher b on a.prof<>b.prof;

12.语句如下,

select a.* from grade a left join grade b on a.sno=b.sno where a.cno='3-105' and b.cno='3-245' and a.degree>b.degree order by degree desc;

或者,

select a.* from grade a,grade b where a.sno=b.sno and a.cno='3-105' and b.cno='3-245' and a.degree>b.degree order by degree desc;

13.语句如下,

select a.* from grade a left join grade b on a.sno=b.sno where a.cno='3-105' and b.cno='3-245' and a.degree>b.degree;

或者,

select a.* from grade a,grade b where a.sno=b.sno and a.cno='3-105' and b.cno='3-245' and a.degree>b.degree;

14.语句如下,

select tname,tsex,tbirthday from teacher;
select sname,ssex,sbirthday from student;

或者,

select sname name,ssex sex,sbirthday birthday from student
union
select tname,tsex,tbirthday from teacher;

15.语句如下,

select sname,ssex,sbirthday from student where ssex='女';
select tname,tsex,tbirthday from teacher where tsex='女';

或者,

select sname name,ssex sex,sbirthday birthday from student where ssex='女'
union
select tname,tsex,tbirthday from teacher where tsex='女';

16.语句如下,

select g.* from grade g,(select cno,avg(degree) avg from grade group by cno) tmp where g.cno=tmp.cno and degree<tmp.avg;

或者,

select g.* from grade g left join(select cno,avg(degree) avg from grade group by cno) tmp on g.cno=tmp.cno where degree<tmp.avg;

17.语句如下,

select t.* from teacher t,course c where t.tno=c.tno;

或者,

select t.* from course c left join teacher t on c.tno=t.tno where t.tno is not null;

18.语句如下,

select distinct t.* from teacher t,course c where t.tno not in (select t.tno from teacher t,course c where t.tno=c.tno);

或者,

select * from teacher t left join course c on t.tno=c.tno where c.cno is null;

19.语句如下,

select class from student where ssex='男' group by class having count(ssex)>1;

20.语句如下,

select * from student where sname not in (select sname from student where sname like '王%');

21.语句如下,

select sname,2020-year(sbirthday) age from student;

22.语句如下,

select max(sbirthday),min(sbirthday) from student;

23.语句如下,

select * from student order by class desc,2020-year(sbirthday) desc;

24.语句如下,

select c.cno,c.cname from course c,teacher t where c.tno=t.tno and t.tsex='男';

或者,

select c.cno,c.cname from course c left join teacher t on c.tno=t.tno where t.tsex='男';

25.语句如下,

select * from grade where degree=(select max(degree) from grade);

26.语句如下,

select sname from student where ssex=(select ssex from student where sname='李军') and sname<>'李军';

27.语句如下,

select sname from student where ssex=(select ssex from student where sname='李军') and class=(select class from student where sname='李军') and sname<>'李军';

28.语句如下,

select g.* from grade g,student s,course c where g.sno=s.sno and g.cno=c.cno and ssex='男' and c.cname='计算机导论';

或者,

select tmp.sno,tmp.cno,tmp.degree from student s left join (select g.*,c.cname from grade g left join course c on g.cno=c.cno) tmp on s.sno=tmp.sno where tmp.cname='计算机导论' and s.ssex='男';

29.语句如下,

select c.* from grade g,course c,teacher t where g.cno=c.cno and c.tno=t.tno and t.depart='计算机系' and g.degree between (select down from rank where rank='B') and (select up from rank where rank='B');

或者,

select tmp.cno,tmp.cname,tmp.tno from grade g left join (select c.*,t.depart from course c left join teacher t on c.tno=t.tno) tmp on g.cno=tmp.cno where tmp.depart='计算机系' and g.degree between (select down from rank where rank='B') and (select up from rank where rank='B');

30.语句如下,

select distinct s.class from student s,grade g where g.degree>(select up from rank where rank='C');

或者,

select distinct s.class from student s left join grade g on s.sno=g.sno where g.degree>(select up from rank where rank='C');

31.语句如下,

select s.class from student s,grade g where s.sno=g.sno and g.degree>(select up from rank where rank='B') group by class having count(s.sno)=(select distinct count(s.sno) from student s,grade g where s.sno=g.sno and g.degree>(select up from rank where rank='B') group by class);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值