共用方式為


JOIN

適用於:核取記號為「是」Databricks SQL 核取記號為「是」Databricks Runtime

根據 join 準則,將兩個 table 參考中的數據列 結合起來。

語法

left_table_reference { [ join_type ] JOIN right_table_reference [ join_criteria ] |
           NATURAL join_type JOIN right_table_reference |
           CROSS JOIN right_table_reference }

join_type
  { [ INNER ] |
    LEFT [ OUTER ] |
    [ LEFT ] SEMI |
    RIGHT [ OUTER ] |
    FULL [ OUTER ] |
    [ LEFT ] ANTI |
    CROSS }

join_criteria
  { ON boolean_expression |
    USING ( column_name [, ...] ) }

Parameters

  • left_table_reference

    join左側的 table 參考。

  • right_table_reference

    table 在 join的右側作為參考。

  • join_type

    join型。

    • [ INNER ]

      傳回在這兩個 table 參考中具有相符 values 的數據列。 預設 join類型。

    • LEFT [ OUTER ]

      從左 table 引用傳回所有 values,並從右 table 引用傳回相符的 values,如果找不到相符,則附加 NULL。 也稱為 左外 join

    • RIGHT [ OUTER ]

      從右側 table 參考傳回所有 values,並從左側 table 參考傳回匹配的 values;若無匹配項,則附加 NULL。 也稱為 右外部 join

    • FULL [OUTER]

      傳回這兩個關係的所有 values,將 NULLvalues 附加到沒有相符的一方。 也稱為 完整外部 join

    • [ LEFT ]半

      從 table 參考的左側傳回與右邊相符的 values。 也稱為 左半 join

    • [ LEFT ]反

      從左側 table 參考中傳回與右側 table 參考不匹配的 values。 也稱為 左反 join

  • CROSS JOIN

    傳回兩個關聯性之笛卡兒乘積。

  • NATURAL

    指定兩個關聯的資料列將隱含地根據相同名稱的所有 columns 進行等式匹配。

  • join_criteria

    選擇性地指定一個 table 參考中的數據列如何與另一個 table 參考的數據列結合。

    警告

    如果您省略 join_criteria 任何 join_type 的語意會變成 的語 CROSS JOIN意。

    • ON boolean_expression

      具有 BOOLEAN 傳回型別的運算式,指定兩個關聯數據列的比對方式。 如果結果為 true,則會將數據列視為相符專案。

    • USING ( column_name [, ...] )

      藉由比對兩個關聯中都必須存在的 columnscolumn_name 的 list 相等,來匹配數據列。

  • table_alias

    具有選擇性 columnidentifierlist的暫存名稱。

備註

當您指定 USINGNATURAL時,SELECT * 只會針對每個用來首次匹配的 columns 顯示一次出現,後續顯示左邊的 columns,然後是右邊的 jointables,但不包括於其上聯結的 columns。

SELECT * FROM first JOIN second USING (a, b)

相當於

SELECT first.a, first.b,
       first.* EXCEPT(a, b),
       second.* EXCEPT(a, b)
  FROM first JOIN second ON first.a = second.a AND first.b = second.b

範例

-- Use employee and department tables to demonstrate different type of joins.
> CREATE TEMP VIEW employee(id, name, deptno) AS
     VALUES(105, 'Chloe', 5),
           (103, 'Paul' , 3),
           (101, 'John' , 1),
           (102, 'Lisa' , 2),
           (104, 'Evan' , 4),
           (106, 'Amy'  , 6);

> CREATE TEMP VIEW department(deptno, deptname) AS
    VALUES(3, 'Engineering'),
          (2, 'Sales'      ),
          (1, 'Marketing'  );

-- Use employee and department tables to demonstrate inner join.
> SELECT id, name, employee.deptno, deptname
   FROM employee
   INNER JOIN department ON employee.deptno = department.deptno;
 103  Paul      3 Engineering
 101  John      1   Marketing
 102  Lisa      2       Sales

-- Use employee and department tables to demonstrate left join.
> SELECT id, name, employee.deptno, deptname
   FROM employee
   LEFT JOIN department ON employee.deptno = department.deptno;
 105 Chloe      5        NULL
 103  Paul      3 Engineering
 101  John      1   Marketing
 102  Lisa      2       Sales
 104  Evan      4        NULL
 106   Amy      6        NULL

-- Use employee and department tables to demonstrate right join.
> SELECT id, name, employee.deptno, deptname
    FROM employee
    RIGHT JOIN department ON employee.deptno = department.deptno;
 103  Paul      3 Engineering
 101  John      1   Marketing
 102  Lisa      2       Sales

-- Use employee and department tables to demonstrate full join.
> SELECT id, name, employee.deptno, deptname
    FROM employee
    FULL JOIN department ON employee.deptno = department.deptno;
 101  John      1   Marketing
 106   Amy      6        NULL
 103  Paul      3 Engineering
 105 Chloe      5        NULL
 104  Evan      4        NULL
 102  Lisa      2       Sales

-- Use employee and department tables to demonstrate cross join.
> SELECT id, name, employee.deptno, deptname
    FROM employee
    CROSS JOIN department;
 105 Chloe      5 Engineering
 105 Chloe      5   Marketing
 105 Chloe      5       Sales
 103  Paul      3 Engineering
 103  Paul      3   Marketing
 103  Paul      3       Sales
 101  John      1 Engineering
 101  John      1   Marketing
 101  John      1       Sales
 102  Lisa      2 Engineering
 102  Lisa      2   Marketing
 102  Lisa      2       Sales
 104  Evan      4 Engineering
 104  Evan      4   Marketing
 104  Evan      4       Sales
 106   Amy      4 Engineering
 106   Amy      4   Marketing
 106   Amy      4       Sales

-- Use employee and department tables to demonstrate semi join.
> SELECT *
    FROM employee
    SEMI JOIN department ON employee.deptno = department.deptno;
 103  Paul      3
 101  John      1
 102  Lisa      2

-- Use employee and department tables to demonstrate anti join.
> SELECT *
    FROM employee
    ANTI JOIN department ON employee.deptno = department.deptno;
 105 Chloe      5
 104  Evan      4
 106   Amy      6

-- Use employee and department tables to demonstrate lateral inner join.
> SELECT id, name, deptno, deptname
    FROM employee
    JOIN LATERAL (SELECT deptname
                    FROM department
                    WHERE employee.deptno = department.deptno);
 103 Paul    3  Engineering
 101 John    1  Marketing
 102 Lisa    2  Sales

-- Use employee and department tables to demonstrate lateral left join.
> SELECT id, name, deptno, deptname
    FROM employee
    LEFT JOIN LATERAL (SELECT deptname
                         FROM department
                         WHERE employee.deptno = department.deptno);
 105 Chloe   5      NULL
 103 Paul    3      Engineering
 101 John    1      Marketing
 102 Lisa    2      Sales
 104 Evan    4      NULL
 106 Amy     6      NULL