MariaDB UNION vs. UNION ALL: When Do Duplicate Rows Disappear?

MariaDB’s UNION appends the results of multiple SELECT statements and returns each distinct row once. UNION ALL keeps every row. The distinctness test uses all columns in the result together.

Compare row counts when combining results

Select a practice database where you have CREATE TEMPORARY TABLES permission. Run this setup once per connection. If these table names already exist in the session, use a new connection. Closing it removes the temporary tables.

CREATE TEMPORARY TABLE union_first_demo (
    id INT NOT NULL,
    label VARCHAR(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin
);
CREATE TEMPORARY TABLE union_second_demo LIKE union_first_demo;
INSERT INTO union_first_demo VALUES (1,'A'), (2,'B'), (2,'B'), (3,'C');
INSERT INTO union_second_demo VALUES (1,'A'), (2,'X'), (4,'D');

Run the setup and the queries below in the same connection. The sample data includes repeated rows and different labels for the same ID. As described in the MariaDB UNION reference, UNION DISTINCT means the same as UNION.

SELECT id, label FROM union_first_demo
UNION
SELECT id, label FROM union_second_demo
ORDER BY id, label;

SELECT id, label FROM union_first_demo
UNION ALL
SELECT id, label FROM union_second_demo
ORDER BY id, label;

The first query returns five rows: (1,A), (2,B), (2,X), (3,C), (4,D). The second returns seven because (1,A) and (2,B) each occur twice. UNION removes repeated rows both across queries and within a single query.

See how selected columns change duplicates

The first query now selects only id. The second adds origin to the output. Compare which rows are considered identical.

SELECT id FROM union_first_demo
UNION
SELECT id FROM union_second_demo
ORDER BY id;

SELECT 'first' AS origin, id, label FROM union_first_demo
UNION
SELECT 'second', id, label FROM union_second_demo
ORDER BY origin, id, label;

With only the ID, the result is 1, 2, 3, 4. Both rows with ID 2 become one because the labels B and X are excluded. With the source included, the two (1,A) rows are distinct and only the repeated (2,B) within the first source collapses. The result has six rows. Neither query changes the source data.

Why duplicates disappear when mixing ALL and DISTINCT

These two statements combine the same rows in different operator orders. They run without the temporary tables.

SELECT 1 AS mixed_value
UNION ALL SELECT 1
UNION DISTINCT SELECT 2
ORDER BY mixed_value;

SELECT 1 AS mixed_value
UNION DISTINCT SELECT 2
UNION ALL SELECT 1
ORDER BY mixed_value;

The first returns 1, 2 because the later UNION DISTINCT also removes repeats preserved by the UNION ALL to its left. The second returns 1, 1, 2 because the final ALL appends a new row after deduplication. Use parentheses or a derived table to make the scope explicit when only part of a query needs deduplication.

Align column counts, positions, and types

Each SELECT must return the same number of columns. Align their meaning by position because the first SELECT supplies the result column names. Explicit column lists make that alignment easier to verify when a schema changes.

SELECT CAST(1 AS CHAR(10)) AS display_code, 'first' AS origin
UNION ALL
SELECT '0042', 'second'
ORDER BY origin;

SELECT NULL AS value_text
UNION
SELECT NULL;

SELECT COUNT(*) AS combined_rows
FROM (
    SELECT id, label FROM union_first_demo
    UNION ALL
    SELECT id, label FROM union_second_demo
) AS combined;

The first query shows the text codes 1 and 0042. MariaDB can resolve differing result types, but keep identifiers as text when leading zeros matter. The second query combines the two NULL rows into one, and the last query counts all seven rows using a derived table. String comparison and duplicate removal can also depend on collation.

Sort and limit the combined or individual results

The first query picks the top three rows after combining both sources. The second uses parentheses to select the top row from each source before combining them.

SELECT id AS item_id, label AS item_label FROM union_first_demo
UNION ALL
SELECT id, label FROM union_second_demo
ORDER BY item_id DESC, item_label DESC
LIMIT 3;

(SELECT id, label FROM union_first_demo
 ORDER BY id DESC, label DESC LIMIT 1)
UNION ALL
(SELECT id, label FROM union_second_demo
 ORDER BY id DESC, label DESC LIMIT 1)
ORDER BY id DESC, label DESC;

The first result is (4,D), (3,C), (2,X), and the second is (4,D), (3,C). The global sort uses the aliases from the first SELECT, such as item_id and item_label. An inner sort determines which rows its own LIMIT selects. Set final display order at the end. If ties must resolve consistently, add a source identifier and a unique key where appropriate.

Use UNION ALL when a report must preserve every order or amount, including orders with identical display values. To match rows across tables by a condition and return columns from both, use JOIN. Choose the duplicate policy first, then use the execution plan of the real query to compare costs.