You're looking for common patterns to identify duplicate values within a specific column in a MySQL table. Here are several methods, ranging from basic GROUP BY to more advanced window functions (available in MySQL 8+).
Let's assume you have a table named products with columns like product_id (PRIMARY KEY), product_name, sku, price. We'll look for duplicates in the sku column.
Table Setup (for testing):
CREATE TABLE products (
product_id INT AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(100),
sku VARCHAR(50) NOT NULL,
price DECIMAL(10, 2)
);
INSERT INTO products (product_name, sku, price) VALUES
('Laptop Pro', 'LAP001', 1200.00),
('Desktop Mini', 'DESK002', 800.00),
('Monitor 27"', 'MON003', 300.00),
('Wireless Mouse', 'ACC004', 25.00),
('Laptop Air', 'LAP001', 1100.00), -- Duplicate SKU
('External SSD', 'STO005', 150.00),
('USB Keyboard', 'ACC004', 35.00), -- Duplicate SKU
('Gaming Headset', 'AUD006', 75.00),
('Monitor 24"', 'MON003', 250.00); -- Duplicate SKU
Method 1: Find the Duplicate Values Themselves
This is the most common and straightforward way to identify which values are duplicated and how many times they appear.
SELECT
sku,
COUNT(sku) AS occurrence_count
FROM
products
GROUP BY
sku
HAVING
COUNT(sku) > 1;
Explanation:
GROUP BY sku: Groups all rows that have the same sku value together.
COUNT(sku): Counts the number of rows within each group.
HAVING COUNT(sku) > 1: Filters the groups, showing only those where the sku appeared more than once.
Result (for the sample data):
| sku | occurrence_count |
|---|
| ACC004 | 2 |
| LAP001 | 2 |
| MON003 | 2 |
Method 2: Find All Rows That Have Duplicate Values
If you want to see all the actual rows that contain duplicate sku values (including their product_id and other columns), you can use a subquery or a JOIN.
Option A: Using INNER JOIN (Recommended for clarity and performance)
SELECT
p1.*
FROM
products p1
INNER JOIN (
SELECT
sku
FROM
products
GROUP BY
sku
HAVING
COUNT(sku) > 1
) AS duplicate_skus ON p1.sku = duplicate_skus.sku
ORDER BY
p1.sku, p1.product_id;
Explanation:
- The subquery
(SELECT sku FROM products GROUP BY sku HAVING COUNT(sku) > 1) first identifies all the sku values that are duplicates (as in Method 1).
- The main query then
INNER JOINs the products table with this list of duplicate_skus on the sku column. This retrieves all original rows whose sku is present in the duplicate_skus list.
Result (for the sample data):
| product_id | product_name | sku | price |
|---|
| 4 | Wireless Mouse | ACC004 | 25.00 |
| 7 | USB Keyboard | ACC004 | 35.00 |
| 1 | Laptop Pro | LAP001 | 1200.00 |
| 5 | Laptop Air | LAP001 | 1100.00 |
| 3 | Monitor 27" | MON003 | 300.00 |
| 9 | Monitor 24" | MON003 | 250.00 |
Option B: Using EXISTS (Alternative)
SELECT
p1.*
FROM
products p1
WHERE
EXISTS (SELECT 1 FROM products p2 WHERE p1.sku = p2.sku AND p1.product_id <> p2.product_id);
Explanation:
- This query checks for each
p1 row if there EXISTS another row p2 that has the same sku but a different product_id. This effectively finds rows with duplicates.
- Caution: This can be less performant than the
JOIN approach on very large datasets, as it might perform a correlated subquery for each row.
Method 3: Using Window Functions (MySQL 8.0+)
Window functions offer a very powerful and often more efficient way to handle this, especially if you need to mark duplicates or keep specific ones.
Option A: Count occurrences per partition
SELECT
product_id,
product_name,
sku,
price,
occurrence_count
FROM (
SELECT
product_id,
product_name,
sku,
price,
COUNT(*) OVER (PARTITION BY sku) AS occurrence_count
FROM
products
) AS sub
WHERE
occurrence_count > 1
ORDER BY
sku, product_id;
Explanation:
COUNT(*) OVER (PARTITION BY sku): This is a window function. For each row, it counts all rows that have the same sku value without collapsing the rows (unlike GROUP BY). The result occurrence_count is then added as a new column for each original row.
- The outer query then filters these results, showing only those rows where
occurrence_count is greater than 1.
This produces the same result as Method 2, Option A, but can be more flexible for further operations.
Option B: Identify and Prioritize Duplicates (e.g., for deletion)
If you want to keep one instance of each duplicate and identify the others as "to be removed", ROW_NUMBER() is excellent.
SELECT
product_id,
product_name,
sku,
price,
rn AS row_number_in_group
FROM (
SELECT
product_id,
product_name,
sku,
price,
ROW_NUMBER() OVER (PARTITION BY sku ORDER BY product_id) AS rn
FROM
products
) AS sub
WHERE
rn > 1 -- These are the 'extra' duplicates you might want to delete
ORDER BY
sku, rn;
Explanation:
ROW_NUMBER() OVER (PARTITION BY sku ORDER BY product_id): This assigns a sequential number to each row within groups of identical sku values. The ORDER BY product_id part determines which row gets 1, 2, etc. (e.g., the row with the lowest product_id for a given sku will get 1).
WHERE rn > 1: Filters for all rows that are not the "first" occurrence according to our ordering, thus identifying the true duplicates you might want to remove.
Result (for the sample data):
| product_id | product_name | sku | price | row_number_in_group |
|---|
| 7 | USB Keyboard | ACC004 | 35.00 | 2 |
| 5 | Laptop Air | LAP001 | 1100.00 | 2 |
| 9 | Monitor 24" | MON003 | 250.00 | 2 |
Important Considerations:
- Indexing: For large tables, ensure the column you are checking for duplicates (
sku in our example) is indexed. This significantly speeds up GROUP BY and JOIN operations.
ALTER TABLE products ADD INDEX idx_sku (sku);
- What to do with Duplicates?
Choose the method that best suits your MySQL version and your specific goal (just identifying, or identifying for further action).