Example 1
Merge active and archived product lists into one result
The four distinct product names from both tables are merged and sorted alphabetically. Because all names are unique across the two tables, UNION and UNION ALL would produce the same result here.
Source table data Rows loaded before the example query runs.
Setup
CREATE TABLE products (id INT, name VARCHAR(50));
CREATE TABLE archived_products (id INT, name VARCHAR(50));
INSERT INTO
products (id, name)
VALUES
(1, 'Keyboard'),
(2, 'Mouse');
INSERT INTO
archived_products (id, name)
VALUES
(3, 'Trackball'),
(4, 'Joystick');Validated query Shared across supported engines.
SQL
SELECT
name
FROM
products
UNION
SELECT
name
FROM
archived_products
ORDER BY
name;Expected result Returned rows for the shared example.
| name |
|---|
| Joystick |
| Keyboard |
| Mouse |
| Trackball |
Output is identical across all engines.