Joins in BigQuery

Introductie tot BigQuery

Matthew Forrest

Field CTO

Data joinen in BigQuery

  • INNER JOIN: Waarden komen in beide tabellen voor.

  • LEFT JOIN: Alle rijen in linker tabel, matcht met rechter tabel.

  • RIGHT JOIN: Alle rijen in rechter tabel, matcht met linker tabel.

  • FULL JOIN: Alle rijen uit beide tabellen, matches en non-matches.

  • CROSS JOIN: Elke rij met elke rij uit beide tabellen.

Afbeelding die SQL-joins visueel toont

Introductie tot BigQuery

Voorbeelden van joins uit de praktijk

Customers: linker tabel

Orders: rechter tabel

  • INNER JOIN: Matchende customers en hun orders

  • LEFT JOIN: Alle customers, ook zonder orders

  • RIGHT JOIN: Alle orders, ook met ontbrekende customer ID

  • FULL JOIN: Alle customers en alle orders, ook zonder interactie

  • CROSS JOIN: Koppel elke order aan elke customer zonder voorwaarden

Introductie tot BigQuery

INNER JOIN

  • Geeft alleen matches uit beide datasets
SELECT
  c.customer_id, s.product_name
FROM customers c
-- The INNER keyword is optional
JOIN sales_data s
ON c.customer_id = s.customer_id;
| customer_id | product_name         |
|-------------|----------------------|
| 1           | Bluetooth Headphones |
| 2           | Running Shoes        |
Introductie tot BigQuery

LEFT JOIN

  • Geeft alle rijen uit de LINKER dataset terug
SELECT
  c.customer_id, s.product_name
FROM customers c
LEFT JOIN sales_data s
ON c.customer_id = s.customer_id;
| customer_id | product_name         |
|-------------|----------------------|
| 1           | Bluetooth Headphones |
| 2           | Running Shoes        |
| 3           | null                 |
Introductie tot BigQuery

RIGHT JOIN

  • Geeft alle rijen uit de RECHTER dataset terug
SELECT
  c.customer_id, s.product_name
FROM customers c
RIGHT JOIN sales_data s
ON c.customer_id = s.customer_id;
| customer_id | product_name         |
|-------------|----------------------|
| 1           | Bluetooth Headphones |
| 2           | Running Shoes        |
| null        | External Microphone  |
Introductie tot BigQuery

OUTER JOIN

  • Een "RIGHT-LEFT" join: alle rijen uit zowel RECHTER als LINKER datasets
SELECT
  c.customer_id, s.product_name
FROM customers c
OUTER JOIN sales_data s
ON c.customer_id = s.customer_id;
| customer_id | product_name         |
|-------------|----------------------|
| 1           | Bluetooth Headphones |
| 2           | Running Shoes        |
| 3           | null                 |
| null        | External Microphone  |
Introductie tot BigQuery

SELF of CROSS JOIN

  • Een cartesische join: elke rij met elke rij
SELECT
  c.customer_id,
  s.product_name,

-- Adding table names separated 
-- by a comma is a CROSS JOIN
-- Order is determined by the
-- left table, here "customers"
FROM customers c, sales_data s;
| customer_id | product_name         |
|-------------|----------------------|
| 1           | Bluetooth Headphones |
| 1           | null                 |
| 2           | Bluetooth Headphones |
| 2           | null                 |
| 3           | null                 |
| 3           | Bluetooth Headphones |
Introductie tot BigQuery

Joins en UNNEST

  • Ook gebruikt om geunneste data te joinen
SELECT
  c.customer_id,
  payments.method
FROM customers c, 
UNNEST(
  customers.payment_methods
) payments;
| customer_id | product_name |
|-------------|--------------|
| 1           | Visa         |
| 1           | Mastercard   |
| 1           | Venmo        |
| 1           | Paypal       |
| 2           | Amex         |
| 2           | Visa         |
Introductie tot BigQuery

Laten we oefenen!

Introductie tot BigQuery

Preparing Video For Download...