Difference between INNER and OUTER joins?

Assuming you‘re joining on columns with no duplicates, which is a very common case:

  • An inner join of A and B gives the result of A intersect B, i.e. the inner part of a venn diagram intersection.
    
    An outer join of A and B gives the results of A union B, i.e. the outer parts of a Venn diagramunion.
    

      

Examples

Suppose you have two tables, with a single column each, and data as follows:

A    B
-    -
1    3
2    4
3    5
4    6

Note that (1,2) are unique to A, (3,4) are common, and (5,6) are unique to B.

Inner join

An inner join using either of the equivalent queries gives the intersection of the two tables, i.e. the two rows they have in common.

select * from a INNER JOIN b on a.a = b.b;
select a.*,b.*  from a,b where a.a = b.b;

  

a | b --+-- 3 | 3 4 | 4 

Left outer join

A left outer join will give all rows in A, plus any common rows in B.

select * from a LEFT OUTER JOIN b on a.a = b.b;
select a.*,b.*  from a,b where a.a = b.b(+);

  

a | b --+----- 1 | null 2 | null 3 | 3 4 | 4 

Full outer join

A full outer join will give you the union of A and B, i.e. all the rows in A and all the rows in B. If something in A doesn‘t have a corresponding datum in B, then the B portion is null, and vice versa.

select * from a FULL OUTER JOIN b on a.a = b.b;

  

a | b -----+----- 1 | null 2 | null 3 | 3 4 | 4 null | 6 null | 5
时间: 2024-12-17 22:28:43

Difference between INNER and OUTER joins?的相关文章

How Distributed Outer Joins on PostgreSQL with Citus Work

转自: https://docs.citusdata.com/en/v7.5/articles/outer_joins.html SQL is a very powerful language for analyzing and reporting against data. At the core of SQL is the idea of joins and how you combine various tables together. One such type of join: out

哪些问题是面试官经常问Java工程师的问题 ? --- 转自quora

Which are the frequently asked interview questions for Java Engineers ? Vivek Vermani, www.buggybread.com | Programme... (more) 265 upvotes by Ridox Liu, Shivani Sahni Vermani, Viet Thang, (more) Java的基础知识   For a Core Java Developer , Questions arou

你想成为优秀的Java程序员吗?

Java是全世界最受欢迎的3大编程语言之一,它可以开发出许多实用的WEB应用程序和桌面应用程序,更重要的一点,Java是跨平台的语言——编写一次,可以再任何地方运行.另外,Java也很容易入门,如果你想成为一名优秀的Java程序员,那么请你扪心自问一下,你熟悉下面的知识点吗? 面向对象概念 抽象类和接口 构造函数和初始化函数的执行顺序 文件读写和序列化 集合——List.Map.Set 访问控制 异常处理 泛型 Java关键字——Static , Final , volatile, synchr

Java程序员应该掌握哪些东西?

Java是热门的语言之一,2014年7月份TIOBE编程语排名Java排名第二,仅在C语言之后.Java可以用来开发web应用和桌面应用,更重要的是Java具有跨平台性:write once, run everywhere. Java相对其他语言来说,更容易学习.如果你想成为一名Java程序员,根据个人经验,认为你至少应该对下面的这些内容非常熟练,才能在Java开发上游刃有余. Java基础部分: OOP概念 抽象类与接口 构造函数与initialization order(初始化顺序) Jav

[HIve - LanguageManual] Joins

Hive Joins Hive Joins Join Syntax Examples MapJoin Restrictions Join Optimization Predicate Pushdown in Outer Joins Enhancements in Hive Version 0.11 Join Syntax Hive supports the following syntax for joining tables: join_table:     table_reference J

如何写出高性能的sql语句?

(1) 选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table)将被最先处理,在FROM子句中包含多个表的情况下,你必须选择记录条数最少的表作为基础表.如果有3个以上的表连接查询, 那就需要选择交叉表(intersection table)作为基础表, 交叉表是指那个被其他表所引用的表. (2) WHERE子句中的连接顺序.: ORACLE采用自下而上的顺序解析WHER

cratedb joins 原理(官方文档)

JOINs are essential operations in relational databases. They create a link between rows based on common values and allow the meaningful combination of these rows. CrateDB supports joins and due to its distributed nature allows you to work with large

文摘:OUTER JOIN

原文地址:(https://www.w3resource.com/sql/joins/perform-a-full-outer-join.php) What is Full Outer Join in SQL? In SQL the FULL OUTER JOIN combines the results of both leftand right outer joins and returns all (matched or unmatched) rows from the tables on

【mysql】关于ICP、MRR、BKA等特性

一.Index Condition Pushdown(ICP) Index Condition Pushdown (ICP)是mysql使用索引从表中检索行数据的一种优化方式,从mysql5.6开始支持,mysql5.6之前,存储引擎会通过遍历索引定位基表中的行,然后返回给Server层,再去为这些数据行进行WHERE后的条件的过滤.mysql 5.6之后支持ICP后,如果WHERE条件可以使用索引,MySQL 会把这部分过滤操作放到存储引擎层,存储引擎通过索引过滤,把满足的行从表中读取出.IC