Summary on Unnesting Queries
DISTINCT at top level: Can ignore duplicates.
- Can sometimes infer DISTINCT at top level! (e.g. subquery clause matches at most one tuple)
DISTINCT in subquery w/o DISTINCT at top: Hard to convert.
Subqueries inside OR: Hard to convert.
ALL subqueries: Hard to convert.
- EXISTS and ANY are just like IN.
Aggregates in subqueries: Tricky.
Good news: Some systems now rewrite under the covers (e.g. DB2).