sql1复习笔记8

sql1复习笔记8

1.查询练习-子查询

查询出“计算机系”教师所教课程的成绩表
思路

select * from teacher where depart='计算机系';

select * from course where tno in (select tno from teacher where depart='95032');

select * from score where cno in (select cno from course where tno in (select tno from teacher where depart='95032'));

一步步查,把选的语句作为条件。

2.查询练习-union和notin的使用

查询【95032与5032】不同职称的教师的tname和prof。
注意:题目是要不同职称的,都有的职称是不要的。
teacher表

select prof from teacher where depart='5032';

select * from teacher where depart='95032' and prof not in(select prof from teacher where depart='5032') 
union 
select * from teacher where depart='5032' and prof not in(select prof from teacher where depart='95032');

3.查询练习-any表示至少一个desc降序

查询选修编号为3-105课程且成绩【至少】高于选修编号3-245的其中一个同学的成绩的cno sno和degree。
并按degree从高到低排序

select * from score where cno='3-245';
select * from score where cno='3-105';
//至少?大于其中至少一个。
select * from score where cno='3-105' and degree>any(select degree from score where cno='3-245') 
order by degree desc;

order by是排序,any是至少一个,desc是降序。

4.查询练习-all表示所有

查询选修编号为3-105且成绩高于【全部】选修编号为3-245课程的同学的cno、sno和degree。

select * from score where cno='3-245';
select * from score where cno='3-105';

select * from score where cno='3-105' and degree>all(select degree from score where cno='3-245') 
order by degree desc;

5.查询练习-as取别名-union求并集

查询所有教师和同学的name sex和birthday

select tname,tsex,tbirthday from teacher;
select sname,ssex,sbirthday from student;

select tname,tsex,tbirthday from teacher 
union 
select sname,ssex,sbirthday from student;

//发现第一行全是tname,加入别名
select tname as name,tsex as sex,tbirthday as birthday from teacher 
union 
select sname,ssex,sbirthday from student;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值