this post was submitted on 10 Aug 2025
9 points (100.0% liked)
SQL
1051 readers
1 users here now
Related Fediverse communities:
- #sql on Mastodon
- #postgresql on Mastodon
- c/PostgreSQL on programming.dev
Icon base by Delapouite under CC BY 3.0 with modifications to add a gradient
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
The UNION removing any dups here is what makes them the same - the top query would never have duplicates as written.
Ah but that's true ONLY IF the table doesn't itself contain duplicates. Quick example:
So we could change the query to use UNION ALL, which does include duplicates. In that case, the returned rows are the same ONLY IF the rows returned by the left side of the UNION do not overlap those returned by the right side, otherwise it will return more rows.
For completeness, here's an example where the two queries in the UNION do not return any of the same rows: