JOIN
適用於: Databricks SQL Databricks Runtime
根據聯結準則,結合兩 個數據表參考 中的數據列。
語法
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 [, ...] ) }
參數
-
聯結左側的數據表參考。
-
聯結右側的數據表參考。
join_type
聯結類型。
[ INNER ]
傳回在這兩個數據表參考中具有相符值的數據列。 默認聯結類型。
LEFT [ OUTER ]
從左資料表參考傳回所有值,以及來自右資料表參考的相符值,如果沒有相符專案,則會附加
NULL
。 也稱為 左外部聯接。RIGHT [ OUTER ]
從右資料表參考傳回所有值,以及來自左資料表參考的相符值,如果沒有相符專案,則會附加
NULL
。 也稱為 右外部聯接。FULL [OUTER]
傳回這兩個關聯中的所有值,並將
NULL
值附加在不相符的一邊。 也稱為 完整外部聯接。[ LEFT ]半
從數據表參考左側傳回值,該參考與右邊相符。 也稱為 左半聯結。
[ LEFT ]反
從左數據表參考傳回與右數據表參考沒有相符的值。 也稱為 左反聯結。
CROSS JOIN
傳回兩個關聯性之笛卡兒乘積。
NATURAL
指定兩個關聯性中的數據列會以相等方式隱含地比對具有相符名稱的所有數據行。
join_criteria
指定一個數據表參考中的數據列如何與另一個數據表參考的數據列結合。
ON boolean_expression
具有 BOOLEAN 傳回型別的運算式,指定兩個關聯數據列的比對方式。 如果結果為 true,則會將數據列視為相符專案。
USING ( column_name [, ...] )
比較兩個關聯中必須存在之數據行清單
column_name
的相等性,以比對數據列。
-
具有選擇性數據行標識碼清單的暫存名稱。
備註
當您指定 USING
或 NATURAL
時, SELECT *
只會針對每個用來比對的數據行顯示一個相符專案,後面接著左邊的數據行,然後是右聯結數據表,排除所聯結的數據行。
SELECT * FROM left JOIN right USING (a, b)
相當於
SELECT left.a, left.b,
left.* EXCEPT(a, b),
right.* EXCEPT(a, b)
FROM left JOIN right ON left.a = right.a AND left.b = right.b
如果您省略 join_criteria
任何 join_type
的語意會變成 的語 CROSS JOIN
意。
範例
-- 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