Skip to content

Commit 5675b50

Browse files
release: v0.10.0
1 parent e05ba60 commit 5675b50

File tree

19 files changed

+3267
-1394
lines changed

19 files changed

+3267
-1394
lines changed

README.md

Lines changed: 383 additions & 439 deletions
Large diffs are not rendered by default.

examples/example_queries.sql

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
-- Example DuckDB queries for the sample data files
2-
-- Open the data files in Neovim and run these queries using :DuckDB
2+
-- Select a query and run it using visual selection + <leader>dq
33

44
-- ============================================================================
55
-- BASIC QUERIES (employees.csv)
66
-- ============================================================================
77

88
-- Get all employees
9-
SELECT * FROM buffer;
9+
SELECT * FROM read_csv('./examples/employees.csv');
1010

1111
-- Count employees by department
1212
SELECT department, COUNT(*) as count
13-
FROM buffer
13+
FROM read_csv('./examples/employees.csv')
1414
GROUP BY department
1515
ORDER BY count DESC;
1616

@@ -20,18 +20,18 @@ SELECT department,
2020
AVG(salary) as avg_salary,
2121
MIN(salary) as min_salary,
2222
MAX(salary) as max_salary
23-
FROM buffer
23+
FROM read_csv('./examples/employees.csv')
2424
GROUP BY department;
2525

2626
-- Top 5 highest paid employees
2727
SELECT name, department, salary
28-
FROM buffer
28+
FROM read_csv('./examples/employees.csv')
2929
ORDER BY salary DESC
3030
LIMIT 5;
3131

3232
-- Employees hired in 2020
3333
SELECT name, department, hire_date
34-
FROM buffer
34+
FROM read_csv('./examples/employees.csv')
3535
WHERE hire_date >= '2020-01-01' AND hire_date < '2021-01-01'
3636
ORDER BY hire_date;
3737

@@ -41,20 +41,20 @@ ORDER BY hire_date;
4141

4242
-- Products by category
4343
SELECT category, COUNT(*) as count, AVG(price) as avg_price
44-
FROM buffer
44+
FROM read_json('./examples/products.json')
4545
GROUP BY category;
4646

4747
-- High-rated products in stock
4848
SELECT name, price, stock, rating
49-
FROM buffer
49+
FROM read_json('./examples/products.json')
5050
WHERE rating >= 4.5 AND stock > 0
5151
ORDER BY rating DESC, price ASC;
5252

5353
-- Total inventory value
5454
SELECT category,
5555
SUM(price * stock) as total_value,
5656
SUM(stock) as total_units
57-
FROM buffer
57+
FROM read_json('./examples/products.json')
5858
GROUP BY category
5959
ORDER BY total_value DESC;
6060

@@ -67,33 +67,36 @@ SELECT name,
6767
department,
6868
salary,
6969
RANK() OVER (PARTITION BY department ORDER BY salary DESC) as dept_rank
70-
FROM buffer;
70+
FROM read_csv('./examples/employees.csv');
7171

7272
-- Running total of salaries
7373
SELECT name,
7474
salary,
7575
SUM(salary) OVER (ORDER BY hire_date) as running_total
76-
FROM buffer
76+
FROM read_csv('./examples/employees.csv')
7777
ORDER BY hire_date;
7878

7979
-- Department salary percentiles
8080
SELECT department,
8181
name,
8282
salary,
8383
PERCENT_RANK() OVER (PARTITION BY department ORDER BY salary) as percentile
84-
FROM buffer;
84+
FROM read_csv('./examples/employees.csv');
8585

8686
-- ============================================================================
8787
-- ADVANCED QUERIES
8888
-- ============================================================================
8989

