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
1212SELECT department, COUNT (* ) as count
13- FROM buffer
13+ FROM read_csv( ' ./examples/employees.csv ' )
1414GROUP BY department
1515ORDER 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 ' )
2424GROUP BY department;
2525
2626-- Top 5 highest paid employees
2727SELECT name, department, salary
28- FROM buffer
28+ FROM read_csv( ' ./examples/employees.csv ' )
2929ORDER BY salary DESC
3030LIMIT 5 ;
3131
3232-- Employees hired in 2020
3333SELECT name, department, hire_date
34- FROM buffer
34+ FROM read_csv( ' ./examples/employees.csv ' )
3535WHERE hire_date >= ' 2020-01-01' AND hire_date < ' 2021-01-01'
3636ORDER BY hire_date;
3737
@@ -41,20 +41,20 @@ ORDER BY hire_date;
4141
4242-- Products by category
4343SELECT category, COUNT (* ) as count, AVG (price) as avg_price
44- FROM buffer
44+ FROM read_json( ' ./examples/products.json ' )
4545GROUP BY category;
4646
4747-- High-rated products in stock
4848SELECT name, price, stock, rating
49- FROM buffer
49+ FROM read_json( ' ./examples/products.json ' )
5050WHERE rating >= 4 .5 AND stock > 0
5151ORDER BY rating DESC , price ASC ;
5252
5353-- Total inventory value
5454SELECT category,
5555 SUM (price * stock) as total_value,
5656 SUM (stock) as total_units
57- FROM buffer
57+ FROM read_json( ' ./examples/products.json ' )
5858GROUP BY category
5959ORDER 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
7373SELECT name,
7474 salary,
7575 SUM (salary) OVER (ORDER BY hire_date) as running_total
76- FROM buffer
76+ FROM read_csv( ' ./examples/employees.csv ' )
7777ORDER BY hire_date;
7878
7979-- Department salary percentiles
8080SELECT 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),
9497dept_stats AS (
9598 SELECT department, AVG (salary) as avg_salary
96- FROM buffer
99+ FROM employees
97100 GROUP BY department
98101)
99102SELECT he .name , he .salary , ds .avg_salary ,
@@ -103,9 +106,10 @@ JOIN dept_stats ds ON he.department = ds.department
103106ORDER BY difference DESC ;
104107
105108-- Subqueries: Above average salaries
109+ WITH employees AS (SELECT * FROM read_csv(' ./examples/employees.csv' ))
106110SELECT 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 )
109113ORDER 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 ' )
121125ORDER BY salary;
122126
123127-- ============================================================================
@@ -128,14 +132,14 @@ ORDER BY salary;
128132SELECT 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 ' )
132136ORDER BY years_of_service DESC ;
133137
134138-- Employees by hire quarter
135139SELECT 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 ' )
139143GROUP BY year, quarter
140144ORDER BY year, quarter;
141145
@@ -147,11 +151,11 @@ ORDER BY year, quarter;
147151SELECT 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'
153157SELECT name, department
154- FROM buffer
158+ FROM read_csv( ' ./examples/employees.csv ' )
155159WHERE 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 ' )
170174GROUP 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'})
0 commit comments