SQL 50题刷题记录

简介: SQL 50题刷题记录

1685019034780.jpg

上诉为数据表

1685019048083.jpg

答:

select * from student right join (
select t1.sid,class1,class2 from
(select sid,score as class1 from sc where cid=‘01’)as t1,
(select sid,score as class2 from sc where cid=‘02’)as t2
where t1.sid=t2.sid and t1.class1>t2.class2
)r
on student.sid=r.sid;

学习到的知识:

1.连接,外连接,左右连接,左右外连接

2.如何用查询建立表


1.1

select * from (select * from sc where sc.cid=‘01’)t1,
(select * from sc where sc.cid=‘02’)t2
where t1.sid=t2.sid;

1.2

明显要外连接,由题意是左外连接

select * from (select * from sc where sc.cid='01)t1 left join
(select * from sc where sc.cid=‘02’)t2
on t1.sid=t2.sid;


1.3

select * from sc where sid not in(
select sid from sc where cid=‘01’);


2

select student.sid,sname,avgscore from student,(
select sid,avg(score) as avgscore from sc group by sid)t1
where student.sid=t1.sid and avgscore>60;
select student.sid,sname,avgscore from student,(
select sid,avg(score) as avgscore
from sc group by sid
having avg(score)>60)t1
where student.sid=t1.sid;

学习知识点:

having 语句的筛选


3.

select * from student where sid in(select distinct sid from sc where score is not null);

在sc表中,score非主属性,可能为空


4.明显需要外连接


select student.sid,sname,num,totals from student left join
(select sid,sum(score) as totals,count (sid) as num from sc
group by sid)t1
on student.sid=t1.sid;

学习知识点:

count 对元组个数的统计


4.1

select * from student where sid in(select distinct sid from sc );
select * from student where exists(select * from sc where sc.sid=student.sid);

学习知识点:

exist 和 in 的效率 ,exist的返回值只是真假


5

select count(tname)from teacher where tname like ‘李%’;
select * from student where student.sid in(
select sc.sid from sc where sc.cid=(select course.cid from course,teacher where course.tid=teacher.cid and tname=‘张三’)
);

多表联合查询

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

学习知识点:

多表联合查询的应用

1685019159076.jpg


答:

7.

select * from student where student.sid not in(
select sc.sid from sc group by sc.sid having count (sc.sid)=(
select count() from course));

8.

select distinct student. from student,sc where student.sid=sc.sid and cid in(
select cid from sc where sid=‘01’)
);

9、

select * from student where sid in(
select sid from sc where cid in(select cid from sc where sid=‘01’) and sid !=‘01’ group by sid having count(cid) = (select count(cid) from sc where sid = ‘01’ group by sid) )
);

cid在01中且课程数相同

学习知识点:

怎么间接表示=all

10、

select sname from student where sid not in (
select sid from sc,course,teacher where sc.cid=course.cid and course.id=teacher.tid and tname=‘张三’
);

11.

select student.sid,sanme,avgs from student,(
select sid,avg(score) as avgs from sc where score<60 group by sid having by count(score)>1)r
where student.sid=r.sid;

这里平均成绩不清楚的是是不及格课程的平均成绩和全部课程的平均成绩,如果是全部利用选出sid再求平均

学习知识点:

明白了where,和having的区别:where是分组前的筛选,having是分组后的筛选

12.

select student.* from student,sc
where student.sid=sc.sid and cid=‘01’ and score<60
order by score desc;

13.

select * from student left join
(select sid,score as class1 from sc where cid=‘01’) t1 on student.sid=t1.sid
left join (select sid,score as class1 from sc where cid=‘02’) t2 on
student.sid=t2.sid
left join (select sid,score as class1 from sc where cid=‘03’) t3 on
student.sid=t3.sid
left join (select sid,avg(score)as avgs from sc group by sid)r on
student.sid=r.sid
order by avgs desc;

学习知识点:

如何进行多表左连接

1685019258900.jpg

未完待续

