Join in BigQuery

Introduzione a BigQuery

Matthew Forrest

Field CTO

Unire i dati in BigQuery

  • INNER JOIN: Valori presenti in entrambe le tabelle.

  • LEFT JOIN: Tutte le righe della tabella sinistra, abbinate alla destra.

  • RIGHT JOIN: Tutte le righe della tabella destra, abbinate alla sinistra.

  • FULL JOIN: Tutte le righe di entrambe le tabelle, abbinate e non abbinate.

  • CROSS JOIN: Ogni riga con ogni riga di entrambe le tabelle.

Immagine che mostra visivamente i join in SQL

Introduzione a BigQuery

Esempi reali di join

Customers: tabella sinistra

Orders: tabella destra

  • INNER JOIN: Clienti con i relativi ordini

  • LEFT JOIN: Mostra tutti i clienti, anche senza ordini

  • RIGHT JOIN: Mostra tutti gli ordini, anche con ID cliente mancante

  • FULL JOIN: Mostra tutti i clienti e tutti gli ordini, anche senza corrispondenze

  • CROSS JOIN: Abbina ogni ordine a ogni cliente senza condizioni

Introduzione a BigQuery

INNER JOIN

  • Restituisce solo risultati corrispondenti da entrambi gli insiemi di dati
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        |
Introduzione a BigQuery

LEFT JOIN

  • Restituisce tutte le righe dall'insieme di dati SINISTRO
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                 |
Introduzione a BigQuery

RIGHT JOIN

  • Restituisce tutte le righe dall'insieme di dati DESTRO
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  |
Introduzione a BigQuery

OUTER JOIN

  • Un join "DESTRA-SINISTRA": tutte le righe da entrambi gli insiemi di dati DESTRA e SINISTRA
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  |
Introduzione a BigQuery

SELF o CROSS JOIN

  • Un join cartesiano: ogni riga con ogni riga
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 |
Introduzione a BigQuery

Join e UNNEST

  • Usato anche per unire dati dopo UNNEST
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         |
Introduzione a BigQuery

Esercitiamoci!

Introduzione a BigQuery

Preparing Video For Download...