MySQL经典45题

一、 数据准备

在这里插入图片描述

1、课程表course
在这里插入图片描述
2、成绩表sc(部分数据)
在这里插入图片描述
3、学生表student(部分数据)
在这里插入图片描述
4、教师表teacher
在这里插入图片描述

二、题目讲解

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

select *
from student as a
right join sc as b
on a.sid=b.sid

inner join sc as c
on a.sid=c.sid and b.cid=01 and c.cid=02
where b.score>c.score;

在这里插入图片描述

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

select * 
from sc as a
inner join sc as b
on a.sid=b.sid and a.cid='01' and b.cid='02';

在这里插入图片描述

1.2查询存在01课程但可能不存在02课程的情况(不存在时显示为null)

select * 
from sc as a
left join sc as b
on a.sid=b.sid and b.cid='02' 
where a.cid = '01';

在这里插入图片描述

1.3、查询不存在01课程,存在02课程的情况

select * 
from sc
where sid not in (select sid from sc where cid='01')
and cid ='02';

在这里插入图片描述

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

select a.sid,a.sname,avg(b.score) as s_avg
from student as a
right join sc as b
on a.sid=b.sid
group by b.sid
having s_avg>60;

在这里插入图片描述

3、查询在SC表存在成绩的学生信息

select a.*
from student as a
right join sc as b
on a.sid=b.sid
group by b.sid;

在这里插入图片描述

4、查询所有同学的学生编号、学生姓名、选课总数、所有课程总成绩(没成绩的显示为null)

select a.sid,a.sname,count(b.cid) as c_sum,sum(b.score) as s_sum
from student as a
left join sc as b
on a.sid=b.sid
group by a.sid;

在这里插入图片描述

4.1、查询有成绩的学生信息

#法1
select * from student
where sid in(select distinct sid from sc);
#法2
select * from student
where sid in(select sid from sc group by sid);
#法3
select a.* 
from student as a
inner join sc as b
on a.sid=b.sid
group by a.sid;

在这里插入图片描述

5、查看李老师的数量

select count(*) 
from teacher
where tname like '李%';

在这里插入图片描述

6、查询学过张三老师的课的学生信息

select student.* 
from teacher,course,sc,student
where teacher.tname = '张三'
and teacher.tid=course.tid
and course.cid=sc.cid
and sc.sid=student.sid;

在这里插入图片描述

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

select a.*,count(cid) as c_count
from student as a
inner join sc as b
on a.sid=b.sid
group by a.sid
having c_count<(select count(cid) from course);

在这里插入图片描述

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

#法1
select cid from sc where sid = '01';
select * from student where sid in(
select distinct sid from sc 
where cid in(select cid from sc where sid = '01'));

#法2
select distinct a.* 
from student as a
inner join sc as b
on a.sid=b.sid
where cid in(select cid from sc where sid = '01');

在这里插入图片描述

9、查询与学号为01的同学所学课程完全相同的其他同学信息

select * from student where sid in(
select a.sid from (select sid,group_concat(cid order by cid) as courses 
from sc group by sid) as a
inner join (select sid,group_concat(cid order by cid) as courses 
from sc group by sid having sid='01') as b
on a.sid != 1 and a.courses = b.courses);

在这里插入图片描述

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

select distinct sname from student where sid not in(
select sid from sc where cid in(
select a.cid from course as a
inner join teacher as b
on a.tid=b.tid
where b.tname = '张三' ));

在这里插入图片描述

  • 5
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MySQL是一种关系型数据库管理系统,常用于Web应用程序的后台数据管理。以下是MySQL的50道经典面试: 1. 什么是MySQL? 2. MySQL的优点是什么? 3. MySQL的数据类型有哪些? 4. MySQL中的存储引擎有哪些? 5. 什么是索引?MySQL中的索引有哪些类型? 6. 什么是主键?如何设置主键? 7. 什么是外键?如何设置外键? 8. 什么是事务?MySQL中如何使用事务? 9. 什么是视图?MySQL中如何创建视图? 10. 什么是存储过程?MySQL中如何创建存储过程? 11. 什么是触发器?MySQL中如何创建触发器? 12. 什么是游标?MySQL中如何使用游标? 13. 什么是连接?MySQL中如何进行连接操作? 14. 什么是子查询MySQL中如何使用子查询? 15. 什么是临时表?MySQL中如何创建临时表? 16. 什么是备份?MySQL中如何进行备份操作? 17. 什么是恢复?MySQL中如何进行恢复操作? 18. 什么是复制?MySQL中如何进行复制操作? 19. 什么是分区表?MySQL中如何创建分区表? 20. 什么是分布式数据库?MySQL中如何实现分布式数据库? 21. 什么是索引覆盖?MySQL中如何实现索引覆盖? 22. 什么是慢查询MySQL中如何优化慢查询? 23. 什么是死锁?MySQL中如何避免死锁? 24. 什么是优化器?MySQL中如何使用优化器? 25. 什么是explain?MySQL中如何使用explain? 26. 什么是锁?MySQL中有哪些锁? 27. 什么是MyISAM?MySQL中如何使用MyISAM? 28. 什么是InnoDB?MySQL中如何使用InnoDB? 29. 什么是Memory?MySQL中如何使用Memory? 30. 什么是CSV?MySQL中如何使用CSV? 31. 什么是Blackhole?MySQL中如何使用Blackhole? 32. 什么是Federated?MySQL中如何使用Federated? 33. 什么是Merge?MySQL中如何使用Merge? 34. 什么是Archive?MySQL中如何使用Archive? 35. 什么是NDB?MySQL中如何使用NDB? 36. 什么是Cluster?MySQL中如何使用Cluster? 37. MySQL是如何保证主备同步的? 38. 什么是binlog?MySQL中如何使用binlog? 39. 什么是redo log?MySQL中如何使用redo log? 40. 什么是undo log?MySQL中如何使用undo log? 41. 什么是事务日志?MySQL中如何使用事务日志? 42. 什么是XA事务?MySQL中如何使用XA事务? 43. 什么是XA协议?MySQL中如何使用XA协议? 44. 什么是XA事务管理器?MySQL中如何使用XA事务管理器? 45. 什么是XA资源管理器?MySQL中如何使用XA资源管理器? 46. 什么是XA事务的两阶段提交?MySQL中如何使用XA事务的两阶段提交? 47. 什么是MySQL的锁机制?MySQL中有哪些锁? 48. 什么是表锁?MySQL中如何使用表锁? 49. 什么是行锁?MySQL中如何使用行锁? 50. 什么是间隙锁?MySQL中如何使用间隙锁?

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值