Snowpark Migration Accelerator: 結合

説明

指定した結合条件を使用して、2つの テーブル参照 から行を結合します。詳細については、 Databricks SQL 言語参照 JOIN をご参照ください。

JOIN は、2つのソース(テーブルやビューなど)からのデータを1つの結果セットに結合します。結果の各行には、指定された条件に基づいて両方のソースからの列が含まれます。結合の詳細については、 結合を使用した作業 をご参照ください。(Snowflake SQL 言語参照 JOIN)

構文

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 [, ...] ) }
Copy
SELECT ...
FROM <object_ref1> [
                     {
                       INNER
                       | { LEFT | RIGHT | FULL } [ OUTER ]
                     }
                   ]
                   JOIN <object_ref2>
  [ ON <condition> ]
[ ... ]

SELECT *
FROM <object_ref1> [
                     {
                       INNER
                       | { LEFT | RIGHT | FULL } [ OUTER ]
                     }
                   ]
                   JOIN <object_ref2>
  [ USING( <column_list> ) ]
[ ... ]

SELECT ...
FROM <object_ref1> [
                     {
                       | NATURAL [ { LEFT | RIGHT | FULL } [ OUTER ] ]
                       | CROSS
                     }
                   ]
                   JOIN <object_ref2>
[ ... ]
Copy

サンプルソースパターン

セットアップデータ

Databricks

-- 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'  );
Copy

Snowflake

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

CREATE TEMP VIEW department(deptno, deptname) AS
SELECT deptno, deptname
  FROM (VALUES(3, 'Engineering'),
          (2, 'Sales'      ),
          (1, 'Marketing'  )) AS v1 (deptno, deptname);
Copy

パターンコード

Databricks

-- 1. 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;
 
-- 2. We will use the employee and department tables to show how a left join works. This example will help you understand how to combine data from two tables while keeping all records from the left (first) table.
SELECT id, name, employee.deptno, deptname
   FROM employee
   LEFT JOIN department ON employee.deptno = department.deptno;

-- 3. Demonstrate a RIGHT JOIN using employee and department tables. This query retrieves all departments and matching employees.
SELECT id, name, employee.deptno, deptname
    FROM employee
    RIGHT JOIN department ON employee.deptno = department.deptno;

-- 4. Demonstrate a FULL JOIN operation using the employee and department tables.
SELECT id, name, employee.deptno, deptname
    FROM employee
    FULL JOIN department ON employee.deptno = department.deptno;

-- 5. Demonstrate a cross join operation using the employee and department tables. This query returns all possible combinations of employees and departments.
SELECT id, name, employee.deptno, deptname
    FROM employee
    CROSS JOIN department;

