SOQL Query to Find Duplicate Products by ProductCode
Find duplicate products in Salesforce with a GROUP BY / HAVING SOQL query on ProductCode, then pull the underlying record IDs.
Updated 2026-09-09
Duplicate detection in SOQL is a two-step job: GROUP BY ... HAVING COUNT(Id) > 1 to find the offending values, then a second query to pull the records behind them.
The product object is named Product2 in the API even though the UI calls it Product, which trips up most first-time SOQL queries.
Step 1 - find the duplicated values
HAVING filters the grouped result; WHERE filters rows before grouping. You will usually want both - WHERE to scope the time window, HAVING to keep only the collisions.
SELECT ProductCode, COUNT(Id) total
FROM Product2
GROUP BY ProductCode
HAVING COUNT(Id) > 1
ORDER BY COUNT(Id) DESCStep 2 - pull the records behind a duplicate
Aggregate queries do not return record IDs. Take a value from step 1 and query the records so you can decide which one to keep.
SELECT Id, Name, ProductCode, Family, IsActive
FROM Product2
WHERE ProductCode = 'paste-value-here'
ORDER BY CreatedDateScope it to recent data
On a large object, group across the whole table only once. After the initial cleanup, scope to a rolling window so the query stays fast and the list stays actionable.
SELECT ProductCode, COUNT(Id) total
FROM Product2
WHERE CreatedDate = LAST_N_DAYS:90
GROUP BY ProductCode
HAVING COUNT(Id) > 1Common mistakes to avoid
- Use the API name
Product2, not the UI label - the two differ on several standard objects. - Wrap literal text values in single quotes; date literals such as
LAST_N_DAYS:30must stay unquoted. - Run the query in a sandbox first if it feeds a bulk update - a
WHEREclause that is one character off can select the whole table. GROUP BYcannot be combined withIdin the SELECT list - that is the error most people hit first.- Case and whitespace differences make near-duplicates invisible to
GROUP BY; a trailing space is a different value.
Run it faster with TurboKit
Deduplication is an export-edit-reimport loop, and the export half is where most of the time goes.
TurboKit's AI SOQL builder lets you describe the result you want in plain English, returns the query, and exports the rows to CSV or Excel in the same panel - without leaving the Salesforce tab you are already on.
Frequently asked questions
- Why can't I select Id in a GROUP BY query?
- Every selected field in a grouped query must be either a grouping column or an aggregate.
Idis unique per row, so grouping on it would produce one group per record. - Does this replace Salesforce duplicate rules for products?
- No. Duplicate rules and matching rules prevent new duplicates at save time with fuzzy matching. This query is exact-match, and it is for auditing the records already in the org.
- How do I catch duplicates that differ only in case?
- SOQL grouping is case-insensitive for text fields in most orgs, but leading/trailing whitespace still splits groups. Export and normalise in a spreadsheet when the match must be fuzzy.
More from the SOQL Library
- SOQL Query to Find Duplicate Accounts by NameFind duplicate accounts in Salesforce with a GROUP BY / HAVING SOQL query on Name, then pull the underlying record IDs.
- SOQL Query to Find Duplicate Contacts by EmailFind duplicate contacts in Salesforce with a GROUP BY / HAVING SOQL query on Email, then pull the underlying record IDs.
- SOQL Query to Find Duplicate Leads by EmailFind duplicate leads in Salesforce with a GROUP BY / HAVING SOQL query on Email, then pull the underlying record IDs.
- SOQL Query to Find Products With a Blank ProductCodeFind products with an empty ProductCode using SOQL, including the NULL syntax, blank-string gotcha, and a count-first workflow.
- SOQL GROUP BY: Products by FamilyAggregate SOQL that breaks products down by Family, with COUNT, SUM, and the AggregateResult alias rules.
- SOQL Query to Count Products in SalesforceCopy-paste SOQL to count products in Salesforce, with variants for filtered counts, COUNT(Id) vs COUNT(), and grouped totals.
Related reading
- Admin How-ToHow to Find and Merge Duplicate Records in SalesforceA full workflow for cleaning up duplicate accounts, contacts, and leads in Salesforce - the native merge tool, its record limits, and the SOQL to find what it can't show you.
- GlossaryWhat Is a Duplicate Rule in Salesforce?A duplicate rule defines what Salesforce does when a user is about to save a record that looks like an existing one - block it, allow it with a warning, or ju
- Admin How-ToHow to Write Salesforce Queries Without Knowing SOQL (AI SOQL)Describe the records you want in plain English and get a runnable SOQL query back - how AI SOQL builders work, what to check before you run the result, and where they still fall short.
- GlossaryWhat Is an AI SOQL Builder in Salesforce?An AI SOQL builder turns a plain-English description of the records you want into a runnable SOQL query, using the schema of the org you are working in so the