File size: 11,103 Bytes
64ccc23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
[
  {
    "id": "hard_001",
    "difficulty": "hard",
    "schema": {
      "tables": [
        "users(id, name, email, age, city, active)",
        "orders(id, user_id, product_id, amount, status, created_at)",
        "products(id, name, category, price, stock)",
        "reviews(id, user_id, product_id, rating, body, created_at)"
      ]
    },
    "intent": "Find users who bought a product and also reviewed it, show user name and product name",
    "broken_query": "SELECT u.name, p.name FROM users u JOIN orders o ON u.id = o.user_id JOIN reviews r ON u.id = r.user_id JOIN products p ON p.id = o.product_id",
    "correct_query": "SELECT DISTINCT u.name, p.name FROM users u JOIN orders o ON u.id = o.user_id JOIN reviews r ON u.id = r.user_id AND o.product_id = r.product_id JOIN products p ON p.id = o.product_id",
    "error_type": "join",
    "min_turns_expected": 4,
    "clarification_answers": {
      "how to match bought and reviewed same product": "join reviews on both user_id AND product_id",
      "reviews columns": "user_id, product_id, rating, body",
      "orders columns": "user_id, product_id, amount, status"
    }
  },
  {
    "id": "hard_002",
    "difficulty": "hard",
    "schema": {
      "tables": [
        "users(id, name, email, age, city, active)",
        "orders(id, user_id, product_id, amount, status, created_at)",
        "products(id, name, category, price, stock)",
        "reviews(id, user_id, product_id, rating, body, created_at)"
      ]
    },
    "intent": "Get products with average rating above 4.0 that are also currently in stock, ordered by average rating descending",
    "broken_query": "SELECT p.name, AVG(r.rating) FROM products p JOIN reviews r ON p.id = r.product_id WHERE AVG(r.rating) > 4.0 AND stock > 0 ORDER BY AVG(r.rating) DESC",
    "correct_query": "SELECT p.name, AVG(r.rating) AS avg_rating FROM products p JOIN reviews r ON p.id = r.product_id WHERE p.stock > 0 GROUP BY p.id, p.name HAVING AVG(r.rating) > 4.0 ORDER BY avg_rating DESC",
    "error_type": "logic",
    "min_turns_expected": 4,
    "clarification_answers": {
      "stock column location": "stock is in the products table",
      "aggregate filter placement": "use HAVING for AVG filter, WHERE for stock filter"
    }
  },
  {
    "id": "hard_003",
    "difficulty": "hard",
    "schema": {
      "tables": [
        "users(id, name, email, age, city, active)",
        "orders(id, user_id, product_id, amount, status, created_at)",
        "products(id, name, category, price, stock)",
        "tags(id, product_id, tag)"
      ]
    },
    "intent": "Find users who ordered a product tagged as 'ergonomic', return unique user names",
    "broken_query": "SELECT u.name FROM users u JOIN orders o ON u.id = o.user_id JOIN tags t ON o.id = t.product_id WHERE t.tag = 'ergonomic'",
    "correct_query": "SELECT DISTINCT u.name FROM users u JOIN orders o ON u.id = o.user_id JOIN tags t ON o.product_id = t.product_id WHERE t.tag = 'ergonomic'",
    "error_type": "join",
    "min_turns_expected": 4,
    "clarification_answers": {
      "tags join key": "tags.product_id links to orders.product_id, not orders.id",
      "tag values": "office, writing, computer, accessories, ergonomic, lighting, mechanical",
      "orders to tags": "join on orders.product_id = tags.product_id"
    }
  },
  {
    "id": "hard_004",
    "difficulty": "hard",
    "schema": {
      "tables": [
        "users(id, name, email, age, city, active)",
        "orders(id, user_id, product_id, amount, status, created_at)",
        "products(id, name, category, price, stock)",
        "reviews(id, user_id, product_id, rating, body, created_at)"
      ]
    },
    "intent": "Using a CTE, rank users by total amount spent, show top 3 with their rank",
    "broken_query": "WITH ranked AS (SELECT user_id, SUM(amount) spent FROM orders GROUP BY user_id) SELECT u.name, spent FROM ranked JOIN users u ON ranked.id = u.id ORDER BY spent DESC LIMIT 3",
    "correct_query": "WITH ranked AS (SELECT user_id, SUM(amount) AS spent FROM orders GROUP BY user_id) SELECT u.name, r.spent FROM ranked r JOIN users u ON r.user_id = u.id ORDER BY r.spent DESC LIMIT 3",
    "error_type": "subquery",
    "min_turns_expected": 4,
    "clarification_answers": {
      "CTE alias": "the CTE is aliased as ranked, access with r.user_id not ranked.id",
      "join column in CTE": "CTE exposes user_id column, not id"
    }
  },
  {
    "id": "hard_005",
    "difficulty": "hard",
    "schema": {
      "tables": [
        "users(id, name, email, age, city, active)",
        "orders(id, user_id, product_id, amount, status, created_at)",
        "products(id, name, category, price, stock)",
        "reviews(id, user_id, product_id, rating, body, created_at)"
      ]
    },
    "intent": "Find the category with the highest average review rating",
    "broken_query": "SELECT category, AVG(rating) FROM products JOIN reviews ON products.id = reviews.id GROUP BY category ORDER BY AVG(rating) DESC LIMIT 1",
    "correct_query": "SELECT p.category, AVG(r.rating) AS avg_rating FROM products p JOIN reviews r ON p.id = r.product_id GROUP BY p.category ORDER BY avg_rating DESC LIMIT 1",
    "error_type": "join",
    "min_turns_expected": 4,
    "clarification_answers": {
      "join key for products and reviews": "reviews.product_id = products.id, not reviews.id"
    }
  },
  {
    "id": "hard_006",
    "difficulty": "hard",
    "schema": {
      "tables": [
        "users(id, name, email, age, city, active)",
        "orders(id, user_id, product_id, amount, status, created_at)",
        "products(id, name, category, price, stock)",
        "reviews(id, user_id, product_id, rating, body, created_at)"
      ]
    },
    "intent": "Find products that have been ordered but have received no reviews",
    "broken_query": "SELECT DISTINCT p.name FROM products p JOIN orders o ON p.id = o.product_id WHERE p.id NOT IN (SELECT product_id FROM reviews WHERE product_id IS NOT NULL)",
    "correct_query": "SELECT DISTINCT p.name FROM products p JOIN orders o ON p.id = o.product_id WHERE p.id NOT IN (SELECT product_id FROM reviews)",
    "error_type": "logic",
    "min_turns_expected": 4,
    "clarification_answers": {
      "null in subquery NOT IN": "NOT IN with NULLs returns no rows; remove the IS NOT NULL filter or use NOT EXISTS",
      "reviews product_id nullable": "product_id in reviews is NOT NULL by design in this schema"
    }
  },
  {
    "id": "hard_007",
    "difficulty": "hard",
    "schema": {
      "tables": [
        "users(id, name, email, age, city, active)",
        "orders(id, user_id, product_id, amount, status, created_at)",
        "products(id, name, category, price, stock)",
        "tags(id, product_id, tag)"
      ]
    },
    "intent": "Get products that have BOTH the 'office' tag and the 'computer' tag",
    "broken_query": "SELECT p.name FROM products p JOIN tags t ON p.id = t.product_id WHERE t.tag = 'office' AND t.tag = 'computer'",
    "correct_query": "SELECT p.name FROM products p JOIN tags t1 ON p.id = t1.product_id AND t1.tag = 'office' JOIN tags t2 ON p.id = t2.product_id AND t2.tag = 'computer'",
    "error_type": "logic",
    "min_turns_expected": 4,
    "clarification_answers": {
      "why single join fails": "a single row cannot have two different tag values; join tags twice",
      "tags structure": "one row per tag per product"
    }
  },
  {
    "id": "hard_008",
    "difficulty": "hard",
    "schema": {
      "tables": [
        "users(id, name, email, age, city, active)",
        "orders(id, user_id, product_id, amount, status, created_at)",
        "products(id, name, category, price, stock)",
        "reviews(id, user_id, product_id, rating, body, created_at)"
      ]
    },
    "intent": "For each city, find the most popular product category (by number of orders from users in that city)",
    "broken_query": "SELECT u.city, p.category, COUNT(*) FROM users u JOIN orders o ON u.id = o.user_id JOIN products p ON o.product_id = p.id GROUP BY u.city ORDER BY COUNT(*) DESC",
    "correct_query": "WITH city_cat AS (SELECT u.city, p.category, COUNT(*) AS cnt FROM users u JOIN orders o ON u.id = o.user_id JOIN products p ON o.product_id = p.id GROUP BY u.city, p.category) SELECT city, category, cnt FROM city_cat cc WHERE cnt = (SELECT MAX(cnt) FROM city_cat WHERE city = cc.city)",
    "error_type": "subquery",
    "min_turns_expected": 5,
    "clarification_answers": {
      "most popular means": "category with the highest order count for that city",
      "how to get top per group": "use a CTE with a correlated subquery to filter max per city",
      "products join key": "orders.product_id = products.id"
    }
  },
  {
    "id": "hard_009",
    "difficulty": "hard",
    "schema": {
      "tables": [
        "users(id, name, email, age, city, active)",
        "orders(id, user_id, product_id, amount, status, created_at)",
        "products(id, name, category, price, stock)",
        "reviews(id, user_id, product_id, rating, body, created_at)"
      ]
    },
    "intent": "Find users who have spent more than the average total spending across all users",
    "broken_query": "SELECT u.name, SUM(o.amount) FROM users u JOIN orders o ON u.id = o.user_id GROUP BY u.id HAVING SUM(o.amount) > AVG(SUM(o.amount))",
    "correct_query": "WITH totals AS (SELECT user_id, SUM(amount) AS total FROM orders GROUP BY user_id) SELECT u.name, t.total FROM users u JOIN totals t ON u.id = t.user_id WHERE t.total > (SELECT AVG(total) FROM totals)",
    "error_type": "subquery",
    "min_turns_expected": 5,
    "clarification_answers": {
      "nested aggregate": "SQLite does not allow AVG(SUM()); use a CTE or subquery",
      "approach": "first compute per-user totals in a CTE, then compare against AVG of those totals"
    }
  },
  {
    "id": "hard_010",
    "difficulty": "hard",
    "schema": {
      "tables": [
        "users(id, name, email, age, city, active)",
        "orders(id, user_id, product_id, amount, status, created_at)",
        "products(id, name, category, price, stock)",
        "reviews(id, user_id, product_id, rating, body, created_at)",
        "tags(id, product_id, tag)"
      ]
    },
    "intent": "Find active users from cities where the total order amount exceeds 300, who have also written at least one review",
    "broken_query": "SELECT u.name FROM users u JOIN orders o ON u.id = o.user_id JOIN reviews r ON u.id = r.user_id WHERE u.active = 1 AND SUM(o.amount) > 300 GROUP BY u.city",
    "correct_query": "SELECT DISTINCT u.name FROM users u JOIN orders o ON u.id = o.user_id JOIN reviews r ON u.id = r.user_id WHERE u.active = 1 AND u.city IN (SELECT u2.city FROM users u2 JOIN orders o2 ON u2.id = o2.user_id GROUP BY u2.city HAVING SUM(o2.amount) > 300)",
    "error_type": "subquery",
    "min_turns_expected": 5,
    "clarification_answers": {
      "city total vs user total": "SUM > 300 is per city, not per user",
      "active filter": "active = 1 in users table",
      "review requirement": "user must exist in reviews table at least once"
    }
  }
]