-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04.lateral-joins.sql
More file actions
126 lines (107 loc) · 3.29 KB
/
04.lateral-joins.sql
File metadata and controls
126 lines (107 loc) · 3.29 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
-- =====================================================
-- Latest Order Per Customer
-- Demonstrates: Correlated subquery using LATERAL
-- Very common production pattern
-- =====================================================
SELECT c.c_custkey,
c.c_name,
o.*
FROM customer c
CROSS JOIN LATERAL (
SELECT *
FROM orders o
WHERE o.o_custkey = c.c_custkey
ORDER BY o.o_orderdate DESC
LIMIT 1
) o;
-- =====================================================
-- Top 3 Orders Per Customer
-- Demonstrates: LIMIT inside LATERAL
-- Efficient alternative to complex window filtering
-- =====================================================
SELECT c.c_custkey,
c.c_name,
o.*
FROM customer c
CROSS JOIN LATERAL (
SELECT *
FROM orders o
WHERE o.o_custkey = c.c_custkey
ORDER BY o.o_totalprice DESC
LIMIT 3
) o;
-- =====================================================
-- Customer With Their Total + Latest Order
-- Demonstrates: Combining aggregation + LATERAL
-- =====================================================
SELECT c.c_custkey,
c.c_name,
totals.total_spent,
latest.o_orderdate
FROM customer c
JOIN LATERAL (
SELECT SUM(o_totalprice) AS total_spent
FROM orders o
WHERE o.o_custkey = c.c_custkey
) totals ON TRUE
LEFT JOIN LATERAL (
SELECT o_orderdate
FROM orders o
WHERE o.o_custkey = c.c_custkey
ORDER BY o_orderdate DESC
LIMIT 1
) latest ON TRUE;
-- =====================================================
-- Exploding Data with LATERAL (Row Expansion Pattern)
-- Demonstrates: Using LATERAL with set-returning logic
-- =====================================================
SELECT c.c_custkey,
o.*
FROM customer c
LEFT JOIN LATERAL (
SELECT *
FROM orders o
WHERE o.o_custkey = c.c_custkey
) o ON TRUE;
-- =====================================================
-- LATERAL for Conditional Filtering
-- Demonstrates: Compute first, then filter
-- =====================================================
SELECT c.c_custkey,
stats.order_count
FROM customer c
JOIN LATERAL (
SELECT COUNT(*) AS order_count
FROM orders o
WHERE o.o_custkey = c.c_custkey
) stats ON stats.order_count > 5;
-- =====================================================
-- LATERAL with Aggregation
-- Demonstrates: Per-row computation pattern
-- =====================================================
SELECT c.c_custkey,
summary.total_spent
FROM customer c
JOIN LATERAL (
SELECT SUM(o_totalprice) AS total_spent
FROM orders o
WHERE o.o_custkey = c.c_custkey
) summary ON TRUE
ORDER BY summary.total_spent DESC;
-- =====================================================
-- LATERAL with Top-N + Join
-- Demonstrates: Real-world analytical pattern
-- =====================================================
SELECT r.r_name,
top_customers.*
FROM region r
JOIN nation n ON n.n_regionkey = r.r_regionkey
JOIN customer c ON c.c_nationkey = n.n_nationkey
JOIN LATERAL (
SELECT c.c_custkey,
SUM(o.o_totalprice) AS total_spent
FROM orders o
WHERE o.o_custkey = c.c_custkey
GROUP BY c.c_custkey
) top_customers ON TRUE
ORDER BY r.r_name, total_spent DESC NULLS LAST;