java笔试题练习---数据库 - Go语言中文社区

java笔试题练习---数据库


表一:student 学生表
在这里插入图片描述
表二:teacher 教师表
在这里插入图片描述
表三 : coures 课程表
在这里插入图片描述
表四 :sc 成绩表
在这里插入图片描述
1. 查询“1”课程比“2”课程成绩高的所有学生的学号;
select a.sid from (select sid,score from sc where cid=‘1’) as a,(select sid,score
from sc where cid=‘2’) as b where a.score>b.score and a.sid=b.sid;

2. 查询平均成绩大于60分的同学的学号和平均成绩;
select sid,avg(score) from sc group by sid having avg(score) >60;

重点:group by 分组。必须有“聚合函数”来配合才能使用,使用时至少需要一个分组标志字段。
order by 行的排序方式,默认的为升序。 order by 后面必须列出排序的字段名,可以是多个字段名
having 通常与 group by 语句联合使用,用来过滤由group by语句返回的记录集。having语句的存在弥补了where关键字不能与聚合函数联合使用的不足

3. 查询所有同学的学号、姓名、选课数、总成绩;
select student.sid ,student.sname,sum(score) ,COUNT(sc.cid) from student left Outer join sc on student.sid=sc.sid
group by student.sid,sname

表连接:inner join、outer join、right join、left join
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录
  right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录
  inner join(等值连接) 只返回两个表中联结字段相等的行
   outer join(外连接) 可分为左外连接left outer join和右外连接right outer join

4.查询姓“李”的老师的个数;
select count(distinct(tname)) from teather where tname like “李%”
distinct :去重

5. 查询没学过“叶平”老师课的同学的学号、姓名;
select student.sid ,student.sname from student where sid not in (select sc.sid from SC,Course,Teacher where SC.cid=Course.Cid and Teacher.tid=Course.tid and Teacher.Tname=‘叶平’)

6. 查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
select sid ,sname from student where sid in( select a.sid from(select sid from sc where cid=“1”)as a ,(select sid from sc where cid=“2” )as b where a.sid = b.sid)
或者
select Student.Sid,Student.Sname from Student,SC where Student.sid=SC.sid and SC.Cid='1’and exists( Select * from SC as SC_2 where SC_2.Sid=SC.Sid and SC_2.Cid=‘2’);

mysql exists 和 in的效率比较 :
这条语句适用于a表比b表大的情况
select * from ecs_goods a where cat_id in(select cat_id from ecs_category);
这条语句适用于b表比a表大的情况
select * from ecs_goods a where EXISTS(select cat_id from ecs_category b where a.cat_id = b.cat_id);

7. 查询学过“叶平”老师所教的所有课的同学的学号、姓名;
select student.sid , student.sname from student ,sc ,teacher
where teacher.name= “叶平” ,teacher.tid=coures.tid and coures.cid=sc.cid
select cid from coures ,teacher where teacher.tid=coures.tid

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

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

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

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_41219586/article/details/104703909
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-04-19 13:29:38
  • 阅读 ( 1099 )
  • 分类:数据库

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