The UNION
operator is used to combine two sets.
( SELECT * FROM products ORDER BY price DESC LIMIT 4 ) UNION ( SELECT * FROM products ORDER BY price / weight DESC LIMIT 4 );
It will also only produce unique results. If that is not what we want, we need to use the UNION ALL
keyword to get the duplicates.
Keyword | Description |
---|---|
UNION | Join together the results of two queries and remove duplicate rows. |
UNION ALL | Join together the results of two queries and keep duplicate rows. |
INTERSECT | Find the rows common in the two queries. Remove duplicates. |
INTERSECT ALL | Find the rows common in the two queries. Keep duplicates from individual queries. |
EXCEPT | Find rows present in first query but not the second. Remove duplicates from LHS. Order matters. |
EXCEPT ALL | Find rows present in first query but not the second. Keep duplicates from LHS. Order matters. |