{"success":true,"name":"choose-the-right-sql-join-instead-of-filtering-in-code","description":"Combining data from multiple SQL tables and unsure how.","category":"ai-brimind","path":"ai-brimind/choose-the-right-sql-join-instead-of-filtering-in-code/SKILL.md","file":"SKILL.md","content":"---\nname: choose-the-right-sql-join-instead-of-filtering-in-code\ndescription: \"Combining data from multiple SQL tables and unsure how.\"\n---\n\n# Choose the Right SQL JOIN Instead of Filtering in Code\n\n## When to use\nProject uses SQL, and you are about to fetch multiple tables separately and merge them in application code, or you're picking a JOIN type without thinking about missing matches.\n\n## Example request\nFetching two tables and matching them in a loop → the database joins millions of rows faster than your code; JOIN is its core job. INNER JOIN on optional data → customers without orders disappear from the report; LEFT JOIN keeps the parent rows. Rows suddenly duplicated after a JOIN → one-to-many multiplied them; the relationship's cardinality was never stated. WHERE clause on the right table after LEFT JOIN → it quietly turned back into an INNER JOIN; that filter belongs in the ON clause.\n\n## Instructions\nAct like a developer who lets the database do what it's built for. Before merging data in code: check whether a JOIN expresses it — it almost always does. Then reason before typing: state the relationship (one-to-one, one-to-many) and whether rows without a match must still appear; that answer picks INNER vs LEFT JOIN. Know that filtering a LEFT JOIN's right table in WHERE cancels the LEFT — put such conditions in ON. Predict the result's row count from the cardinality, run it, and compare. Only keep in-code merging if you can name a real reason the database can't do it, and explain why the chosen JOIN preserves exactly the rows the feature needs.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1032\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}