Does Your SQL AND/OR Filter Match the Intended Business Rule?

A team wants open requests assigned to either the design group or the operations group. The query returns closed operations requests as well. Every individual condition looks reasonable, yet their combination expresses the wrong rule.
Mixed AND and OR conditions deserve a small logic review. Parentheses should communicate the business grouping, and the test cases should include a record that qualifies for one alternative while failing the condition intended to apply to all alternatives.
Translate the rule before reading the query
Write the requirement as two parts: the request must be open, and its group must be either design or operations. This makes the common condition and the alternatives visible.
Now compare two expressions: open AND design OR operations, versus open AND (design OR operations). In the first, operations can qualify independently of open under the relevant precedence rules. In the second, open applies to both group alternatives.
PostgreSQL's operator-precedence table places AND above OR. It also explains the use of parentheses to control grouping. Do not rely on a reader remembering that precedence when explicit grouping would make the intended rule clearer.
An illustrative four-record worksheet
Use an open design request, an open operations request, a closed operations request, and an open finance request. The intended result contains the first two only.
The closed operations request is the revealing case. It satisfies the operations alternative but fails the shared open condition. If it appears, the query has allowed one branch to bypass that condition. The open finance request tests the opposite boundary: passing the shared condition is not enough without a permitted group.
Review each shared restriction
Look for conditions concerning the report period, portfolio, status, or other required scope. Ask whether each condition applies to the whole expression or only to one branch.
For example, adding a new alternative to an existing filter can accidentally leave it outside a previously understood restriction. Review the full expression after edits rather than only the line that changed. Indentation helps readers, but indentation alone does not change the query's grouping.
Add an unknown-value case separately
If a field can be null, include that situation explicitly and write the intended outcome. Do not assume a two-valued worksheet fully covers SQL's treatment of unknown values.
A business owner might want missing status to enter a review queue rather than the open-request report. That is an additional policy decision. Keep it visible instead of silently treating every missing value as false or replacing it with an invented status.
Make the review executable and understandable
Give the technical reviewer a fixture with stable identifiers and the expected selected identifiers. Give the business reviewer the same cases in plain language. Both should agree before the implementation is approved.
When the query runs, compare membership, not just the number of returned records. A wrongly included closed request and a wrongly excluded open request can leave the total unchanged while making the report unreliable.
Preserve the fixture alongside the report definition. If new groups or states are introduced later, extend the cases to cover their relationship to the common restriction.
Do not turn a report filter into an access control
This exercise checks business selection logic. If the query also touches permission boundaries, those controls require their own verified design and testing. A correctly parenthesized report condition alone does not establish authorization.
Use only data and environments you are permitted to review. Synthetic fixtures are usually sufficient to expose the logical error without copying real customer records into a debugging document.
Close with the bypass question
For every OR branch, ask: can a record enter through this branch while failing a condition that should be mandatory? That question catches the practical defect more directly than asking whether the SQL looks tidy.
The final handoff should contain the plain-language rule, grouped expression, and expected cases. This makes future changes easier to assess and gives the team a clear explanation for each included record.
Continue with What Does Selecting Two Search Filters Actually Mean? for the related planning decision.
The published FutureOS introduction describes the project as being in prelaunch. Use this guide to prepare a concrete project discussion; it does not announce an available FutureOS feature.
Prepared with AI assistance. Primary sources checked on October 25, 2026. Examples and procedures are illustrative editorial guidance, not FutureOS product announcements or an independently reviewed assessment.



Comments