Bird
0
0

Consider this query:

medium📝 Debug Q14 of 15
PostgreSQL - Set Operations and Advanced Queries
Consider this query:
SELECT id FROM table1 UNION ALL SELECT id FROM table2;

It returns duplicate rows. How can you fix it to return only unique rows?
AReplace UNION ALL with UNION
BAdd DISTINCT inside each SELECT
CUse INTERSECT instead of UNION ALL
DAdd GROUP BY id at the end
Step-by-Step Solution
Solution:
  1. Step 1: Understand UNION ALL vs UNION

    UNION ALL returns all rows including duplicates; UNION removes duplicates.
  2. Step 2: Fix query to remove duplicates

    Replacing UNION ALL with UNION removes duplicates automatically.
  3. Final Answer:

    Replace UNION ALL with UNION -> Option A
  4. Quick Check:

    UNION removes duplicates, UNION ALL does not [OK]
Quick Trick: Use UNION (not UNION ALL) to remove duplicates [OK]
Common Mistakes:
  • Using UNION ALL when duplicates are unwanted
  • Adding DISTINCT inside each SELECT unnecessarily
  • Using INTERSECT which returns only common rows

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes