File size: 2,456 Bytes
2f8e591
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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",
}