|
本帖最后由 botang 于 2016-4-26 10:09 编辑
课程第12次(2016-04-22星期五):两表连接,子查询
【上完1Z0-051的第6章】:两表连接
【上完1Z0-051的第7章】:子查询
【1Z0-051】:共8章
【1Z0-052】:共3章
2016-04-22.sql:
- select * from departments;
- select * from locations;
- SELECT department_id, department_name,
- location_id, city
- FROM departments d
- NATURAL JOIN locations l ;
- SELECT department_id, department_name,
- l.location_id, city
- from departments d, locations l
- where d.location_id=l.location_id;
-
-
- select e.employee_id,e.last_name,d.department_name, e.department_id
- from employees e , departments d
- where e.department_id=d.department_id
- and e.manager_id=d.manager_id;
-
- select e.employee_id,e.last_name,d.department_name, department_id
- from employees e natural join departments d;
- ----
- select e.employee_id,e.last_name,d.department_name, department_id
- from employees e join departments d
- using (department_id);
-
- ---
-
-
- ---
- select e.employee_id,e.last_name,d.department_name, d.department_id
- from employees e join departments d
- on ( e.department_id=d.department_id)
- ;
- ---
- SELECT l.city, d.department_name
- FROM locations l JOIN departments d
- USING (location_id)
- WHERE location_id = 1400;
- ---
- select worker.last_name||' works for '||manager.last_name
- from employees worker, employees manager
- where worker.manager_id=manager.employee_id;
- --
- select worker.last_name||' works for '||manager.last_name
- from employees worker join employees manager
- on (worker.manager_id=manager.employee_id);
-
- ---
- create table job_grades( grade_level char(1),
- lowest_sal number(8,2),
- highest_sal number(8,2));
-
- insert into job_grades values (
- 'F', 25000 , 40000 ) ;
- select * from job_grades;
- ---
- select e.employee_id, e.last_name , j.grade_level
- from employees e , job_grades j
- where e.salary between j.lowest_sal and j.highest_sal;
- ---
- select e.employee_id, e.last_name , j.grade_level
- from employees e join job_grades j
- on e.salary between j.lowest_sal and j.highest_sal;
-
- ---
-
- select * from employees where employee_id=178;
-
- ---
-
- select * from departments;
-
- ---
-
- SELECT e.employee_id, e.last_name, e.department_id, d.department_name
- FROM employees e LEFT OUTER JOIN departments d
- ON (e.department_id = d.department_id) ;
- SELECT e.employee_id, e.last_name, e.department_id, d.department_name
- FROM employees e , departments d
- where e.department_id =d.department_id(+);
- SELECT e.employee_id, e.last_name, e.department_id, d.department_name
- FROM departments d right OUTER JOIN employees e
- ON (e.department_id = d.department_id) ;
- ---
- SELECT e.employee_id, e.last_name, e.department_id, d.department_name
- FROM employees e right OUTER JOIN departments d
- ON (e.department_id = d.department_id) ;
- SELECT e.employee_id, e.last_name, e.department_id, d.department_name
- FROM employees e , departments d
- where e.department_id(+) =d.department_id;
- SELECT e.employee_id, e.last_name, e.department_id, d.department_name
- FROM departments d left OUTER JOIN employees e
- ON (e.department_id = d.department_id) ;
- --
- SELECT e.employee_id, e.last_name, e.department_id, d.department_name
- FROM employees e full OUTER JOIN departments d
- ON (e.department_id = d.department_id) ;
- SELECT e.employee_id, e.last_name, e.department_id, d.department_name
- FROM departments d full OUTER JOIN employees e
- ON (e.department_id = d.department_id) ;
- --ERROR
- SELECT e.employee_id, e.last_name, e.department_id, d.department_name
- FROM employees e , departments d
- where e.department_id(+) =d.department_id
- union
- SELECT e.employee_id, e.last_name, e.department_id, d.department_name
- FROM employees e , departments d
- where e.department_id=d.department_id(+);
- ---
- select s.SID , n.NAME , se.VALUE
- from v_$session s , v_$sesstat se , v_$statname n
- where s.sid=se.SID and se.STATISTIC#=n.STATISTIC#
- and s.USERNAME='HR' and n.NAME='redo size'
- ;
- ---
- select p.SPID , s.USERNAME ,s.TERMINAL
- from v_$session s, v_$process p
- where s.PADDR =p.ADDR
- and s.SID=197;
- ---
- SELECT employee_id, last_name
- FROM employees
- WHERE salary in
- (SELECT MAX(salary)
- FROM employees
- GROUP BY department_id);
-
- ---
-
- SELECT employee_id, last_name, job_id, salary
- FROM employees
- WHERE salary < ANY
- (SELECT salary
- FROM employees
- WHERE job_id = 'IT_PROG')
- AND job_id <> 'IT_PROG';
-
- ----
- SELECT last_name, salary, department_id
- FROM employees o
- WHERE salary >
- (SELECT AVG(salary)
- FROM employees
- WHERE department_id =
- o.department_id) ;
-
- ----
-
- SELECT o.last_name, o.salary, o.department_id, a.AVGSAL
- from employees o , (SELECT AVG(salary) AVGSAL ,department_id
- FROM employees
- group by department_id ) a
- where o.department_id=a.department_id and o.salary > a.avgsal;
-
-
- ---
-
- SELECT last_name, salary, department_id
- FROM employees o
- WHERE salary in
- (SELECT AVG(salary)
- FROM employees
- WHERE department_id =
- o.department_id) ;
- SELECT e.employee_id, last_name,e.job_id
- FROM employees e
- WHERE employee_id in (SELECT employee_id
- FROM job_history
- WHERE
- employee_id = e.employee_id
- group by employee_id having count(*) >=2);
-
-
- SELECT e.employee_id, last_name,e.job_id
- FROM employees e
- WHERE exists (SELECT 1
- FROM job_history
- WHERE
- employee_id = e.employee_id
- group by employee_id having count(*) >=2);
复制代码
|
|