Joins i BigQuery

Introduktion till BigQuery

Matthew Forrest

Field CTO

Att joina data i BigQuery

  • INNER JOIN: Värden finns i båda tabellerna.

  • LEFT JOIN: Alla rader i vänster tabell, matchar höger tabell.

  • RIGHT JOIN: Alla rader i höger tabell, matchar vänster tabell.

  • FULL JOIN: Alla rader från båda tabellerna, matchande och icke-matchande.

  • CROSS JOIN: Varje rad matchas med varje rad från båda tabellerna.

Bild som visar SQL-joins visuellt

Introduktion till BigQuery

Verkliga exempel på joins

Customers: vänster tabell

Orders: höger tabell

  • INNER JOIN: Matchande kunder och deras beställningar

  • LEFT JOIN: Visar alla kunder, även de utan beställningar

  • RIGHT JOIN: Visar alla beställningar, även om kund-ID saknas

  • FULL JOIN: Visar alla kunder och beställningar, även utan koppling

  • CROSS JOIN: Matchar varje beställning med varje kund utan villkor

Introduktion till BigQuery

INNER JOIN

  • Returnerar endast matchande resultat från båda datamängderna
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        |
Introduktion till BigQuery

LEFT JOIN

  • Returnerar alla rader från den VÄNSTRA datamängden
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                 |
Introduktion till BigQuery

RIGHT JOIN

  • Returnerar alla rader från den HÖGRA datamängden
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  |
Introduktion till BigQuery

OUTER JOIN

  • En "HÖGER-VÄNSTER"-join: alla rader från både HÖGER och VÄNSTER datamängd
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  |
Introduktion till BigQuery

SELF- eller CROSS JOIN

  • En kartesisk join – varje rad med varje rad
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 |
Introduktion till BigQuery

Joins och UNNEST

  • Används även för att joina unnestade data
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         |
Introduktion till BigQuery

Nu kör vi en övning!

Introduktion till BigQuery

Preparing Video For Download...