Skip to content

Commit 722ee33

Browse files
TimelordUKclaude
andcommitted
docs: Add comprehensive qualified names example
Created examples/qualified_names.sql demonstrating table alias support across all SQL clauses. Examples include: - Basic CTE with aliases in WHERE and SELECT - ORDER BY with qualified column names - GROUP BY with qualified columns and aggregations - Nested CTEs with multi-level alias usage - String method calls with qualified names - Complex queries combining all clauses with aliases All 7 examples tested and working correctly. Note: HAVING clause currently requires unqualified column names. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 8000cad commit 722ee33

1 file changed

Lines changed: 136 additions & 0 deletions

File tree

examples/qualified_names.sql

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
-- Qualified Names and Table Aliases
2+
-- Demonstrates table alias support across SQL clauses (WHERE, SELECT, ORDER BY, GROUP BY)
3+
4+
-- Example 1: Basic CTE with alias in WHERE and SELECT
5+
-- Shows qualified column references (t.column_name)
6+
WITH data AS (
7+
SELECT value as id, value * 10 as amount, value % 3 as category
8+
FROM RANGE(1, 10)
9+
)
10+
SELECT t.id, t.amount, t.category
11+
FROM data t
12+
WHERE t.amount > 30;
13+
GO
14+
15+
-- Example 2: CTE with alias in ORDER BY
16+
-- Demonstrates sorting by qualified column names
17+
WITH products AS (
18+
SELECT
19+
value as product_id,
20+
value * 100 as price,
21+
CASE WHEN value % 2 = 0 THEN 'even' ELSE 'odd' END as type
22+
FROM RANGE(1, 10)
23+
)
24+
SELECT p.product_id, p.price, p.type
25+
FROM products p
26+
WHERE p.price >= 300
27+
ORDER BY p.price DESC;
28+
GO
29+
30+
-- Example 3: CTE with alias in GROUP BY
31+
-- Shows aggregation with qualified column references
32+
WITH sales AS (
33+
SELECT
34+
value % 4 as region,
35+
value % 3 as product_category,
36+
value * 100 as revenue
37+
FROM RANGE(1, 24)
38+
)
39+
SELECT
40+
s.region,
41+
s.product_category,
42+
COUNT(*) as count,
43+
SUM(s.revenue) as total_revenue,
44+
AVG(s.revenue) as avg_revenue
45+
FROM sales s
46+
GROUP BY s.region, s.product_category
47+
ORDER BY s.region, s.product_category;
48+
GO
49+
50+
-- Example 4: Nested CTEs with aliases
51+
-- Demonstrates multiple CTEs with qualified names at each level
52+
WITH base AS (
53+
SELECT value as id, value % 5 as bucket
54+
FROM RANGE(1, 25)
55+
),
56+
enriched AS (
57+
SELECT b.id, b.bucket, b.id * 10 as score
58+
FROM base b
59+
WHERE b.bucket IN (0, 1, 2)
60+
),
61+
aggregated AS (
62+
SELECT
63+
e.bucket,
64+
COUNT(*) as count,
65+
SUM(e.score) as total_score,
66+
AVG(e.score) as avg_score
67+
FROM enriched e
68+
GROUP BY e.bucket
69+
)
70+
SELECT a.bucket, a.count, a.total_score, a.avg_score
71+
FROM aggregated a
72+
ORDER BY a.avg_score DESC;
73+
GO
74+
75+
-- Example 5: Complex filtering with string methods
76+
-- Shows qualified names with method calls
77+
WITH records AS (
78+
SELECT
79+
value as id,
80+
CASE
81+
WHEN value % 3 = 0 THEN 'type.category.A'
82+
WHEN value % 3 = 1 THEN 'type.category.B'
83+
ELSE 'type.category.C'
84+
END as classification
85+
FROM RANGE(1, 20)
86+
)
87+
SELECT r.id, r.classification
88+
FROM records r
89+
WHERE r.classification.Contains('category.B');
90+
GO
91+
92+
-- Example 6: Multiple aggregations with HAVING
93+
-- Demonstrates qualified names in HAVING clause
94+
-- Note: With RANGE(1, 50) and % 5, each customer has 10 transactions
95+
WITH transactions AS (
96+
SELECT
97+
value % 5 as customer_id,
98+
value * 50 as transaction_amount
99+
FROM RANGE(1, 100)
100+
)
101+
SELECT
102+
t.customer_id,
103+
COUNT(*) as transaction_count,
104+
SUM(t.transaction_amount) as total_amount
105+
FROM transactions t
106+
GROUP BY t.customer_id
107+
HAVING COUNT(*) >= 10
108+
ORDER BY total_amount DESC;
109+
GO
110+
111+
-- Example 7: Combining all clauses with aliases
112+
-- Full-featured query using qualified names throughout
113+
WITH raw_data AS (
114+
SELECT
115+
value as id,
116+
value % 10 as category,
117+
value * 25 as value,
118+
CASE
119+
WHEN value % 2 = 0 THEN 'even'
120+
ELSE 'odd'
121+
END as parity
122+
FROM RANGE(1, 100)
123+
)
124+
SELECT
125+
r.category,
126+
r.parity,
127+
COUNT(*) as record_count,
128+
SUM(r.value) as category_total,
129+
AVG(r.value) as category_average,
130+
MIN(r.value) as min_value,
131+
MAX(r.value) as max_value
132+
FROM raw_data r
133+
WHERE r.value >= 125 AND r.category < 8
134+
GROUP BY r.category, r.parity
135+
ORDER BY r.category ASC, category_total DESC;
136+
GO

0 commit comments

Comments
 (0)