Skip to content
>_sqlbuddy

SQL Topic

Set Operations

UNION, UNION ALL, INTERSECT, and EXCEPT combine results of separate queries. UNION ALL is the tool for stacking rows from two sides — like counting friend relationships in both directions.

When is Set Operations used?

Combining two result sets, counting relationships that exist in either direction, de-duplicating across tables.

Core syntax

SELECT requester_id AS id FROM request_accepted UNION ALL SELECT accepter_id FROM request_accepted.

Common mistakes

  • Using UNION (dedupes) when UNION ALL (keeps duplicates) is needed for counting.
  • Mismatched column counts or types between branches.
  • Forgetting ORDER BY must apply to the whole combined result.

Practice Set Operations questions

Work through them in order — each question builds on the last.

Related topics

Interview preparation