标签:
一、SET运算符select employee_id , department_id from emp01 union all --相当于两个集合相加, union A并B ,intersect ,A交B,MINUS 差集,A-B select employee_id , department_id from emp02
SELECT department_id, TO_NUMBER(null) location, hire_date FROM employees UNION SELECT department_id, location_id, TO_DATE(null) FROM departments;所查询的列一定要一一对应,没有的可以用null来代替
SELECT employee_id, job_id,salary FROM employees UNION SELECT employee_id, job_id,0 --salary可以用0代替 FROM job_history;
指定列不显示column 指定列的名字 noprint;
SELECT 'sing' "My dream", 3 a_nu FROM dual UNION SELECT 'I`d like to teach',1 FROM dual UNION SELECT 'the world to',1 FROM dual order by 2SQL> column a_nu noprint;--指定a_nu列不显示
select manager_id , employee_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)2)不成对比较
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);注意主查询的列 和 内查询的列,一定要一一对应
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 employee_id , e1.department_id , salary ,e2.avg_sal from employees e1 , (select department_id , avg(salary) avg_sal from employees group by department_id ) e2 --以子查询的形式,形成了一个新表 where e1.department_id = e2.department_id and e1.salary > e2.avg_sal单列子查询应用举例
/*select employee_id , last_name , ( case department_id when (select department_id from departments where location_id = 1800 ) then 'Canada' else 'Usa' end ) location from employees */ SELECT employee_id, last_name, (CASE WHEN department_id = (select department_id from departments where location_id = 1800 ) THEN 'Canada' ELSE 'USA' END) location FROM employees在 ORDER BY 子句中使用单列子查询
SELECT employee_id, last_name FROM employees e ORDER BY (SELECT department_name FROM departments d WHERE e.department_id = d.department_id );
SELECT column1, column2, ... FROM table1 outer WHERE column1 operator (SELECT colum1, column2 FROM table2 WHERE expr1 = outer.expr2);子查询中使用主查询中的列
select employee_id , last_name , job_id from employees e where 2 <= ( select count(*) from job_history j where e.employee_id = j.employee_id )
/* select employee_id , last_name , salary from employees e1 where e1.employee_id in( select manager_id from employees e2 ) */ /* select distinct e1.employee_id , e1.last_name , e1.salary from employees e1,employees e2 where e1.employee_id = e2.manager_id */ SELECT employee_id, last_name, job_id, department_id FROM employees outer WHERE EXISTS ( SELECT 'X' FROM employees WHERE manager_id = outer.employee_id );问题:查询departments表中,不存在于employees表中的部门的department_id和department_name
/* select d1.department_id from departments d1 minus SELECT department_id FROM employees */ SELECT department_id, department_name FROM departments d WHERE NOT EXISTS ( SELECT 'X' FROM employees WHERE department_id = d.department_id );
标签:
原文地址:http://blog.csdn.net/wjw0130/article/details/43818679