Product case study · November 30, 2026

Ask Claude Code to keep zero-task projects in a SQL report

Fix a SQLite report that drops projects with no open tasks. Give Claude Code a failing fixture, protect zero counts, and verify the repaired query.

Reading time
6 min
Checked
Nov 30, 2026
Three paper project trays sit beneath a charcoal sieve, with blue task cards in one tray and two empty trays retained.
An empty project still deserves a row in the report.
Bottom line

A report needs tests for the rows it must retain, not just the totals it happens to return. Make zero-count projects part of the acceptance contract.

A project with no open tasks should be the easiest row in a workload report: zero. Yet a small SQL mistake can make that project disappear entirely, leaving a report that looks plausible precisely because the missing rows are invisible.

I would put an empty project in the test fixture before asking an agent to polish this query. Checking only the busy project lets two different bugs slip through: dropping the quiet ones, or counting each quiet one as having a task.

This lab uses fictional project data, Python 3 with SQLite support, Bash, and an installed, signed-in Claude Code. The query and tests were run locally; the agent prompt is an authored exercise, not a recorded Claude Code session. Official sources were checked November 30, 2026.

Give the report three very different projects

Create a temporary practice folder. The database will live only in memory, so this exercise needs no production connection.

join_lab=$(mktemp -d /tmp/claude-left-join.XXXXXX)
cd "$join_lab"
cat > report.sql <<'SQL'
SELECT p.id, p.name, COUNT(*) AS open_tasks
FROM projects AS p
LEFT JOIN tasks AS t ON t.project_id = p.id
WHERE t.status = 'open'
GROUP BY p.id, p.name
ORDER BY p.id;
SQL

The report’s contract is simple: return every project once, ordered by ID, with its number of open tasks. A project with only closed tasks and a project with no tasks must both return zero. Names are display labels; two projects may share one.

Save this fixed test file in the same folder:

cat > test_report.py <<'PY'
from pathlib import Path
import sqlite3
import unittest


class ReportTests(unittest.TestCase):
    def setUp(self):
        self.db = sqlite3.connect(":memory:")
        self.addCleanup(self.db.close)
        self.db.executescript("""
            CREATE TABLE projects (
                id INTEGER PRIMARY KEY,
                name TEXT NOT NULL
            );
            CREATE TABLE tasks (
                id INTEGER PRIMARY KEY,
                project_id INTEGER NOT NULL,
                status TEXT NOT NULL
            );
            INSERT INTO projects VALUES
                (1, 'Website'), (2, 'Archive'), (3, 'Prototype');
            INSERT INTO tasks VALUES
                (11, 1, 'open'), (12, 1, 'open'),
                (13, 1, 'closed'), (21, 2, 'closed');
        """)

    def rows(self):
        query = Path(__file__).with_name("report.sql").read_text()
        return self.db.execute(query).fetchall()

    def test_every_project_and_exact_counts(self):
        self.assertEqual(self.rows(), [
            (1, 'Website', 2), (2, 'Archive', 0), (3, 'Prototype', 0)
        ])

    def test_no_tasks_still_returns_every_project(self):
        self.db.execute("DELETE FROM tasks")
        self.assertEqual(self.rows(), [
            (1, 'Website', 0), (2, 'Archive', 0), (3, 'Prototype', 0)
        ])

    def test_same_name_does_not_merge_projects(self):
        self.db.execute("INSERT INTO projects VALUES (4, 'Website')")
        self.assertEqual(self.rows(), [
            (1, 'Website', 2), (2, 'Archive', 0),
            (3, 'Prototype', 0), (4, 'Website', 0)
        ])

    def test_no_projects_returns_no_rows(self):
        self.db.execute("DELETE FROM tasks")
        self.db.execute("DELETE FROM projects")
        self.assertEqual(self.rows(), [])


if __name__ == "__main__":
    unittest.main()
PY
python3 -m unittest -v test_report.py

The deliberately broken query fails three tests and passes the empty-database check. In the first case, it returns only (1, 'Website', 2). Archive and Prototype disappear for different reasons; keeping both in the fixture is useful.

Ask for a repair with a fixed definition of done

Start claude from the practice folder and send:

Read report.sql and test_report.py. Run:
python3 -m unittest -v test_report.py

Repair only report.sql. Do not change the tests, fixture, or schema.
Return every project exactly once, ordered by project ID, with the
number of open tasks. Projects with closed tasks only or no tasks
must have zero. Projects with equal names must stay separate.

Explain why the original LEFT JOIN loses rows and why COUNT(*)
can be wrong even after the filter is moved. Rerun the unchanged
four tests and report the actual result. Do not install packages
or connect to external databases.

Anthropic’s common workflows describe giving Claude a bug and verifying its fix. Here, the fixture makes the acceptance criteria executable. Review requested actions under your configured permissions; the prompt itself does not isolate the process.

The two changes belong together

This reference query passed all four tests locally:

SELECT p.id, p.name, COUNT(t.id) AS open_tasks
FROM projects AS p
LEFT JOIN tasks AS t
    ON t.project_id = p.id AND t.status = 'open'
GROUP BY p.id, p.name
ORDER BY p.id;

SQLite’s SELECT documentation explains that a left join adds a null-filled right side when there is no match. Its WHERE filtering happens afterward. In the broken query, the status condition removes those rows. Moving the status condition into ON limits which tasks match while retaining the project.

That repairs membership, but it leaves a counting trap. COUNT(*) counts rows; COUNT(t.id) counts non-null task IDs. The placeholder row for a project without an open task therefore contributes zero to the latter. Count the task key, not a nullable description that could omit a real task.

Keep the GROUP BY tied to project identity. Grouping only by name would merge the two Website projects in the third test. Keeping the name alongside the ID also makes the selected columns explicit.

Reject the almost-right patch

An agent may retain the original filter and append OR t.id IS NULL. That rescues Prototype, which has no tasks, but still loses Archive: its closed task matched the join, so there was no null-filled row to rescue. The closed-only fixture exposes that shortcut.

Run the four tests yourself after the edit. If zero-count projects return one, inspect the aggregate. If Archive is still absent, inspect the filter placement. If the tests unexpectedly passed before the repair, check the contents of report.sql and confirm you created a fresh practice folder.

If the agent edited the fixture, restore the test block above before judging the query. If you want to replay the exercise, replace only the practice folder’s report.sql with the original query. No real database needs restoring.

This contract deliberately includes every project. A different report might intentionally show only projects with open work. Decide that requirement before changing joins. And if you later join task comments or tags, add a fixture with multiple child rows: this lab does not establish that counts survive additional one-to-many joins.

For this report, a zero is useful information. Make the agent prove it can keep one.

Sources

Checked November 30, 2026:

Put this to work

Where to filter child rows when every parent must remain in a report.

Try

Ask Claude Code to repair report.sql against a fixed in-memory SQLite fixture.

Prove it worked

Run the same four tests before and after the query change.

Keep in view

  • Reproduce a LEFT JOIN that loses projects after a WHERE filter.
  • Catch the tempting repair that counts an empty project as one task.
  • Verify projects with open tasks, closed tasks and no tasks at all.
Learn the workflow: Start your first Claude Code project session