-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoins.sql
More file actions
39 lines (33 loc) · 776 Bytes
/
joins.sql
File metadata and controls
39 lines (33 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
Purpose:
Demonstrate common SQL join patterns used in analytics.
Tables:
customers(customer_id, customer_name)
orders(order_id, customer_id, order_date, order_amount)
*/
-- INNER JOIN: only customers with orders
SELECT
c.customer_id,
c.customer_name,
o.order_id,
o.order_amount
FROM customers c
INNER JOIN orders o
ON c.customer_id = o.customer_id;
-- LEFT JOIN: keep all customers, even those without orders
SELECT
c.customer_id,
c.customer_name,
o.order_id,
o.order_amount
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id;
-- Identify customers with NO orders
SELECT
c.customer_id,
c.customer_name
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
WHERE o.order_id IS NULL;