oracle资料:ORACLE复习资料5

/*使用子查询*/
//理解子查询
//子查询可以用在INSERT 、UPDATE、DELETE语句里
//子查询可作为联接运算的一种备选。子查询又叫内部查询
select vFirstName,vLastName,vAddress
from Employee where cCity =
(select cCity from employee e
where e.vfirstname='Catherine' and e.vlastname='Roberts')
//使用单行子查询
//单行运算符(=,>,>=,<,<=,<>不等于)
//创建单行子查询指南
//1,所有的子查询必须用圆括弧括起来
//2,为增强可读性,子查询应置于比较条件的右方
//3,单行子查询可以使用单值或多值运算符
//在子查询中使用Group By 和 Having 子句
select cCollegeCode,avg(nTestScore)
from Externalcandidate
group by cCollegeCode
having avg(nTestScore)>(select avg(nTestScore) from Externalcandidate)
//在子查询中使用WITH子句
//如果在一个复杂的查询中,SELECT语句中某部分查询需要多次用到,那么,
//你可以使用WITH子句。用WITH子句先定义一个查询块,然后在查询中使用此定义。
//WITH子句可用于设计检索多表中数据或涉及要求有数据聚合的查询。
//Oracle服务器先检索查询块的结果,然后把结果保存在用户临时表空间里,
//这种方法有助于提高查询性能
//使用WITH子句指南
//WITH子句只能和SELECT语句一起使用
//WITH子句可包含多个查询,各查询之间以逗号分隔
//如果其查询与表名同名,则以查询块名优先于表名
with
EMP_SAL as (select cEmployeeCode,sum(nMonthlysalary) as EMP_TOTAL from monthlysalary group by cEmployeeCode)
select * from EMP_SAL where EMP_TOTAL>(select avg(EMP_TOTAL) from EMP_SAL)
//使用多行子查询
//多行运算符
//IN 等于列表中的任何元素
//ANY 等于列表中的最小值
//ALL 等于列表中的最大值
//NOT IN不等于列表中的任何值
//使用IN运算符的多行子查询
select vFirstName,vLastName,vAddress from Employee
where cCandidateCode in (select cCandidateCode from Employee
where cCity = 'Columbus' or cCity='Norton')
//使用ANY运算符的多行子查询
// <ANY 小于列表中的最大值
// >ANY 大于列表中的最小值
// =ANY 等于列表中的任意一值。这等于IN运算符
select vFirstName,vLastName,cCity from Employee
where dJoiningDate > any(select dJoiningDate from Employee where cCity='Norton')
//使用ALL运算符的多行子查询
// <ALL 小于列表中的最小值
// >ALL 大于列表中的最大值
select vFirstName,vLastName,cCity from employee
where dJoiningDate > all(select dJoiningDate from Employee where cCity = 'Norton')
//处理子查询中的空值
//not in 等价于 <>ALL
select cCandidateCode,vFirstName,vLastName from Externalcandidate
where cInterviewer not in (select cInterviewer from Externalcandidate)
//嵌套子查询
select cEmployeeCode,vFirstName,vLastName from Employee
where cEmployeeCode in (select cEmployeeCode from Employeeskill
where cSkillCode = (select cSkillCode from skill
where vSkill='Project Management'))
//其他子查询
//相关子查询
select c.cCollegeCode,cCollegeName from College c
where 5 <= (select count(*) from Externalcandidate
where cCollegeCode = c.ccollegecode)
//相关DELETE
delete from student s
where cStudentCode = (select cStudentCode from Alumni
where cStudentCode = s.cStudentCode)
文章引用自:
Tags:  oracle资料

延伸阅读

最新评论

发表评论