码迷,mamicode.com
首页 > 其他好文 > 详细

ORCLA基础总结(二)

时间:2020-12-16 11:45:07      阅读:5      评论:0      收藏:0      [点我收藏+]

标签:job   oca   str   column   微软雅黑   like   location   union   dream   

SET 运算符

技术图片

使用 SET 操作符注意事项

  在SELECT 列表中的列名和表达式在**数量和数据类型**上要相对应
  括号可以改变执行的顺序
  ORDER BY 子句:
      只能在语句的最后出现
      可以使用第一个查询中的列名, 别名或相对位置

SELECT department_id, TO_NUMBER(null) location, hire_date FROM employees UNION SELECT department_id, location_id, TO_DATE(null) FROM departments;
SELECT employee_id, job_id,salary FROM employees UNION SELECT employee_id, job_id,0 FROM job_history;
使用相对位置排序举例

COLUMN a_dummy NOPRINT SELECT ‘sing‘ AS "My dream", 3 a_dummy FROM dual UNION SELECT ‘I"d like to teach‘, 1 FROM dual UNION SELECT ‘the world to‘, 2 FROM dual ORDER BY 2;

子查询

单列子查询

子查询 (内查询) 在主查询之前一次执行完成。 子查询的结果被主查询(外查询)使用 。

SELECT select_list FROM table WHERE expr operator (SELECT select_list FROM table);

多列子查询

1. 多列子查询
成对比较
问题:查询与141号或174号员工的manager_id和department_id相同的其他员工的employee_id, manager_id, department_id
SELECT employee_id, manager_id, department_id FROM EMPLOYEES WHERE ( manager_id, department_id ) in ( SELECT manager_id, department_id FROM employees WHERE employee_id IN (141,174) ) and EMPLOYEE_ID not in (141,174)

不成对比较
SELECT employee_id, manager_id, department_id FROM employees WHERE manager_id IN ( SELECT manager_id FROM employees WHERE employee_id IN ( 174, 141 )) AND department_id IN ( SELECT department_id FROM employees WHERE employee_id IN ( 174, 141 )) AND employee_id NOT IN ( 174, 141 );

2. FROM 子句中使用子查询
问题:返回比本部门平均工资高的员工的last_name, department_id, salary及平均工资

 <font color=gray size=5 face="微软雅黑">方法一</font>

select last_name,department_id,salary, (select avg(salary)from employees e3 where e1.department_id = e3.department_id group by department_id) avg_salary from employees e1 where salary > (select avg(salary) from employees e2 where e1.department_id = e2.department_id group by department_id )
方法二
SELECT a.last_name, a.salary, a.department_id, b.salavg FROM employees a, (SELECT department_id, AVG(salary) salavg FROM employees GROUP BY department_id) b WHERE a.department_id = b.department_id AND a.salary > b.salavg;

3. CASE 表达式中使用单列子查询

4. ORDER BY 子句中使用单列子查询

5. 相关子查询

6. 相关更新

7. 相关删除

EXISTS /NOT EXISTS 操作符

WITH 子句

ORCLA基础总结(二)

标签:job   oca   str   column   微软雅黑   like   location   union   dream   

原文地址:https://www.cnblogs.com/denghy-301/p/14116225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!