{"success":true,"name":"use-exists-for-sql-presence-checks-instead-of-in-or-joins","description":"SQL query asks 'does a related row exist?' for filtering.","category":"ai-brimind","path":"ai-brimind/use-exists-for-sql-presence-checks-instead-of-in-or-joins/SKILL.md","file":"SKILL.md","content":"---\nname: use-exists-for-sql-presence-checks-instead-of-in-or-joins\ndescription: \"SQL query asks 'does a related row exist?' for filtering.\"\n---\n\n# Use EXISTS for SQL Presence Checks Instead of IN or JOINs\n\n## When to use\nProject uses SQL, and you are filtering rows by whether related rows exist, and about to use IN (subquery) or a JOIN just for the check.\n\n## Example request\nJOIN used only to test presence → parent rows duplicate per child and DISTINCT gets bolted on; EXISTS asks the question without multiplying rows. IN with a huge subquery list → the intent was 'is there at least one', which EXISTS expresses and engines can stop early on. NOT IN against a subquery containing NULL → the whole filter returns nothing; NOT EXISTS is immune to that trap. 'Customers with at least one order' → the sentence itself says EXISTS; the SQL should read like the requirement.\n\n## Instructions\nAct like a developer who picks the construct that says what the query means. When the requirement is 'rows where a related row does (or doesn't) exist': write EXISTS (SELECT 1 FROM ... WHERE ...) or NOT EXISTS — it tests presence without joining rows in (so no fan-out, no DISTINCT patch) and it handles NULLs safely where NOT IN famously does not. Reserve IN for small, fixed value lists, and JOINs for when you actually need the related row's columns. After choosing, sanity-check the plan with EXPLAIN on realistic data rather than assuming one form is always faster — but correctness and readability of intent come first. Only finalize once the construct matches the sentence of the requirement and the NULL behavior is safe by design.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1126\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}