Skip to content
Preptima
mediumCodingConceptEntryMidSenior

Find me every customer with no order in the last thirty days, then tell me which ways of writing that get NULLs wrong.

Use NOT EXISTS for customers with no recent order because it expresses an anti-join and avoids NULL traps. NOT IN can return no rows if the subquery contains NULL, and LEFT JOIN becomes wrong if filters are placed in WHERE. Use this SQL answer to show the decision, trade-off, and evidence rather than a memorised definition. It also connects null semantics to the point an interviewer is testing.

4 min readUpdated 2026-08-15Target archetype: Big Tech, Enterprise Captive, Product Startup

Answer

Short answer

For “customers with no recent order,” prefer NOT EXISTS. NOT IN is unsafe when the subquery can contain NULL, because SQL three-valued logic can make the predicate unknown for every row. A LEFT JOIN anti-join is valid only if the date filter stays in the join condition.

Was this answer helpful?

Likely follow-ups

  • Now return each of those customers with the date of their most recent order, including the ones who have never ordered at all.
  • Why can NOT EXISTS be executed as an anti-join when NOT IN generally cannot?
  • The window must be whole calendar days in the customer's own timezone. What changes in the query?
  • The orders table soft-deletes. Where exactly does that predicate go in each of your three versions?

Related questions

Further reading

Related Topics