9090
-- CTEs: High earners analysis
91-
WITH high_earners AS (
92-
SELECT * FROM buffer WHERE salary > 80000
91+
WITH employees AS (
92+
SELECT * FROM read_csv('./examples/employees.csv')
93+
),
94+
high_earners AS (
95+
SELECT * FROM employees WHERE salary > 80000
9396
),
9497
dept_stats AS (
9598
SELECT department, AVG(salary) as avg_salary
96-
FROM buffer
99+
FROM employees
97100
GROUP BY department
98101
)
99102
SELECT he.name, he.salary, ds.avg_salary,
@@ -103,9 +106,10 @@ JOIN dept_stats ds ON he.department = ds.department
103106
ORDER BY difference DESC;
104107

105108
-- Subqueries: Above average salaries
109+
WITH employees AS (SELECT * FROM read_csv('./examples/employees.csv'))
106110
SELECT name, department, salary
107-
FROM buffer
108-
WHERE salary > (SELECT AVG(salary) FROM buffer)
111+
FROM employees
112+
WHERE salary > (SELECT AVG(salary) FROM employees)
109113
ORDER BY salary DESC;
110114

111115
-- CASE expressions: Salary categories
@@ -117,7 +121,7 @@ SELECT name,
117121
WHEN salary < 100000 THEN 'Senior'
118122
ELSE 'Principal'
119123
END as level
120-
FROM buffer
124+
FROM read_csv('./examples/employees.csv')
121125
ORDER BY salary;
122126

123127
-- ============================================================================
@@ -128,14 +132,14 @@ ORDER BY salary;
128132
SELECT name,
129133
hire_date,
130134
DATEDIFF('year', CAST(hire_date AS DATE), CURRENT_DATE) as years_of_service
131-
FROM buffer
135+
FROM read_csv('./examples/employees.csv')
132136
ORDER BY years_of_service DESC;
133137

134138
-- Employees by hire quarter
135139
SELECT EXTRACT(YEAR FROM CAST(hire_date AS DATE)) as year,
136140
EXTRACT(QUARTER FROM CAST(hire_date AS DATE)) as quarter,
137141
COUNT(*) as hires
138-
FROM buffer
142+
FROM read_csv('./examples/employees.csv')
139143
GROUP BY year, quarter
140144
ORDER BY year, quarter;
141145

@@ -147,11 +151,11 @@ ORDER BY year, quarter;
147151
SELECT SPLIT_PART(name, ' ', 1) as first_name,
148152
department,
149153
salary
150-
FROM buffer;
154+
FROM read_csv('./examples/employees.csv');
151155

152156
-- Search for names containing 'son'
153157
SELECT name, department
154-
FROM buffer
158+
FROM read_csv('./examples/employees.csv')
155159
WHERE name LIKE '%son%';
156160

157161
-- ============================================================================
@@ -166,30 +170,39 @@ SELECT department,
166170
STDDEV(salary) as std_dev,
167171
MIN(salary) as min,
168172
MAX(salary) as max
169-
FROM buffer
173+
FROM read_csv('./examples/employees.csv')
170174
GROUP BY department;
171175

172176
-- ============================================================================
173-
-- JSON QUERIES (products.json)
177+
-- JSONL QUERIES (orders.jsonl)
174178
-- ============================================================================
175179

176-
-- Filter by nested properties (if JSON has nested structure)
177-
-- Example: SELECT * FROM buffer WHERE price > 100;
180+
-- Order totals by customer
181+
SELECT customer_id, COUNT(*) as orders, SUM(total) as total_spent
182+
FROM read_json('./examples/orders.jsonl', format='newline_delimited')
183+
GROUP BY customer_id
184+
ORDER BY total_spent DESC;
185+
186+
-- Orders by status
187+
SELECT status, COUNT(*) as count
188+
FROM read_json('./examples/orders.jsonl', format='newline_delimited')
189+
GROUP BY status;
178190

179191
-- ============================================================================
180-
-- MULTI-BUFFER QUERIES
181-
-- (Open both employees.csv and products.json, then run these)
192+
-- MULTI-TABLE QUERIES
182193
-- ============================================================================
183194

184-
-- Cross join (Cartesian product) - be careful with large datasets!
185-
-- SELECT e.name, p.name as product
186-
-- FROM buffer('employees.csv') e, buffer('products.json') p
187-
-- LIMIT 10;
195+
-- Join employees and products (cross join example)
196+
SELECT e.name as employee, p.name as product, p.price
197+
FROM read_csv('./examples/employees.csv') e,
198+
read_json('./examples/products.json') p
199+
WHERE e.department = 'Engineering'
200+
LIMIT 10;
188201

189202
-- ============================================================================
190203
-- EXPORT EXAMPLES (via Lua)
191204
-- ============================================================================
192205

193-
-- Run these from Neovim using Lua:
194-
-- :lua require('duckdb').query('SELECT * FROM buffer WHERE salary > 90000', {export = '/tmp/high_earners.csv', format = 'csv'})
195-
-- :lua require('duckdb').query('SELECT * FROM buffer', {export = '/tmp/all_employees.json', format = 'json'})
206+
-- Run these from Neovim command line:
207+
-- :lua require('duckdb').query("SELECT * FROM read_csv('./examples/employees.csv') WHERE salary > 90000", {export = '/tmp/high_earners.csv', format = 'csv'})
208+
-- :lua require('duckdb').query("SELECT * FROM read_csv('./examples/employees.csv')", {export = '/tmp/all_employees.json', format = 'json'})

examples/my-products.csv

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
id,name,category,price,stock,rating
2+
1,Laptop Pro 15,Electronics,1299.99,45,4.5
3+
2,Wireless Mouse,Electronics,24.99,230,4.2
4+
3,Office Desk,Furniture,299.99,15,4.7
5+
4,Ergonomic Chair,Furniture,399.99,8,4.8
6+
5,"Monitor 27""",Electronics,349.99,62,4.6
7+
6,Keyboard Mechanical,Electronics,89.99,120,4.4
8+
7,Desk Lamp,Furniture,45.99,75,4.3
9+
8,USB-C Hub,Electronics,49.99,95,4.1

0 commit comments

Comments
 (0)