Chapter IV multi-table query

SQL statement multi-table query:

For example: query information in accordance with department_id employee (employee table) and department (department table).

A method :( Universal): SELECT ... FROM ... WHERE

select e.last_name,e.department_id,d.department_name
	from employee e,department d
	where e.department_id=d.department_id

Method two: SELECT ... FROM ... NATURAL JOIN ...

There are limitations: the same column will automatically connect two tables (there may be multiple: DEPARTMENT_ID and the manager_id)

select last_name,department_id,department_name
	from employees
	natural join departments;

Method three: SELECT ... JOIN ... USING ...

There are limitations: the method is better than two, but if the connection Column names of different multi-table, this method is not appropriate

select last_name,department_id,department_name
	from employees
	join departments
	using (department_id);

Method four: SELECT ... FROM ... JOIN ... ON ...

Common methods: Method a relatively easier to achieve coupling outer (left, right, full)

select last_name,e.department_id,department_name 
from employees e
join departments d
on e.department_id = d.department_id;

- In connection

- Equijoins

- not Equijoins

  1. - unnatural connection

    - Self-connection

- external connection

 --左外连接、右外连接、满外连接

exercise:

  1. When multi-table join query, if two tables have the same column, you must use table aliases for column names referenced otherwise wrong!

  2. Check out last_name employees, department_name, city

  select last_name,department_name,city
  	from department d,employees e,location l
  	where d.department_id = e.department_id and d.location_id = l.location_id;
  1. Check out last_name as "Chen" the information manager. (Manager_id employee is an employee of employee_id)

    0) For example: Zhang employee number: "1001", my staff number: "1002"

    My manager_id as "1001" -manager is "embittered"

    1) through two SQL queries:

  select manager_id 
      from employees
      where lower(last_name)='chen';
      --返回结果为108
  select m.* 
  	from employees e, employees m
  	where e.manager_id =108;

2) (from connected through a SQL query):

select m.*
from employees e,employees m
where e.manager_id = m.employee_id and e.last_name='Chen';
  1. Through a SQL query (subquery):
 select * 
 from employees
 where employee_id=(
 				select manager_id 
     				from employees
     				where last_name='Chen'
 				);
  1. Query each employee last_name and GRADE_LEVEL (in JOB_GRADES table), a non-equivalent connection -----
 select last_name,salary ,grade_level,lowest_sal,highest_sal
 	from employees e,job_grades j
 	where e.salary >=j.lowest_sal and e.salary <=j.highest_sal;
  1. Left outer join and right outer join
  select last_name,e.department_id,department_name
  	from employees e,departments d
  	where e.department_id =d.department_id(+);
  	
  select last_name,d.department_id,department_name
  	from employees e,departments d
  	where e.department_id(+)=d.department_id;
  --理解"(+)"的位置:以左外连接为例,因为左表需要返回更多的记录,右表就需要“加上”更多的记录,所以在右表的链接条件上加上“(+)”
  --注意:1).两边都加上"(+)"符号,会发生语法错误!
  	 --2)这种语法是Oracle所独有,不能在其他数据库中使用。
  1. 99 SQL left outer join, right outer join, full outer join
--1).
select last_name.department_name
	from employee e left outer join departments d
	on e.department_id = d.department_id;
--2).
select last_name,department_name
	from employees e right join departments d
	on e.department_id = d.department_id ;
--3).
select last_name,department_name
from employees e full join departments d 
on e.department_id = d.department_id;
  1. SQL 99 connecting the Employees table and the table Department
  --1).
  select *
  	from employees join departments
  	using (department_id);
  	--缺点:要求两个表中必须有一样的列名。
--2).
select * 
	from employees e join departments d
	on e.department_id =d.department_id;
  --3).多表连接
  select e.last_name,d.department_name,l.city
  	from employees e join departments d 
  	on e.department_id = d.department_id
  	join location l
  	on d.location_id = l.location_id;
Released eight original articles · won praise 11 · views 905

Guess you like

Origin blog.csdn.net/qq_41241814/article/details/103902574
Recommended