sql-debug-env / server /tasks /task_medium.py
darshsingh1's picture
Upload folder using huggingface_hub
2f8e591 verified
Raw History Blame Contribute Delete
2.46 kB
"""
Task 2 — fix-logic (Medium)
Scenario: INNER JOIN should be LEFT JOIN; date filter uses > instead of >=.
Goal: List ALL users (including those with no orders) and their order totals from 2024 onwards.
"""
SCHEMA_STR = """
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
user_id INTEGER NOT NULL,
total REAL NOT NULL,
created_at TEXT NOT NULL -- stored as ISO-8601 date string: 'YYYY-MM-DD'
);
""".strip()
SETUP_SQL = [
"""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
)
""",
"""
CREATE TABLE IF NOT EXISTS orders (
id INTEGER PRIMARY KEY,
user_id INTEGER NOT NULL,
total REAL NOT NULL,
created_at TEXT NOT NULL
)
""",
"INSERT INTO users VALUES (1, 'Alice')",
"INSERT INTO users VALUES (2, 'Bob')",
"INSERT INTO users VALUES (3, 'Carol')", # Carol has no orders
"INSERT INTO users VALUES (4, 'Dave')", # Dave has no orders
# Orders: some before 2024, some on exactly 2024-01-01, some after
"INSERT INTO orders VALUES (1, 1, 150.0, '2023-12-31')", # before cutoff
"INSERT INTO orders VALUES (2, 1, 200.0, '2024-01-01')", # exactly on cutoff (>= must include)
"INSERT INTO orders VALUES (3, 2, 400.0, '2024-06-15')", # after cutoff
"INSERT INTO orders VALUES (4, 2, 50.0, '2023-11-01')", # before cutoff
]
BROKEN_QUERY = """SELECT u.name, o.total
FROM users u
INNER JOIN orders o ON u.id = o.user_id
WHERE o.created_at > '2024-01-01';"""
REFERENCE_QUERY = """SELECT u.name, o.total
FROM users u
LEFT JOIN orders o ON u.id = o.user_id AND o.created_at >= '2024-01-01'
ORDER BY u.id, o.id;"""
TASK_DESCRIPTION = (
"Fix two bugs in the SQL query:\n"
"1. Change INNER JOIN to LEFT JOIN so that ALL users appear, even those with no orders.\n"
"2. Change the date filter from '> 2024-01-01' to '>= 2024-01-01' so orders made exactly on 2024-01-01 are included.\n"
"The result should list every user with their qualifying order totals (NULL for users with no orders in 2024+)."
)
TASK = {
"name": "fix-logic",
"difficulty": "medium",
"schema_str": SCHEMA_STR,
"broken_query": BROKEN_QUERY,
"reference_query": REFERENCE_QUERY,
"task_description": TASK_DESCRIPTION,
"setup_sql": SETUP_SQL,
"grader": "grade_medium",
}