相关文章
【计算机网络】第三章 数据链路层(可靠传输)
【计算机网络】第三章 数据链路层(可靠传输)
|
Windows
电脑win系统开机自动运行批处理、脚本等的方法/开机自动执行bat脚本
电脑win系统开机自动运行批处理、脚本等的方法/开机自动执行bat脚本
1751 0
|
C#
WPF 界面实现多语言支持 中英文切换 动态加载资源字典
原文:WPF 界面实现多语言支持 中英文切换 动态加载资源字典 1、使用资源字典,首先新建两个字典文件en-us.xaml、zh-cn.xaml。定义中英文的字符串在这里面【注意:添加xmlns:s="clr-namespace:System;assembly=mscorlib】 zh-cn.
3877 0
|
28天前
|
人工智能 缓存 前端开发
DeepSeek 重磅开源的超级 Agent,到底是个啥?
DeepSeek Harness 团队新的大招 Super-agent 是什么?和普通 Agent 有什么区别?一文带你搞懂新的 Agent 架构
455 0
|
4月前
|
人工智能 运维 IDE
零成本AI编程实战|Qoder CN免费社区版全解,额度规则、多端部署与CLI实操命令完整指南
AI赋能研发已经成为软件开发领域的主流趋势,AI编码助手可以极大降低重复编码工作量,提升学习与开发效率。但市面上大量高质量AI编程工具普遍采用订阅付费模式,对于编程学生、业余爱好者、初级开发者来说,长期订阅会带来不小的经济负担,抬高了AI编程的入门门槛。为了普惠广大基层开发群体,降低AI编程落地门槛,原通义灵码完成品牌迭代升级,正式更名为Qoder CN,并且推出永久可用的免费社区版本。该版本配套独立Credits额度体系,普通用户不需要付费订阅,就可以使用专业级AI编码智能体能力,覆盖代码学习、脚本编写、小型项目开发、代码调试等轻量化开发场景。
1346 0
|
5月前
|
前端开发 Java API
[049][Crypto模块]前后端混合加密API实战:基于Spring Boot的AES+RSA安全传输方案
本文详解Spring Boot中AES+RSA混合加密实战:前端用RSA公钥加密随机AES密钥并传输,后端通过`@Crypto`注解自动解密请求体。涵盖公钥分发、Hex/Base64编码统一、ECB模式适配及Caffeine缓存优化,提供开箱即用的端到端安全传输方案。(239字)
585 0
|
6月前
|
存储 人工智能 安全
AI智能体的开发
AI智能体是企业数字化转型的关键引擎,融合大模型(大脑)、知识库(记忆)与API工具(双手),实现自动化办公。本文详解其四大核心模块、五步开发流程及三档费用构成,助企业科学评估与落地AI员工。(239字)
|
Web App开发 人工智能 JSON
Windows版来啦!Qwen3+MCPs,用AI自动发布小红书图文/视频笔记!
上一篇用 Qwen3+MCPs实现AI自动发小红书的最佳实践 有超多小伙伴关注,同时也排队在蹲Windows版本的教程。
2800 1
|
数据安全/隐私保护 Docker 容器
如何本地跑通一个大模型
这里主要借助两个开源项目 [ollama](https://github.com/ollama/ollama) 和 [openwebui](https://github.com/open-webui/open-webui) 这两个项目,来尝试本地跑通`llama3.1 8b` 、 `mistral-nemo 12b` 和 `qwen2 7b` 这些模型,再大的模型机器也撑不住了。
849 12
|
缓存 算法 架构师
京东面试:如何设计600Wqps高并发ID?如何解决时钟回拨问题?
资深架构师尼恩在其读者交流群中分享了关于分布式ID系统的设计与实现,特别是针对高并发场景下的解决方案。他强调了分布式ID系统在高并发核心组件中的重要性,并详细介绍了百度的UidGenerator,这是一个基于Snowflake算法改进的Java实现,旨在解决分布式系统中的唯一ID生成问题。UidGenerator通过自定义workerId位数和初始化策略,支持虚拟化环境下的实例自动重启和漂移,其单机QPS可达600万。此外尼恩的技术分享不仅有助于提升面试表现,还能帮助开发者在实际项目中应对高并发挑战。
京东面试:如何设计600Wqps高并发ID?如何解决时钟回拨问题?