MariaDB SELECT DISTINCT: return unique values and combinations
In MariaDB, SELECT DISTINCT returns one result row for each unique combination of selected values. Select one column for its unique values, or two columns for unique pairs. The rows stored in the table stay as they are.
Set up the example data
Use the practice database learning with CREATE, INSERT, and SELECT permissions. If distinct_demo already exists, use a new practice database. The fixture has 10 unique IDs, with repeats in category and batch_no. DISTINCT is an option immediately after SELECT. MariaDB Docs · MariaDB SELECT
USE learning;
CREATE TABLE distinct_demo (
id INT PRIMARY KEY,
category VARCHAR(20) NULL,
batch_no INT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO distinct_demo (id, category, batch_no) VALUES
(1,'A',1), (2,'B',1), (3,'C',2), (4,'D',2),
(5,'A',1), (6,'B',2), (7,'C',1), (8,'D',2),
(9,NULL,1), (10,NULL,1);
SELECT id, category, batch_no FROM distinct_demo ORDER BY id;
Compare DISTINCT on one column and several columns
SELECT DISTINCT category
FROM distinct_demo ORDER BY category;
SELECT DISTINCT category, batch_no
FROM distinct_demo ORDER BY category, batch_no;
SELECT DISTINCT id, category, batch_no
FROM distinct_demo ORDER BY id;
The first query returns five rows: NULL, A, B, C, and D. The second returns seven pairs: (NULL,1), (A,1), (B,1), (B,2), (C,1), (C,2), and (D,2). B in batch 1 differs from B in batch 2. Adding the unique id as in the last query leaves all 10 rows. DISTINCT * behaves the same way when all selected columns include that key.
Compare a list containing NULL with distinct counts
When removing duplicates, DISTINCT treats multiple NULLs as equivalent. The two NULL rows therefore produce one list item. Use IS NULL in a filter to find the underlying rows with a missing value. MariaDB Docs · MariaDB NULL
SELECT COUNT(*) AS all_rows,
COUNT(category) AS non_null_rows,
COUNT(DISTINCT category) AS distinct_non_null_values,
COUNT(DISTINCT category, batch_no) AS distinct_non_null_pairs
FROM distinct_demo;
SELECT COUNT(*) AS distinct_including_null
FROM (SELECT DISTINCT category FROM distinct_demo) AS values_once;
The first SELECT returns 10, 8, 4, and 6. COUNT(DISTINCT category, batch_no) excludes tuples containing NULL, so it counts one fewer pair than the earlier list. To include a displayed NULL once, apply COUNT(*) to a derived table containing the DISTINCT result. The second SELECT returns 5. MariaDB Docs · MariaDB COUNT(DISTINCT)
Filter, sort, and choose one source row per group
SELECT DISTINCT category
FROM distinct_demo
WHERE batch_no = 1
ORDER BY category DESC;
SELECT DISTINCT category
FROM distinct_demo
WHERE category IS NOT NULL
ORDER BY category
LIMIT 2;
WHERE narrows the input to the unique list. Filtering to batch 1 and ordering descending yields C, B, A, NULL. The second statement removes NULL, sorts ascending, and returns A and B. Use ORDER BY when the display order matters, and LIMIT to take the first rows of the ordered result.
WITH ranked AS (
SELECT id, category, batch_no,
ROW_NUMBER() OVER (
PARTITION BY category
ORDER BY batch_no DESC, id DESC
) AS row_no
FROM distinct_demo
)
SELECT id, category, batch_no
FROM ranked
WHERE row_no = 1
ORDER BY category;
If you need one original row per group, define which one to keep. The next query orders by greatest batch number, then greatest id when batch numbers tie, and uses ROW_NUMBER() to choose the first. For A, B, C, D, and NULL respectively, the IDs are 5, 6, 3, 8, and 10. If batches do not represent creation time, choosing the latest row requires a different ranking column. MariaDB Docs · MariaDB ROW_NUMBER()
Find repeated combinations with GROUP BY
SELECT category, COUNT(*) AS row_count
FROM distinct_demo
GROUP BY category
ORDER BY category;
SELECT category, batch_no, COUNT(*) AS row_count
FROM distinct_demo
GROUP BY category, batch_no
HAVING COUNT(*) > 1
ORDER BY category, batch_no;
Use DISTINCT for a unique list and GROUP BY to calculate values per group. The first query counts two rows each for NULL, A, B, C, and D. The second uses HAVING to keep only repeated pairs, so (NULL,1), (A,1), and (D,2) each have a count of 2. These queries help investigate repeats without changing the table. MariaDB Docs · MariaDB GROUP BY
Check collation and query plans
CREATE TABLE distinct_case_demo (
value_text VARCHAR(20) CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci
);
INSERT INTO distinct_case_demo VALUES ('a'), ('A');
SELECT COUNT(DISTINCT value_text) AS case_insensitive_count,
COUNT(DISTINCT BINARY value_text) AS byte_distinct_count
FROM distinct_case_demo;
The equality rule for text depends on its collation and type. The example with utf8mb4_unicode_ci counts a and A as one distinct value. Using BINARY to compare their bytes gives a count of two. Case, accents, and trailing spaces can all depend on the comparison rule. MariaDB Docs · MariaDB collations
If a JOIN returns more rows than expected, check join keys and one-to-many relationships before adding DISTINCT. Distinct selected values in a one-to-many result still produce separate rows. With large tables, select only what you need and use EXPLAIN to inspect the actual query plan. MariaDB Docs · MariaDB EXPLAIN




