sql-debug-env / server /tasks /task_hard.py
darshsingh1's picture
Upload folder using huggingface_hub
2f8e591 verified
Raw History Blame Contribute Delete
3.22 kB
"""
Task 3 — fix-and-optimize (Hard)
Scenario: Correlated subquery uses wrong alias (e.dept instead of employees.dept)
AND the outer query is missing a GROUP BY clause.
Goal: For each department, return the average salary of employees earning above
their department's average. Must GROUP BY dept.
Bonus: +0.05 if the agent uses a CTE (WITH clause) instead of a correlated subquery.
"""
SCHEMA_STR = """
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
dept TEXT NOT NULL,
salary REAL NOT NULL
);
""".strip()
SETUP_SQL = [
"""
CREATE TABLE IF NOT EXISTS employees (
id INTEGER PRIMARY KEY,
dept TEXT NOT NULL,
salary REAL NOT NULL
)
""",
# Engineering: avg = (90000+80000+70000)/3 = 80000
# Above avg: 90000, 80000 — avg of those = 85000
"INSERT INTO employees VALUES (1, 'Engineering', 90000)",
"INSERT INTO employees VALUES (2, 'Engineering', 80000)",
"INSERT INTO employees VALUES (3, 'Engineering', 70000)",
# Marketing: avg = (60000+50000)/2 = 55000
# Above avg: 60000 — avg of those = 60000
"INSERT INTO employees VALUES (4, 'Marketing', 60000)",
"INSERT INTO employees VALUES (5, 'Marketing', 50000)",
# HR: avg = (45000+55000+40000)/3 = 46667
# Above avg: 55000 — avg of those = 55000
"INSERT INTO employees VALUES (6, 'HR', 45000)",
"INSERT INTO employees VALUES (7, 'HR', 55000)",
"INSERT INTO employees VALUES (8, 'HR', 40000)",
]
# The broken query: wrong alias 'e.dept' in subquery (table alias 'e' not defined inside subquery)
# and missing GROUP BY in outer query
BROKEN_QUERY = """SELECT dept, AVG(salary)
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees WHERE dept = e.dept);"""
# Fixed query (standard correlated subquery approach — valid SQL)
REFERENCE_QUERY = """SELECT dept, AVG(salary) as avg_above_dept_avg
FROM employees e
WHERE salary > (SELECT AVG(salary) FROM employees WHERE dept = e.dept)
GROUP BY dept
ORDER BY dept;"""
# CTE alternative (also correct — eligible for bonus)
CTE_REFERENCE_QUERY = """WITH dept_avgs AS (
SELECT dept, AVG(salary) AS dept_avg
FROM employees
GROUP BY dept
)
SELECT e.dept, AVG(e.salary) as avg_above_dept_avg
FROM employees e
JOIN dept_avgs d ON e.dept = d.dept
WHERE e.salary > d.dept_avg
GROUP BY e.dept
ORDER BY e.dept;"""
TASK_DESCRIPTION = (
"Fix two bugs in this SQL query:\n"
"1. The correlated subquery references alias 'e.dept', but alias 'e' is not defined in the subquery scope. "
"Move the alias to the outer query: 'FROM employees e'.\n"
"2. The outer query is missing a GROUP BY clause — add 'GROUP BY dept'.\n"
"Goal: For each department, return the average salary of employees who earn MORE than their "
"department's average salary.\n"
"BONUS: Rewrite using a CTE (WITH clause) for extra credit (+0.05 score)."
)
TASK = {
"name": "fix-and-optimize",
"difficulty": "hard",
"schema_str": SCHEMA_STR,
"broken_query": BROKEN_QUERY,
"reference_query": REFERENCE_QUERY,
"cte_reference_query": CTE_REFERENCE_QUERY,
"task_description": TASK_DESCRIPTION,
"setup_sql": SETUP_SQL,
"grader": "grade_hard",
}