SQL查询练习

SQL查询练习

student(学生表)

sidsname

sbject(课程表)

kidkname

select(选修课程表)

skidkidsid

1.查询没有被任何一个同学选修的课程信息
2.查询每个学生选修了哪些课程,需要详细的学生信息与课程信息
3.查询选修化学课程的学生信息
4.查询选修了三门课程的学生信息

1.select kname from subject where kid exists 
2.select sid,sname,kid,kname from student a,sbject b,`select` c where a.sid = c.sid and b.kid = c.kid;
3.
-- 子查询
select sname from student where sid in(select sid from `select` where jid in(select jid from subject where jn ='化学'))-- 关联查询
select sname from select, subject, student where select.jid = subject.jid and student.sid = select.sid and subject.jn = '化学';
4.
--第一种方式
select sname from student where sid in (select sid from `select` c group by c.sid having count(*) =3);
-- 第二种方式
select sname from student where sid in 
(select sid from (select sid, count(*) as cnt from `select` group by sid) a where cnt >= 3);
-- 第三种方式 将查询语句取别名可作为临时表使用
select student.* from student, 
(select sid, count(*) as cnt from select group by sid) a where student.sid = a.sid and a.cnt >= 3 

可以将查询语句取别名后作为临时表使用!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值