Repository navigation
Expand file tree
/
Copy pathcsv_generator.py
More file actions
198 lines (175 loc) · 7.41 KB
/
Copy pathcsv_generator.py
File metadata and controls
198 lines (175 loc) · 7.41 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import pandas as pd
data = [
# Customers
("Show all customer names and emails",
"SELECT first_name, last_name, email FROM customers"),
("List phone numbers of all customers",
"SELECT phone FROM customers"),
("Find the email of customer named Alice Smith",
"SELECT email FROM customers WHERE first_name = 'Alice' AND last_name = 'Smith'"),
# Products
("Get all product names and prices",
"SELECT name, price FROM products"),
("Which products belong to the Electronics category?",
"SELECT name FROM products WHERE category = 'Electronics'"),
("List products that cost more than 500",
"SELECT name, price FROM products WHERE price > 500"),
# Orders
("Show all orders with their status",
"SELECT order_id, status FROM orders"),
("Find all orders placed by customer with ID 1",
"SELECT * FROM orders WHERE customer_id = 1"),
("Get order IDs and dates for delivered orders",
"SELECT order_id, order_date FROM orders WHERE status = 'Delivered'"),
# Joins: Orders + Customers
("List all customer names with their order status",
"SELECT c.first_name, c.last_name, o.status FROM customers c JOIN orders o ON c.customer_id = o.customer_id"),
("Which customers placed orders in September 2025?",
"SELECT DISTINCT c.first_name, c.last_name FROM customers c JOIN orders o ON c.customer_id = o.customer_id WHERE o.order_date LIKE '2025-09%'"),
# Order Items
("Get all products and quantities for order 1",
"SELECT p.name, oi.quantity FROM order_items oi JOIN products p ON oi.product_id = p.product_id WHERE oi.order_id = 1"),
("List total quantity of items per order",
"SELECT order_id, SUM(quantity) AS total_qty FROM order_items GROUP BY order_id"),
# Employees
("Show all employees and their departments",
"SELECT first_name, last_name, department FROM employees"),
("List employees with salary above 5000",
"SELECT first_name, last_name FROM employees WHERE salary > 5000"),
# Departments
("Get all departments and their locations",
"SELECT name, location FROM departments"),
("Which department is located in Chicago?",
"SELECT name FROM departments WHERE location = 'Chicago'"),
# Join Employees + Departments
("List employees with their department location",
"SELECT e.first_name, e.last_name, d.location FROM employees e JOIN departments d ON e.department = d.name")
]
df = pd.DataFrame(data, columns=["text_query", "sql_command"])
df.to_csv("synthetic_text2sql.csv", index=False)
import pandas as pd
# New complex questions and queries
extra_data = [
(
"Which customers ordered more than 2 different products?",
"SELECT c.first_name, c.last_name, COUNT(DISTINCT oi.product_id) AS product_count "
"FROM customers c "
"JOIN orders o ON c.customer_id = o.customer_id "
"JOIN order_items oi ON o.order_id = oi.order_id "
"GROUP BY c.customer_id "
"HAVING COUNT(DISTINCT oi.product_id) > 2"
),
(
"List the total amount spent by each customer, ordered by amount descending.",
"SELECT c.first_name, c.last_name, SUM(oi.quantity * p.price) AS total_spent "
"FROM customers c "
"JOIN orders o ON c.customer_id = o.customer_id "
"JOIN order_items oi ON o.order_id = oi.order_id "
"JOIN products p ON oi.product_id = p.product_id "
"GROUP BY c.customer_id "
"ORDER BY total_spent DESC"
),
(
"Show the most expensive product in each category.",
"SELECT category, name, price "
"FROM products p1 "
"WHERE price = ( "
" SELECT MAX(p2.price) FROM products p2 WHERE p2.category = p1.category)"
),
(
"Which employees earn more than the average salary of their department?",
"SELECT e.first_name, e.last_name, e.department, e.salary "
"FROM employees e "
"JOIN departments d ON e.department = d.name "
"WHERE e.salary > ( "
" SELECT AVG(e2.salary) "
" FROM employees e2 "
" WHERE e2.department = e.department)"
),
(
"Find customers who have never placed an order.",
"SELECT c.first_name, c.last_name "
"FROM customers c "
"LEFT JOIN orders o ON c.customer_id = o.customer_id "
"WHERE o.order_id IS NULL"
),
(
"Which products were ordered the most times (by total quantity)?",
"SELECT p.name, SUM(oi.quantity) AS total_quantity "
"FROM products p "
"JOIN order_items oi ON p.product_id = oi.product_id "
"GROUP BY p.product_id "
"ORDER BY total_quantity DESC"
),
(
"Get the number of orders for each order status.",
"SELECT status, COUNT(*) AS order_count "
"FROM orders "
"GROUP BY status"
),
(
"Find employees working in departments located in New York.",
"SELECT e.first_name, e.last_name, e.department "
"FROM employees e "
"JOIN departments d ON e.department = d.name "
"WHERE d.location = 'New York'"
),
(
"Which customers ordered Electronics products in September 2025?",
"SELECT DISTINCT c.first_name, c.last_name "
"FROM customers c "
"JOIN orders o ON c.customer_id = o.customer_id "
"JOIN order_items oi ON o.order_id = oi.order_id "
"JOIN products p ON oi.product_id = p.product_id "
"WHERE p.category = 'Electronics' "
"AND o.order_date LIKE '2025-09%'"
),
(
"Show average product price per category, sorted highest to lowest.",
"SELECT category, AVG(price) AS avg_price "
"FROM products "
"GROUP BY category "
"ORDER BY avg_price DESC"
),
(
"List customers and their total number of orders, but only if they have more than 1 order.",
"SELECT c.first_name, c.last_name, COUNT(o.order_id) AS total_orders "
"FROM customers c "
"JOIN orders o ON c.customer_id = o.customer_id "
"GROUP BY c.customer_id "
"HAVING COUNT(o.order_id) > 1"
),
(
"Which employees earn above 5500 and are not in IT?",
"SELECT first_name, last_name, department, salary "
"FROM employees "
"WHERE salary > 5500 AND department <> 'IT'"
),
(
"For each customer, list their most recent order date.",
"SELECT c.first_name, c.last_name, MAX(o.order_date) AS latest_order "
"FROM customers c "
"JOIN orders o ON c.customer_id = o.customer_id "
"GROUP BY c.customer_id"
),
(
"What is the average quantity of products per order?",
"SELECT AVG(total_items) AS avg_items_per_order "
"FROM ( "
" SELECT order_id, SUM(quantity) AS total_items "
" FROM order_items "
" GROUP BY order_id "
") t"
),
(
"List the departments along with the number of employees in each.",
"SELECT d.name AS department, COUNT(e.employee_id) AS employee_count "
"FROM departments d "
"LEFT JOIN employees e ON e.department = d.name "
"GROUP BY d.department_id"
),
]
# Append to CSV
df_extra = pd.DataFrame(extra_data, columns=["text_query", "sql_command"])
df_extra.to_csv("synthetic_text2sql.csv", mode="a", header=False, index=False)
print("✅ Added", len(extra_data), "new examples to synthetic_text2sql.csv")