数据库
首页 > 数据库> > SQL经典50查询语句(面试题)案例

SQL经典50查询语句(面试题)案例

作者:互联网

题目如下:
查询有课程成绩小于60分的同学的学号、姓名
首先咱们还是先分析题目,这道题要使用到两个表,score和student这两个表
score表中有本题需要的分数,student表中有咱们需要的学号和姓名,
首先就是要连接两表,我使用的是内连接
第一步:

select st.sid,st.sname,sc.student_id,sc.num 
from student st
inner join score sc
on st.sid=sc.student_id

这样就得到了表sct(我给这个表起的别名)
然后去筛选就可以了,条件是有课程小于60

select
sct.sid,sct.sname 
from 
(select st.sid,st.sname,sc.student_id,sc.num 
from student st
inner join score sc
on st.sid=sc.student_id)sct
where sct.num<60;

 这个表其实挺有意思的名字起得有意思,
而且结果是除了一个13学号的同学只学了一课没有低于60分其他的都低于60了哈哈。
今日分享到这啦~

标签:面试题,sid,50,st,60,student,SQL,sc,sct
来源: https://blog.csdn.net/weixin_56369425/article/details/118435684