G-XR8P2XJ088

AI-Written SQL: A WHERE Filter Can Undo Your LEFT JOIN

You ask an AI assistant for every customer, with any paid orders shown beside them. The generated SQL contains LEFT JOIN, so the result looks reassuring. Yet customers without paid orders disappear. The query runs successfully while narrowing the report beyond the request.

The detail to inspect is where the paid-order condition appears. A condition used to decide which orders match a customer has a different role from a condition used to keep or discard the joined rows. A small example makes that distinction visible.

Write the population rule before the query

For a fictional digital-product studio, the requested report starts with all customers in the supplied customer table. Show their paid orders where those exist. Retain customers without a paid order, leaving the order fields empty. This is the example’s reporting rule, not a claim that every business needs that population.

The deliberately small fixture contains three customers and two orders. Customer IDs and order IDs are unique in their respective tables. There are no date, currency or amount conditions in this example.

  • Customer C1 has order O1 with status paid.
  • Customer C2 has order O2 with status pending.
  • Customer C3 has no order in the fixture.

The expected result retains C1, C2 and C3. Only C1 should have a paid-order ID displayed. C2 is an important test case: having an order that does not qualify is different from having no order at all.

LEFT JOIN does not settle later filtering

The PostgreSQL 18 joined-table documentation explains that the ON condition determines matching rows. A left outer join retains unmatched rows from the left side with null values for the right-side columns. It also demonstrates that putting a restriction in WHERE can produce a different result from putting it in ON.

Consider this first query, where c represents customers and o represents orders:

SELECT c.id, o.id FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.status = 'paid' ORDER BY c.id;

In a local in-memory test using SQLite 3.53.1, it returned only C1 paired with O1. C2’s pending-order row failed the paid condition. C3’s unmatched row did not satisfy it either. SQLite’s SELECT documentation states that WHERE retains rows only when its expression evaluates to true; false or null excludes them.

The presence of LEFT JOIN therefore does not establish that every customer survives the completed query. Read the matching condition and the final filter together. The first query answers a narrower question than the studio asked.

Why adding “or no order” is still incomplete

A plausible repair is to retain rows where the order is paid or its ID is null. The second tested query changed the filter to:

WHERE o.status = 'paid' OR o.id IS NULL

That version returned C1 with O1 and C3 with no order ID. C2 was still absent. Its pending order matched the original customer-ID join, so its joined row contained O2. It was neither paid nor a row with a null order ID.

The repair handles the customer with no order in this fixture, while missing the customer whose existing order does not qualify. If the test data contained only C1 and C3, this omission would remain hidden.

This result is useful when reviewing AI-generated corrections. A changed query can look more careful and still fail the intended population rule. Ask which example distinguishes the proposed repair from the required behaviour, then check that example explicitly.

Put the qualification into the matching rule

For this report, the paid status belongs in the rule defining which orders match customers. The third tested query was:

SELECT c.id, o.id FROM customers c LEFT JOIN orders o ON o.customer_id = c.id AND o.status = 'paid' ORDER BY c.id;

It returned C1 with O1, C2 with a null order ID and C3 with a null order ID. All three expected customers remained. C2’s pending order did not qualify as a match, so the left join retained C2 without a paid-order record.

SQLite’s documentation explains this through the logical relationship between ON processing, the addition of unmatched rows and subsequent WHERE filtering. This is a way to understand the result, rather than a promise about the database engine’s physical execution plan.

Moving every condition into ON is not a general repair rule. A condition defining which customers belong in the report needs its own treatment. The right placement follows the stated question: what defines the left-side population, what defines a qualifying match and what should be excluded afterwards?

Read an empty match precisely

In this fixture, the empty order fields mean no paid order matched the specified rule. They do not distinguish C2, which has a pending order, from C3, which has no order. If the business needs that distinction, the report must request and calculate it separately.

Equally, the example does not certify that the source tables contain every real customer or order. A query can preserve all customers in its input while still relying on incomplete input. Keep the scope of the test attached to its conclusion.

There is another boundary if a customer has several paid orders: this query can return several rows for that customer. Do not describe it as guaranteeing one row per customer. A report requiring a summary would need a separate design and corresponding checks.

MaryChuks.com’s guide to keeping each order counted once examines that related question of row meaning after a join. Preserving the intended population and preserving the intended level of detail are both necessary review tasks.

Ask the assistant to explain the missing cases

A proposed instruction is: “Draft a read-only query starting from all supplied customers and showing paid orders where present. Keep customers with only pending orders and customers with no orders. Explain ON and WHERE separately, and show the expected rows for this fixture.”

Compare the explanation with the actual SQL and run it in an appropriate test environment. The local results reported here establish the behaviour of these three queries on this fixture in SQLite. They do not test a production schema or a generated query in another database.

  • Check a customer with a qualifying order.
  • Check a customer with only a non-qualifying order.
  • Check a customer with no order.
  • Add multiple qualifying orders if the report’s required row structure needs testing.
  • Compare customer IDs in the result with the expected population.

Keep the population rule, sample data and observed output beside the query. When a later edit adds another filter, recheck the customer cases that should remain visible. Successful execution is useful; the retained records tell you whether the query still answers the intended question.

Sources

Documentation checked on 5 November 2026. The fictional fixture and three query variants were tested locally with SQLite 3.53.1. No production database was accessed.


Discover more from Marychuks.com AI, Psychology, Business & CreativeVerse

Subscribe to get the latest posts sent to your email.

One thought on “AI-Written SQL: A WHERE Filter Can Undo Your LEFT JOIN”

Leave a Reply

Discover more from Marychuks.com AI, Psychology, Business & CreativeVerse

Subscribe now to keep reading and get access to the full archive.

Continue reading