Unions and intersections with sets
UNION
is used to combine output from multiple queries. By default it removes duplicate results. Parentheses are not required but could be used to remove ambiguity, such as ORDER BY
and LIMIT
at the end of the following query.
(
SELECT *
FROM products
ORDER BY price DESC
LIMIT 4
)
UNION
(
SELECT *
FROM products
ORDER BY price / weight DESC
LIMIT 4
);
To show duplicate results, use UNION ALL
.
(
SELECT *
FROM products
ORDER BY price DESC
LIMIT 4
)
UNION ALL
(
SELECT *
FROM products
ORDER BY price / weight DESC
LIMIT 4
);