Yes, you can get unique values in Google Sheets using the QUERY function. The simplest way is to use =QUERY(A:A, "SELECT A WHERE A IS NOT NULL GROUP BY A") or =QUERY(A:A, "SELECT DISTINCT A WHERE A IS NOT NULL"). Both return a list of unique values from the range, but they behave slightly differently with numeric data and sorting. This article explains those differences, shows practical examples, and covers common pitfalls.
What Does “Unique” Mean in Google Sheets?
In Google Sheets, “unique” usually means removing duplicate rows or values. There are two distinct concepts:
- Distinct values: Each value appears once. For example, from the list [A, B, A, C], distinct values are A, B, C.
- Unique values: Values that appear exactly once. From the same list, unique values are B and C (A appears twice, so it’s excluded).
The QUERY function can produce both, but the keywords DISTINCT and GROUP BY are the tools. The UNIQUE function (which is separate from QUERY) returns distinct values, not unique-only values. Many users confuse these terms, so it’s worth clarifying before diving into formulas.
How to Use QUERY to Get Unique Values
Assume you have a list of product names in column A, from A2 to A100, with a header in A1. To get a list of distinct product names, you can use:
=QUERY(A2:A100, "SELECT A WHERE A IS NOT NULL GROUP BY A")– This groups identical values and returns one row per group. It also sorts the results alphabetically by default.=QUERY(A2:A100, "SELECT DISTINCT A WHERE A IS NOT NULL")– This returns distinct values but does not sort them; it preserves the order of first appearance.
Both formulas ignore blank cells because of the WHERE A IS NOT NULL clause. If you want to include blanks, remove that clause, but be aware that blanks might show as empty rows.
For a more advanced scenario, suppose you have a table with columns A (product) and B (sales amount). To get unique products with their total sales, use:
=QUERY(A2:B100, "SELECT A, SUM(B) WHERE A IS NOT NULL GROUP BY A")
This returns a two-column result: product and sum of sales for that product. The GROUP BY clause is required when you use aggregate functions like SUM, COUNT, or AVG.
Difference Between GROUP BY and SELECT DISTINCT
Many users wonder which to use. The key difference lies in what you can do with the results:
- GROUP BY allows you to include aggregate functions (SUM, COUNT, AVG, etc.) in the SELECT clause. It also sorts the output by the grouped column in ascending order by default.
- SELECT DISTINCT only returns the distinct values of the selected columns. It cannot be combined with aggregate functions. It preserves the original order of appearance.
For simple unique value extraction, both work. But if you need counts or sums alongside unique values, GROUP BY is the only option. If you need the order to match the original data, use SELECT DISTINCT.
How to Get Unique Values from Multiple Columns
Sometimes you need unique combinations of values from two or more columns. For example, you have columns A (Region) and B (Product), and you want all unique region-product pairs. Use:
=QUERY(A2:B100, "SELECT A, B WHERE A IS NOT NULL AND B IS NOT NULL GROUP BY A, B")
This returns distinct pairs. If you want to count how many times each pair appears, add a COUNT column:
=QUERY(A2:B100, "SELECT A, B, COUNT(A) WHERE A IS NOT NULL AND B IS NOT NULL GROUP BY A, B")
Note that when grouping by multiple columns, the order of columns in GROUP BY affects sorting. The result is sorted by the first grouped column, then the second.
Common Problems and How to Fix Them
Here are the most frequent issues users encounter with QUERY and unique values:
- Error: “Unable to parse query string” – Usually a typo in the query string. Check that you have proper commas and no extra spaces. Also, ensure you’re using double quotes around the query.
- Blank cells appear as blank rows – Add
WHERE A IS NOT NULLto filter them out. - Numbers and text are treated differently – QUERY treats numbers and text as distinct types. If a column has mixed types, GROUP BY might separate them. Use
TO_TEXT(A)to convert to text if needed. - Case sensitivity – QUERY is case-insensitive by default for text comparison. If you need case-sensitive unique values, you’ll need to use a workaround with ARRAYFORMULA and COUNTIF.
For case-sensitive unique extraction, you can use a helper column with =ARRAYFORMULA(IF(COUNTIFS(A2:A, A2:A, ROW(A2:A), "<="&ROW(A2:A))=1, A2:A, "")) and then filter out blanks. This is more advanced but works.
QUERY vs. UNIQUE Function
Google Sheets has a dedicated UNIQUE function that is simpler for basic distinct value extraction. For example, =UNIQUE(A2:A100) returns distinct values, ignoring blanks, and preserves order of appearance. So why use QUERY?
- QUERY can filter and aggregate in one step, e.g., unique values that meet a condition.
- QUERY can sort results with
ORDER BY. - QUERY can combine multiple columns and aggregate functions.
If you just need a quick list of unique items, UNIQUE is faster to type. But if you need to summarize data, QUERY is more powerful. For example, to get unique products with sales over $100, you'd use:
=QUERY(A2:B100, "SELECT A, SUM(B) WHERE B > 100 GROUP BY A")
This is not possible with UNIQUE alone.
Performance Tips for Large Datasets
QUERY can handle tens of thousands of rows, but performance can degrade if you use it on entire columns (like A:A) because it processes all rows, including empty ones. To improve speed:
- Limit the range to the actual data, e.g., A2:A1000 instead of A:A.
- Use
WHEREto filter early. - Avoid using QUERY inside ARRAYFORMULA; instead, use it standalone.
- If you have a very large dataset (over 100k rows), consider using a pivot table or a Google Apps Script to extract unique values.
Also, note that QUERY results are dynamic – they update when the source data changes. That's usually a benefit, but if you need a static list, you can copy and paste values.
Frequently Asked Questions
How do I get unique values from a column in Google Sheets?
Use the UNIQUE function: =UNIQUE(A2:A100). This returns distinct values, ignoring blanks. If you need to use QUERY, use =QUERY(A2:A100, "SELECT DISTINCT A WHERE A IS NOT NULL").
Can I use QUERY to count unique values?
Yes. To count distinct values in a column, use =COUNTA(UNIQUE(A2:A100)). To count unique values that appear only once, you'd need a more complex formula like =SUM(ARRAYFORMULA(IF(COUNTIF(A2:A100, A2:A100)=1, 1, 0))).
Why does my QUERY return duplicate values?
If you're using SELECT without DISTINCT or GROUP BY, QUERY returns all rows. Ensure you include DISTINCT or GROUP BY in the query string. Also, check for hidden spaces or non-printable characters in your data that make values appear different.
How do I sort unique values in QUERY?
Add an ORDER BY clause. For example: =QUERY(A2:A100, "SELECT DISTINCT A ORDER BY A"). For descending order, use ORDER BY A DESC.
Can I get unique values from a row instead of a column?
Yes. Use TRANSPOSE to convert the row to a column, then apply UNIQUE or QUERY. For example, =TRANSPOSE(UNIQUE(TRANSPOSE(A1:Z1))) returns unique values from a row.
Does QUERY work with numbers and dates?
Yes, but be careful with formatting. QUERY treats dates as serial numbers. To group by date, you might need to use the TEXT function to format dates as text first. For example, =QUERY(A2:B100, "SELECT TEXT(A, 'YYYY-MM'), SUM(B) GROUP BY TEXT(A, 'YYYY-MM')").
Related
- Google Sheets Query Offset: How to Skip Rows
- Shortcut for Downloading? Quick Ways to Save Files
- General Ledger Data Entry: How It Works
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Change Dark View in Outlook: Steps for Every VersionTo change Outlook to dark view, open its appearance or theme settings and choose Dark. In classic Outlook…
- Excel Shortcut to Delete a Row (Windows and Mac)In Excel for Microsoft 365 on Windows, select a cell in the row and press Shift+Space to select…
- JavaScript Add to Dictionary: 4 Ways to Do ItIn JavaScript, a “dictionary” is typically an object or a Map. To add a key-value pair to an…
- Excel Work Schedule Template: Build and Customize OneAn Excel work schedule template is a reusable spreadsheet for assigning employees to dates and shifts, tracking hours,…
- Excel: Expand Print Area to Fit a PageTo make an Excel printout fit a page, select the cells you want, choose Page Layout > Print…
- Delete a Blank Page in Word: Step-by-Step GuideTo delete a blank page in Word, turn on formatting marks, find the paragraph mark or break creating…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.