Skip to content

Commit 5accf60

Browse files
committed
docs(notes): add IBM DB2 advanced and JCC documentation
1 parent bddbe7f commit 5accf60

File tree

2 files changed

+533
-0
lines changed

2 files changed

+533
-0
lines changed
+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
---
2+
title: IBM DB2 SQL Advanced Operations
3+
lang: en
4+
layout: post
5+
audio: false
6+
translated: false
7+
generated: true
8+
---
9+
10+
I'll teach you some advanced SQL operations in IBM DB2. Let's dive into nested queries, complex conditions, and advanced grouping techniques.
11+
12+
## Nested Queries (Subqueries)
13+
14+
Subqueries are queries embedded within other queries, often used in WHERE, HAVING, or FROM clauses.
15+
16+
### In WHERE Clause:
17+
```sql
18+
SELECT employee_id, last_name, salary
19+
FROM employees
20+
WHERE salary > (SELECT AVG(salary) FROM employees);
21+
```
22+
This returns employees with salary above the average.
23+
24+
### Correlated Subquery:
25+
```sql
26+
SELECT d.dept_name, e.last_name
27+
FROM departments d
28+
WHERE EXISTS (
29+
SELECT 1
30+
FROM employees e
31+
WHERE e.dept_id = d.dept_id
32+
AND e.salary > 100000
33+
);
34+
```
35+
This finds departments that have at least one employee earning over $100,000.
36+
37+
### In FROM Clause (Derived Tables):
38+
```sql
39+
SELECT dept_name, avg_salary
40+
FROM (
41+
SELECT d.dept_name, AVG(e.salary) as avg_salary
42+
FROM departments d
43+
JOIN employees e ON d.dept_id = e.dept_id
44+
GROUP BY d.dept_name
45+
) AS dept_stats
46+
WHERE avg_salary > 75000;
47+
```
48+
49+
## Complex WHERE Conditions
50+
51+
### Using CASE in WHERE:
52+
```sql
53+
SELECT product_id, product_name, price
54+
FROM products
55+
WHERE
56+
CASE
57+
WHEN category = 'Electronics' THEN price < 1000
58+
WHEN category = 'Furniture' THEN price < 500
59+
ELSE price < 200
60+
END;
61+
```
62+
63+
### Multiple Conditions with Combinations:
64+
```sql
65+
SELECT *
66+
FROM orders
67+
WHERE
68+
(status = 'Pending' AND created_date > CURRENT DATE - 7 DAYS)
69+
OR
70+
(status = 'Processing' AND created_date > CURRENT DATE - 14 DAYS)
71+
OR
72+
(customer_id IN (SELECT customer_id FROM premium_customers));
73+
```
74+
75+
### Using BETWEEN and IN:
76+
```sql
77+
SELECT order_id, customer_id, total_amount
78+
FROM orders
79+
WHERE
80+
order_date BETWEEN '2024-01-01' AND '2024-03-31'
81+
AND
82+
shipping_method IN ('Express', 'Priority')
83+
AND
84+
total_amount > 1000;
85+
```
86+
87+
## Advanced GROUP BY Operations
88+
89+
### GROUP BY with ROLLUP:
90+
```sql
91+
SELECT
92+
COALESCE(region, 'All Regions') as region,
93+
COALESCE(country, 'All Countries') as country,
94+
SUM(sales) as total_sales
95+
FROM sales_data
96+
GROUP BY ROLLUP(region, country);
97+
```
98+
This produces subtotals for each group level and a grand total.
99+
100+
### GROUP BY with CUBE:
101+
```sql
102+
SELECT
103+
COALESCE(year, 0) as year,
104+
COALESCE(quarter, 0) as quarter,
105+
COALESCE(product_line, 'All Products') as product_line,
106+
SUM(revenue) as total_revenue
107+
FROM sales_data
108+
GROUP BY CUBE(year, quarter, product_line);
109+
```
110+
This generates all possible combinations of the specified dimensions.
111+
112+
### GROUPING SETS:
113+
```sql
114+
SELECT
115+
product_category,
116+
region,
117+
SUM(sales) as total_sales
118+
FROM sales_data
119+
GROUP BY GROUPING SETS(
120+
(product_category, region),
121+
(product_category),
122+
(region),
123+
()
124+
);
125+
```
126+
This specifies multiple grouping combinations in a single query.
127+
128+
## Common Table Expressions (CTEs)
129+
130+
```sql
131+
WITH regional_sales AS (
132+
SELECT region, SUM(amount) as total_sales
133+
FROM orders
134+
GROUP BY region
135+
),
136+
top_regions AS (
137+
SELECT region
138+
FROM regional_sales
139+
ORDER BY total_sales DESC
140+
FETCH FIRST 3 ROWS ONLY
141+
)
142+
SELECT o.order_id, o.customer_id, o.amount
143+
FROM orders o
144+
JOIN top_regions tr ON o.region = tr.region
145+
WHERE o.amount > 1000;
146+
```
147+
148+
## Advanced JOINS
149+
150+
### Self JOIN:
151+
```sql
152+
SELECT e.employee_id, e.last_name as employee, m.last_name as manager
153+
FROM employees e
154+
LEFT JOIN employees m ON e.manager_id = m.employee_id;
155+
```
156+
157+
### Multiple JOINS with Complex Conditions:
158+
```sql
159+
SELECT
160+
c.customer_name,
161+
p.product_name,
162+
o.order_date,
163+
SUM(od.quantity * od.unit_price) as line_total
164+
FROM customers c
165+
JOIN orders o ON c.customer_id = o.customer_id
166+
JOIN order_details od ON o.order_id = od.order_id
167+
JOIN products p ON od.product_id = p.product_id
168+
WHERE
169+
o.order_date > '2024-01-01'
170+
AND p.category IN ('Electronics', 'Appliances')
171+
AND c.country = 'USA'
172+
GROUP BY c.customer_name, p.product_name, o.order_date
173+
HAVING SUM(od.quantity * od.unit_price) > 500
174+
ORDER BY line_total DESC;
175+
```
176+
177+
## Window Functions
178+
179+
```sql
180+
SELECT
181+
dept_id,
182+
employee_id,
183+
salary,
184+
AVG(salary) OVER (PARTITION BY dept_id) as dept_avg_salary,
185+
salary - AVG(salary) OVER (PARTITION BY dept_id) as diff_from_avg,
186+
RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) as salary_rank
187+
FROM employees;
188+
```
189+
190+
Would you like me to elaborate on any of these techniques or provide more specific examples for a particular DB2 scenario?

0 commit comments

Comments
 (0)