Data & SQL21 categories
Find where this join is duplicating rows
Your totals are suspiciously round multiples of what they should be.
My results are inflated and I think a join is multiplying rows. 1. For each join in the query below, state the expected relationship: one-to-one, one-to-many, or many-to-many. Say which side can have duplicates on the join key. 2. Identify every join where the key is not unique on at least one side. These are the suspects. 3. Point out any join condition that is missing a component of a composite key, or that joins on a column which is only unique per tenant, per day, or per version. 4. Look for a missing filter on a slowly-changing or versioned table where multiple historical rows match. Then give me a diagnostic query for each suspect: group by the join key, count, and return only counts greater than one. Finally, give the corrected query, and say whether to fix it by deduplicating, by aggregating before joining, or by tightening the key. Query:
Making it yours
- Paste your own material where the prompt asks for it. Everything above that line is instruction, not content.
- Delete any rule that does not apply to you. A shorter prompt that fits beats a thorough one that does not.
- If the answer comes back vague, add a line saying what you do not want. Constraints work better than encouragement.
Correctness checks. Catching the join that duplicates rows, the NULL that drops them, and the GROUP BY that lies.
Related prompts
Check this query before I run itBefore running anything expensive, destructive, or going into a report.Check my aggregation for grouping trapsAny query with GROUP BY that feeds a number someone will act on.Hunt every NULL trap in this queryWhen a count came back lower than expected and nothing looks wrong.Can any of these joins multiply rows?Totals look inflated and you suspect the join before the data.Am I counting the right thing?A count feeds a number someone will act on.Where could NULL change this result?A count came back lower than expected and nothing looks wrong.
Keep this one
Phrasery saves prompts with a right-click and puts them back the same way. Add this to your own library in one click.
Add to your browser โ free