SQL - multi-table join query

If a query involves two or more tables at the same time, it is called a join query (reflected in the FROM clause). There can be multiple tables participating in the connection, but the connection operation is performed between two tables, that is, pairwise connection.


Join queries include:

  • inner join
    • Equivalent join: use "=" to compare the column values ​​​​of the joined columns
    • Non-equivalent connection: use ">, >=, <, <=, <>" signs for comparison operations
    • Self-connection: special inner connection, one table is regarded as two tables, and it is connected by itself, and the table must be aliased
  • outer join
    • left outer
    • right outer
    • front outside
  • cross connect       

1 inner join

The process of performing a connection operation:

First take the first tuple in table 1 , then scan table 2 from the beginning , and find the tuples that meet the connection conditions one by one ;

After finding it, splice the first tuple in Table 1 with this tuple to form a tuple in the result table.

After all the searches in table 2 are completed , take the second tuple in table 1 , and then scan table 2 from the beginning, …

Repeat this process until all tuples in Table 1 have been processed.

select ...
    from tablename [inner] join 被连接表
    on 连接条件
    ......

——Example (quote existing database: bjpowernode.sql)

dept: department table emp: employee table salgrade : Salary grade table

deptno: department number

empno: employee number

grade: grade
dname: department name ename: employee name losal: minimum wage
loc: department location job: job position hisal: the highest salary
mgr: superior leadership number
hiredate: entry time
sal: monthly salary
comm: subsidy/stipend
deptno: department number

1.1 Equivalent connection

Query the department name of each employee, and display the employee name and department name.

select ename,dname
from emp e join dept d
on e.deptno=d.deptno;

1.2 Non-equivalent connection 

Query the salary level of each employee, display the employee name, salary, and salary level.

select ename,sal,grade
from emp e join salgrade s
on e.sal between s.losal and s.hisal;

1.3 Self-join 

Special inner joins—joined tables are physically the same table.

The two tables must be aliased to logically form two tables (one is the query result table and the other is the query condition table). Note: When specifying an alias for a table, in other places in the query statement, all places where the table name is used must use the alias, and the original table name cannot be used again.  

<table name> [AS] <table alias>   (note the difference with column aliases, column aliases cannot be used after group by)

Query the superior leader of an employee, display the employee name and the corresponding leader name.

Note: employee's leader number = leader's employee number

select a.ename as '员工名',b.ename as '领导名'
from emp a inner join emp b
on a.mgr=b.empno;

 2 outer joins

Only the data in one table must meet the connection conditions , while the data in the other table may not meet the connection conditions .   

The difference between outer join and ordinary join: the ordinary join operation only outputs tuples that meet the join conditions

The outer join operation uses the specified table as the main body of the connection, and outputs the tuplesin the main table that do not meet the join conditions

Left outer join: List all tuples in the left relation  

Right outer join: List all tuples in the right relation

# 1.ANSI方式的外连接的语法格式为:
FROM  表1  LEFT | RIGHT |FULL [OUTER]  
    JOIN  表2  ON  <连接条件> 

# 2.theta方式的外连接的语法格式为:
# 左外连接(输出表1中所有内容):
   FROM  表1, 表2  WHERE [表1.]列名(+) = [表2.]列名
# 右外连接(输出表2中所有内容):
   FROM  表1, 表2  WHERE [表1.]列名= [表2.]列名(+) 

2.1 Left Outer Join 

Query the supervisor of each employee.

Note: Mainly query the employee table on the left

select a.ename as '员工', b.ename '领导'
from emp a left join emp b
on a.mgr=b.empno;

2.1 Right outer join 

Query the supervisor of each employee.

Note: Mainly query the leader table on the right

select a.ename as '员工', b.ename '领导'
from emp a right join emp b
on a.mgr=b.empno;

 Query which department has no employees.

Note: The main table is the department table, use right where e.empno is null

select d.*
from emp e right join dept d
on e.deptno=d.deptno
where e.empno is null;

3 three table connection 

Query each employee's department name and salary grade.

Note: Use the emp table dept table to connect first, and then connect the emp table and the salgrade table

select 
    e.ename,d.dname,s.grade
from 
    emp e 
join 
    dept d 
on 
    e.deptno=d.deptno
join 
    salgrade s 
on 
    e.sal between s.losal and s.hisal; 

 Query each employee's department name, salary grade, and superior leader.

Note: Left outer join cannot be performed in the first join

select e.ename '员工',d.dname,s.grade,e1.ename '领导'
from
    emp e
join
    dept d
on
    e.deptno=d.deptno
join
    salgrade s
on
    e.sal between s.losal and s.hisal
left join
    emp e1
on
    e.mgr = e1.empno;

Guess you like

Origin blog.csdn.net/J__aries/article/details/130130586