error404notFound
error404notFound

Reputation: 75

SQL UNION vs OR, INTERSECT vs AND

I would like to know what the difference between an INTERSECT and an AND statement as well as a UNION statement and a OR statement is. Is there a specific scenario where either one is recommended to use and can could I always use a OR/AND instead of an UNION/ INTERSECT ?

Upvotes: 3

Views: 3958

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562270

Use AND or OR between terms in a WHERE clause. If the complete boolean expression evaluates as true, then the row is included in the query's result set.

WHERE country = 'Canada' AND age > 21

Use INTERSECT or UNION between SELECT queries. If a row appears in both result sets, or either result set, respectively, then the row is included in the compound query's result set.

SELECT customer_id FROM archived_orders
UNION
SELECT customer_id FROM recent_orders

Upvotes: 5

Related Questions