Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 858
error404notFound Avatar asked Aug 09 '26 06:08

error404notFound


1 Answers

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
like image 88
Bill Karwin Avatar answered Aug 11 '26 22:08

Bill Karwin