To get distinct rows in Google Sheets Query, use the SELECT DISTINCT clause. For example, =QUERY(A1:C, "SELECT DISTINCT A, B") returns unique combinations of columns A and B, removing duplicate rows based on those columns.
What Does DISTINCT Do in Query?
The DISTINCT keyword in a Google Sheets QUERY function works like the SQL SELECT DISTINCT. It scans the selected columns and returns only one row for each unique combination of values in those columns. If you select a single column, it returns a list of unique values in that column. If you select multiple columns, it returns unique combinations across those columns—meaning two rows are considered duplicates only if all selected column values match.
For example, with data in columns A (Product) and B (Region):
=QUERY(A1:B, "SELECT DISTINCT A")returns each unique product name once.=QUERY(A1:B, "SELECT DISTINCT A, B")returns each unique product-region pair once.
Note that DISTINCT is case-sensitive: “Apple” and “apple” are treated as different values. Also, blank cells are treated as a distinct value, so a blank row will appear once.
How to Use DISTINCT with Other Query Clauses
You can combine DISTINCT with WHERE, ORDER BY, LABEL, and LIMIT to filter and sort your unique results.
Example data in A1:C10 with headers: Product, Region, Sales.
- Unique products with sales over 100:
=QUERY(A1:C, "SELECT DISTINCT A WHERE C > 100") - Unique product-region pairs sorted by product:
=QUERY(A1:C, "SELECT DISTINCT A, B ORDER BY A") - Unique products with a custom header:
=QUERY(A1:C, "SELECT DISTINCT A LABEL A 'Product Name'") - First 5 unique products:
=QUERY(A1:C, "SELECT DISTINCT A LIMIT 5")
Remember that DISTINCT applies to the entire selected column list—you cannot use it on a single column while selecting other columns without including them in the distinct set. For example, SELECT DISTINCT A, B is valid, but SELECT DISTINCT A, B, C will return unique combinations of all three columns.
DISTINCT vs. UNIQUE: Which Should You Use?
Google Sheets also has a UNIQUE function that returns unique rows from a range. The key difference is that UNIQUE is a standalone function, while DISTINCT is part of the QUERY language. UNIQUE is simpler and often faster for basic deduplication:
=UNIQUE(A1:B)returns unique rows from the entire range A1:B.=UNIQUE(A1:A)returns unique values in column A.
Use QUERY with DISTINCT when you need to combine deduplication with other query features like filtering, aggregation, or joining multiple sheets. Use UNIQUE when you just need a quick list of unique values without extra logic. Both are case-sensitive and treat blanks as a distinct value.
Common Errors and How to Fix Them
Here are typical mistakes when using DISTINCT in QUERY:
- Error: “DISTINCT is not a valid function”—This happens if you write
DISTINCT(A)instead ofDISTINCT A. In QUERY, DISTINCT is a keyword, not a function. - Error: “Can’t parse query”—Often caused by missing quotes or commas. Ensure the query string is in double quotes and columns are separated by commas:
"SELECT DISTINCT A, B". - Error: “No values”—If your range includes a header row and you select it, you might get the header as a value. Use
SELECT DISTINCT Aand then filter out the header with a WHERE clause, or useOFFSET 1to skip the header row:=QUERY(A1:C, "SELECT DISTINCT A OFFSET 1", 1). - Blank rows appear—If your range includes empty cells, DISTINCT will return a blank row. Use a WHERE clause to exclude blanks:
WHERE A IS NOT NULL.
Another common issue is that DISTINCT does not work with aggregate functions like SUM or COUNT in the same SELECT clause. For example, SELECT DISTINCT A, SUM(B) is invalid. Instead, use GROUP BY for aggregation: SELECT A, SUM(B) GROUP BY A.
Using DISTINCT with Multiple Columns and Aggregates
If you need unique rows based on one column but also want to include other columns, you have a few options:
- Use
GROUP BYinstead of DISTINCT:=QUERY(A1:C, "SELECT A, MAX(B) GROUP BY A")returns one row per unique A with the maximum B. - Use
UNIQUEon the entire row:=UNIQUE(A1:C)returns unique rows based on all columns. - Use
SORTNwith a column number:=SORTN(A1:C, 9^9, 2, 1, TRUE)returns unique rows based on column A (the third argument 2 means remove duplicates, the fourth argument 1 is the column to check, and TRUE means sort ascending).
For example, to get the latest sales record for each product (assuming column D has dates): =QUERY(A1:D, "SELECT A, MAX(D), B, C GROUP BY A, B, C")—but that groups by all columns. A simpler approach is to use SORTN to keep the first row per product after sorting by date descending.
Performance and Large Datasets
QUERY with DISTINCT can handle thousands of rows, but performance may slow down on very large sheets (100k+ rows). To improve speed:
- Limit the range to the actual data area, e.g.,
A1:C1000instead ofA:C. - Avoid selecting entire columns if you know the data ends at a certain row.
- Use
WHEREto filter early, reducing the number of rows processed. - Consider using
UNIQUEon a helper column if you only need one column’s unique values.
If your data is in a pivot table, you can also use the pivot table’s “Unique rows” option, but that is separate from QUERY.
Frequently Asked Questions
Can I use DISTINCT with a WHERE clause on a date column? Yes, you can filter dates before selecting distinct values. For example, =QUERY(A1:C, "SELECT DISTINCT A WHERE B > DATE '2024-01-01'") returns unique products from rows after Jan 1, 2024.
How do I get distinct values from a column but ignore blanks? Add a WHERE clause: =QUERY(A1:A, "SELECT DISTINCT A WHERE A IS NOT NULL"). This excludes empty cells from the result.
Does DISTINCT work with wildcards or partial matches? No, DISTINCT works on exact values. For partial matches, use WHERE with LIKE or CONTAINS, but then you might get duplicates if the matched values differ. For example, SELECT DISTINCT A WHERE A CONTAINS 'apple' returns unique values that contain ‘apple’.
Can I use DISTINCT in a QUERY that joins two sheets? Yes, you can use SELECT DISTINCT with JOIN or QUERY combined with {} arrays. For example, =QUERY({Sheet1!A:B; Sheet2!A:B}, "SELECT DISTINCT Col1, Col2") returns unique pairs from both sheets.
Why does my DISTINCT query return duplicate-looking rows? If you select multiple columns, DISTINCT looks at the combination. Two rows might have the same first column but different second column, so they are not duplicates. Also, check for extra spaces or hidden characters—DISTINCT is exact, so “Apple” and “Apple ” are different.
Is there a way to count distinct values in QUERY? QUERY does not have a COUNT DISTINCT function directly, but you can use COUNT(DISTINCT A) in some SQL dialects—however, Google Sheets QUERY does not support that. Instead, use =COUNTUNIQUE(A1:A) or wrap your DISTINCT query with COUNTA: =COUNTA(QUERY(A1:A, "SELECT DISTINCT A")).
Related
- Sign Out Shortcut Windows: 5 Fast Ways
- Google Sheets QUERY Clause Guide
- Entry Level Data Entry Jobs: What They Are & How to Start
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Generate 5 Digit Random Number in PythonTo generate a 5-digit random number in Python, use random.randint(10000, 99999) for a uniform integer, or random.randrange(10000, 100000)…
- Excel Conditional Formatting on All SheetsExcel does not have a single built-in command that applies one conditional-formatting rule to every worksheet at once.…
- Google Sheets Calendar: Create and Customize a Month ViewCreate a calendar view in Google Sheets by entering the first day of a month in cell B1,…
- How to Loop a PowerPoint SlideshowTo loop a PowerPoint slideshow in Microsoft 365 for Windows, open the presentation and select Slide Show >…
- How to Add Reference Footnotes in Word, Step by StepTo add a reference footnote in Word, place the cursor after the passage you are citing, choose References…
- Multiple Goal Seek in Excel: What Works and What Doesn’tExcel’s Goal Seek can solve for one changing cell to make one formula reach one target per run;…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.