SQL Advanced-Usage of Outer Joins
Categories of outer joins:
- left outer join
- right outer join
- full outer join
A full outer join means that if the join conditions are not met, the elements in both tables will be retained.
Since SQL is a language based on set theory, we can do the following operations to perform set operations:
Intersection: inner join implementation, in the SQL standard, you can use intersect
Union: full outer join, union in SQL standard
Difference set: After outer join, take the row whose b column is null, or it can be achieved by not in or not exist.
XOR: First take the difference set, and then filter out the elements in the difference set from table A.
Normally we need to 行转为列
use this in a database scenario:
- outer join
- scalar subquery
- case expression
When the elements in the two tables are 一对一或者一对多
related: we can directly connect and then aggregate the results.
But if there is 多对多
a relationship between the elements of the two tables, they need to be connected 某一个表先进行聚合
and then connected.