-- 6. This example shows how to use a semi join between employee and department tables. A semi join returns records from the first table (employee) where there is a matching record in the second table (department).
```sql
SELECT *
    FROM employee
    SEMI JOIN department ON employee.deptno = department.deptno;
Copy
  1. ここでは、「employee」と「department」の2つのサンプルテーブルを使用して、内部結合の仕組みを説明します。内側結合は、指定した列が一致する両方のテーブルの行を結合します。

id

name

deptno

deptname

103

Paul

3

エンジニアリング

101

John

1

マーケティング

102

Lisa

2

販売


  1. employeeテーブルとdepartmentテーブルを使用して、左結合の仕組みを説明します。この例では、左(最初の)テーブルのすべての記録を保持しながら、2つのテーブルのデータを結合する方法を理解するのに役立ちます。

id

name

deptno

deptname

105

Chloe

5

null

103

Paul

3

エンジニアリング

101

John

1

マーケティング

102

Lisa

2

販売

104

Evan

4

null

106

Amy

6

null


  1. employeeテーブルとdepartmentテーブルを使用して、 SQL で RIGHT JOIN がどのように機能するかを見てみましょう。

id

name

deptno

deptname

103

Paul

3

エンジニアリング

102

Lisa

2

販売

101

John

1

マーケティング


  1. employeeテーブルとdepartmentテーブルを使用して、完全結合がどのように機能するかを見てみましょう。完全結合は、どちらかのテーブルの一致しない行を含め、両方のテーブルのすべての記録を結合します。

id

name

deptno

deptname

101

John

1

マーケティング

102

Lisa

2

販売

103

Paul

3

エンジニアリング

104

Evan

4

null

105

Chloe

5

null

106

Amy

6

null


  1. employeeテーブルとdepartmentテーブルの間に交差結合を作成し、あるテーブルのすべての行と別のテーブルのすべての行を結合する方法を示します。

id

name

deptno

deptname

105

Chloe

5

エンジニアリング

105

Chloe

5

販売

105

Chloe

5

マーケティング

103

Paul

3

エンジニアリング

103

Paul

3

販売

103

Paul

3

マーケティング

101

John

1

エンジニアリング

101

John

1

販売

101

John

1

マーケティング

102

Lisa

2

エンジニアリング

102

Lisa

2

販売

102

Lisa

2

マーケティング

104

Evan

4

エンジニアリング

104

Evan

4

販売

104

Evan

4

マーケティング

106

Amy

6

エンジニアリング

106

Amy

6

販売

106

Amy

6

マーケティング


  1. employeeテーブルとdepartmentテーブルを使用して、半結合がどのように機能するかを見てみましょう。半結合は、2つ目のテーブルに一致する記録がある場合、1つ目のテーブルから記録を返します。

id

name

deptno

103

Paul

3

101

John

1

102

Lisa

2

Snowflake

-- 1. 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;
 
-- 2. 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;


-- 3. 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;


-- 4. 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;


-- 5. Use employee and department tables to demonstrate cross join.
SELECT id, name, employee.deptno, deptname
    FROM employee
    CROSS JOIN department;

-- 6. Use employee and department tables to demonstrate semi join.
SELECT e.*
    FROM employee e, department d
    WHERE e.deptno = d.deptno;
Copy
  1. ここでは、「employee」と「department」の2つのサンプルテーブルを使用して、内部結合の仕組みを説明します。内部結合は、指定された列に一致する値がある両方のテーブルの記録を結合します。

id

name

deptno

deptname

103

Paul

3

エンジニアリング

101

John

1

マーケティング

102

Lisa

2

販売


  1. employeeテーブルとdepartmentテーブルを使用して左結合を示します。

id

name

deptno

deptname

105

Chloe

5

null

103

Paul

3

エンジニアリング

101

John

1

マーケティング

102

Lisa

2

販売

104

Evan

4

null

106

Amy

6

null


  1. employeeテーブルとdepartmentテーブルを使用して、右結合がどのように機能するかを見てみましょう。

id

name

deptno

deptname

103

Paul

3

エンジニアリング

102

Lisa

2

販売

101

John

1

マーケティング


  1. employeeテーブルとdepartmentテーブルを使用して、完全結合がどのように機能するかを見てみましょう。完全結合は、どちらかのテーブルの一致しない行を含め、両方のテーブルのすべての記録を結合します。

id

name

deptno

deptname

105

Chloe

5

null

103

Paul

3

エンジニアリング

101

John

1

マーケティング

102

Lisa

2

販売

104

Evan

4

null

106

Amy

6

null


  1. employeeテーブルとdepartmentテーブルの間に交差結合を作成し、各従業員がどの部門とペアになるかを示します。この例では、両方のテーブルの行の可能なすべての組み合わせを結合して、交差結合がどのように機能するかを示しています。

id

name

deptno

deptname

105

Chloe

5

エンジニアリング

105

Chloe

5

販売

105

Chloe

5

マーケティング

103

Paul

3

エンジニアリング

103

Paul

3

販売

103

Paul

3

マーケティング

101

John

1

エンジニアリング

101

John

1

販売

101

John

1

マーケティング

102

Lisa

2

エンジニアリング

102

Lisa

2

販売

102

Lisa

2

マーケティング

104

Evan

4

エンジニアリング

104

Evan

4

販売

104

Evan

4

マーケティング

106

Amy

6

エンジニアリング

106

Amy

6

販売

106

Amy

6

マーケティング


  1. employeeテーブルとdepartmentテーブルを使用して、半結合がどのように機能するかを見てみましょう。半結合は、2つ目のテーブルに一致する記録がある場合、1つ目のテーブルから記録を返します。

id

name

deptno

103

Paul

3

101

John

1

102

Lisa

2

既知の問題

問題は見つかりませんでした