Google Sheets Tips

QUERY with UNIQUE in Google Sheets: how to return distinct rows

The QUERY function has no DISTINCT keyword. Wrapping it in UNIQUE returns each row of the result once; a group by clause does the same when you also need totals. This guide works both on one marketing question, which channels brought new customers this month, with the Col1 notation for UNIQUE inside QUERY and the mistakes that make the formulas fail or count a customer twice.

Google Sheets TipsVadym Kramarenko14 min read

QUERY with UNIQUE in Google Sheets: how to return distinct rows

The QUERY function in Google Sheets has no DISTINCT keyword, so a query cannot remove duplicate rows by itself. The fix is to wrap it in UNIQUE: =UNIQUE(QUERY(range, “select B, C”)) runs the query first and then returns each resulting row once.

You want to Use
List each value of a column once =UNIQUE(QUERY(A2:F13, "select D", 0))
List each combination of columns once =UNIQUE(QUERY(A2:F13, "select C, D", 0))
Count the distinct values =COUNTA(UNIQUE(QUERY(A2:F13, "select C", 0)))
One row per value, with a count or a total next to it =QUERY(A2:F13, "select D, count(C) group by D", 0)

This guide works every formula on one question a CMO asks at the end of the month: which channels brought new customers this month, without double counting? The answer is one QUERY. Getting there shows why the first count you write is usually too high: on the orders below, the same question returns 9, 7 or 6 new customers, depending on what the formula treats as a duplicate.

It also covers the reverse order (UNIQUE inside QUERY), the group by alternative when you also need totals, and the mistakes that make either formula fail.

Note: Originally published in January 2024. Updated in November 2026: every example now works on one orders export and one marketing question, and the error section has a new entry for counts that do not match the ad platforms.

Reading to get this done?

The question and the sheet

The question and the sheet

A store’s order export is the usual place to start. The one used here has twelve orders, one per row, from row 2 to row 13, with the headers in row 1:

Column Holds
A Order date
B Order ID
C Customer email
D Channel the order came from: Paid search, Paid social, Email, Organic search, or nothing when the source was not recorded
E First order: the date of the customer’s first ever order, from the customer record
F Revenue

The month is November 2026. A new customer is one whose first order is in November, so the test is column E, not column A: ana.p@mail.com ordered twice in November but has been a customer since June. The export also starts with one October order, and it has the faults real exports have. Four customers ordered twice. One of them came back through a different channel. One email was typed with capitals the second time. One order has no channel.

A Google Sheets orders export with twelve rows, columns for order date, order ID, customer email, channel, first order date and revenue, and a QUERY in H1 that returns new customers by the channel of their first order: no channel 1, Email 1, Organic search 1, Paid search 2, Paid social 1.

The finished answer is the block on the right, and the formula in the bar is the whole of it:

Channel of the first order New customers Orders from new customers
Paid search 2 4
Paid social 1 2
Email 1 1
Organic search 1 1
No channel recorded 1 1
Total 6 9

Six new customers, each counted once, under the channel that brought them in. Count orders instead and you report nine; count each channel’s customers separately and add them up and you report seven. All the numbers come from the same rows. The rest of this guide builds the formulas that tell those numbers apart.

The two functions in brief

The two functions in brief

QUERY

=QUERY(data, query, [headers])

  • data: the range to query.
  • query: the instruction, in quotes: which columns to return, which rows to keep, how to sort or group.
  • headers (optional): the number of header rows at the top of the data. If omitted, Google Sheets guesses. The examples here query A2:F13, the rows without the header, and set it to 0.

💡 Everything else QUERY can do is in our guide to QUERY in Google Sheets.

Exploring Google Sheets QUERY Function: Tips and Tricks for Efficient Data Management

UNIQUE

=UNIQUE(range, [by_column], [exactly_once])

UNIQUE returns the rows of a range with the duplicates left out. It compares whole rows: with two columns, a row is a duplicate only if both cells match.

Distinct values and unique values

The two words are often used as if they meant the same. They do not:

  • Distinct values: every value once, however often it appears. This is what UNIQUE returns by default. =UNIQUE(C2:C13) lists every customer email once.
  • Unique values: only the values that appear exactly once. The UNIQUE function returns only unique values in this sense when the third argument, exactly_once, is TRUE.

On the orders, UNIQUE with exactly_once, =UNIQUE(C2:C13,,TRUE), gives a list of the customers who ordered only once. It returns six emails, and two of them are kim.w@mail.com and Kim.W@mail.com: the same customer, counted as two one-time buyers because the second email was typed with capitals. Clean the case first and the list is four.

💡 More on the function in our guide to UNIQUE in Google Sheets.

Hand this to your AI

You want each campaign and date once in your QUERY result, and QUERY has no DISTINCT to ask for it.

Paste this into your AI assistant. It wraps your own QUERY in UNIQUE, or rewrites it with group by, so each row comes back once and the totals are right.

The prompt

return the distinct rows of my range from a QUERY, wrapped in UNIQUE or with a group by when I also need totals, with the column references written the way QUERY reads them

Distinct rows from a QUERY: three examples

Distinct rows from a QUERY: three examples

Each example asks a smaller question about the same twelve orders.

1. Distinct rows of the whole table

=UNIQUE(QUERY(A2:F13, "select *", 0))

This returns all twelve rows. Nothing is removed, because every row has its own order ID in column B, so no two rows are identical. UNIQUE across the whole table removes rows that were pasted twice, such as last week’s export appended to this week’s. It does not answer a question about customers, because a customer with two orders is two different rows.

2. Distinct combinations of some columns

Select fewer columns and the comparison gets coarser. To list each customer and channel pair once:

=UNIQUE(QUERY(A2:F13, "select C, D", 0))

The twelve orders become ten pairs. tom.r@mail.com ordered twice through Paid search and appears once; ana.p@mail.com ordered twice by Email and appears once.

Select a single column, “select D”, and you get the distinct values in column D, a list of channels: Paid social, Paid search, Email, Organic search and one empty cell, the order with no channel. That empty cell is a line of the report, not a mistake to delete.

3. Distinct rows that meet a condition

A where clause keeps the rows where the condition holds, before the duplicates are removed. To list the new customers of the month with the channels they ordered through:

=UNIQUE(QUERY(A2:F13, "select C, D where E >= date '2026-11-01'", 0))

Google Sheets with UNIQUE wrapped around a QUERY that keeps customers whose first order is in November, returning eight customer and channel pairs; mia.s@mail.com appears twice, under Paid social and Email, and kim.w@mail.com appears twice because of a capital letter.

Eight pairs for six customers. Two rows are highlighted for two different reasons. mia.s@mail.com is a real customer counted under two channels: her first order came from Paid social, her second from an Email. kim.w@mail.com and Kim.W@mail.com are one customer that UNIQUE treats as two, because it compares text exactly.

Dates in a query are written as date 'yyyy-mm-dd'. Text values are written in single quotes, because the whole query is already inside double quotes. To make “this month” move by itself, build the date from TODAY: "select C, D where E >= date '"&TEXT(EOMONTH(TODAY(),-1)+1,"yyyy-mm-dd")&"'".

UNIQUE inside QUERY

UNIQUE inside QUERY

The order can be reversed: remove the duplicates first, then query the result.

=QUERY(UNIQUE(C2:E13), "select Col1, Col2 where Col3 >= date '2026-11-01' order by Col2", 0)

One thing changes. UNIQUE returns an array, not a range of the sheet, so the query can no longer use column letters. Columns are called Col1, Col2, Col3, counted from the left of the array: here Col1 is the email, Col2 the channel and Col3 the first order date.

This returns the same eight pairs as example 3, sorted by channel. Add limit 10 at the end of the query to keep only the first 10 rows of a longer list. Use this order when the condition should apply to the deduplicated data, or when you want the query to sort the final result.

When you also need totals: group by

When you also need totals: group by

If the goal is one row per value with a number next to it, you do not need UNIQUE at all. A group by clause returns each value once and aggregates the rest:

=QUERY(A2:F13, "select D, count(C) where E >= date '2026-11-01' group by D", 0)

This returns each channel once with the number of orders new customers placed through it: Paid search 4, Email 2, Paid social 1, Organic search 1 and the empty channel 1. count(), sum(), avg(), max() and min() work the same way. Every column in the select must either be in the group by or be inside an aggregate function. In a table of sales with the representative in column B and the amount in column D, the same pattern is select B, sum(D) group by B.

It counts orders, though, not customers: count() counts rows, and QUERY has no count distinct. Nine orders are not nine new customers. To count each customer once, add the condition that makes one row per customer, the order placed on the first order date, which the list returned by UNIQUE cannot pick out:

=QUERY(A2:F13, "select D, count(C) where A = E and E >= date '2026-11-01' group by D label D 'Channel', count(C) 'New customers'", 0)

That is the formula in the first image. A where clause can compare two columns, and A = E keeps only each customer’s first order, so every customer is counted once, under the channel that brought them. Crediting the first order is a decision, not a fact of the data: the same sheet could credit the latest order instead, and Email would gain a customer from Paid social. Write the rule next to the number.

Count distinct with COUNTA and UNIQUE

To count the distinct values a query returns, the count unique formula is COUNTA around UNIQUE. UNIQUE and COUNTA together are the count distinct that QUERY lacks:

Three formulas counting the new customers of November on the same orders: COUNTA of the QUERY returns 9 orders, COUNTA of UNIQUE returns 7 distinct emails as typed, and COUNTA of UNIQUE of LOWER returns 6 customers.

  • =COUNTA(QUERY(A2:F13, "select C where E >= date '2026-11-01'", 0)) returns 9: every order from a new customer.
  • =COUNTA(UNIQUE(QUERY(A2:F13, "select C where E >= date '2026-11-01'", 0))) returns 7: the distinct emails, as typed.
  • =COUNTA(UNIQUE(ARRAYFORMULA(LOWER(QUERY(A2:F13, "select C where E >= date '2026-11-01'", 0))))) returns 6: the new customers.

All three are arithmetically correct, and none of them is wrong as a formula. They answer three different questions, and only the last one is the number of new customers. When a count of customers is challenged in a meeting, the first thing to ask is which of the three it is.

Why the formula fails or returns too many rows

Why the formula fails or returns too many rows

Column letters in the query

Column letters in a query must be upper case, and the data range must include all referenced columns: for a query on A2:F13, you can select A to F, not G. When the data is an array, such as the output of UNIQUE or two ranges in curly braces, use Col1, Col2 instead of letters. Mixing the two styles returns a #VALUE! error with “Unable to parse query string”.

Quotes

The query is wrapped in double quotes, so text values inside it take single quotes: where D = 'Paid search', or where B = 'John Smith' in a table of sales representatives. Column references take no quotes at all. Written as ‘Col1’, a column reference becomes a piece of text and no longer refers to the column.

The header row appears in the result

If the header shows up as a data row, or a data row is treated as a header, set the third argument of QUERY: 1 when the range starts with one header row, 0 when it has none. With A1:F13 and 1, the result starts with the header, and COUNTA around it counts the header as one more customer.

Values that look identical are returned twice

UNIQUE compares exactly. “kim.w@mail.com” and “Kim.W@mail.com”, or an email with a trailing space, are different values, and each one is a customer in the count. Names typed two ways behave the same. Clean the column with TRIM and one of the case functions before deduplicating, as LOWER does in the last count above, or fix the entries at the source.

A column loses some of its values

QUERY expects one data type per column. In a column that mixes numbers and text, it keeps the majority type and treats the rest as empty. Convert the column to one type first; TO_TEXT turns everything into text. Dates stored as text are the common case here: E >= date '2026-11-01' skips them silently.

The count is right and does not match the ad platforms

Add up the new customers each channel reports and you usually get more than the shop has. Here mia.s@mail.com ordered after a Paid social ad and again after an Email, so a customer-per-channel count credits both: seven claims for six customers. The usual reasons the sheet and the platforms disagree are the credit rule (first order, last order or both), the period and time zone of “this month”, returning customers counted as new, and orders with no channel left out of one report and kept in the other. None of these is a formula error. Each is a definition to agree on and write next to the number.

The formula is slow

On large amounts of data, or on a range of thousands of rows, filter first and deduplicate second: UNIQUE(QUERY(…)) with a where clause hands UNIQUE fewer rows to compare. Open-ended ranges such as A2:F make both functions scan every row of the sheet, so close the range when you can.

An alternative without formulas: a pivot table

An alternative without formulas: a pivot table

A pivot table (Insert → Pivot table) lists each value of a field once when you add the field to Rows, and can add a sum, an average or a count next to it. It suits people who prefer to build the summary by clicking, with the same care about what is counted: a COUNTUNIQUE summary of emails by channel counts a customer once in every channel they ordered through, so it adds up to the channels’ claims, not to the number of customers.

Related Google Sheets functions

Functions that are often used next to QUERY and UNIQUE, each with its own guide:

Deduplicating in the sheet hides the question of why there are duplicates

Deduplicating in the sheet hides the question of why there are duplicates

UNIQUE(QUERY(…)) gives you a clean list every time the sheet recalculates. The formula is exact; the rows may not be. It does not tell you why the rows were duplicated or why a customer appears twice: an email typed two ways, a join that matched one order to two campaigns, an export appended to last week’s export, a customer entered twice in the CRM. And it cannot decide which channel gets the credit. If the duplicates carry numbers, they may already be added to the totals elsewhere in the workbook, and the wrong number travels on to the report.

The OWOX Data Marts extension for Google Sheets covers that half. A data analyst publishes a Data Mart (a table or an SQL query in your data warehouse, with the definitions the team agreed, such as what counts as a new customer and which order gets the credit) and makes it available for reports. In Sheets you open the extension, pick the Data Mart, tick the columns you want and run it. The rows land in a tab, ready for your formulas.

Google Sheets with the OWOX Data Marts extension open beside an ad spend table: the Unified Ad Spend Data Mart, nine of fourteen columns ticked and a Save & Run button – the rows in the sheet come from a governed Data Mart, not a pasted export

What changes for the person who maintains the sheet:

  • Refresh replaces re-export. Refresh current report in the extension menu re-runs the same pull, and a scheduled refresh keeps the tab current without anyone touching it.
  • The source is written on the sheet. The header cell of an imported tab carries a note with the Data Mart it came from, the time of the import and a link to the report.
  • No SQL and no data warehouse access needed. The analyst decides which Data Marts and columns are available. You choose from that list.

The extension does not write or change the analyst’s SQL. Whether a customer appears once is decided there, in a query that can be read and checked, and QUERY and UNIQUE in the sheet are left to shape the result for the report. The extension comes with the OWOX Cloud plans of OWOX Data Marts; spreadsheet reporting describes both ways data reaches a sheet. OWOX for reporting analysts is the same setup from the analyst’s side: reports defined once and delivered to spreadsheets on schedule.

Hand this to your AI

You want each campaign and date once in your QUERY result, and QUERY has no DISTINCT to ask for it.

Paste this into your AI assistant. It wraps your own QUERY in UNIQUE, or rewrites it with group by, so each row comes back once and the totals are right.

The prompt

return the distinct rows of my range from a QUERY, wrapped in UNIQUE or with a group by when I also need totals, with the column references written the way QUERY reads them

FAQ

Frequently Asked Questions

What common issues might I encounter when using QUERY and UNIQUE together?

The usual ones: column letters written in lower case or outside the data range, column letters used where the data is an array (use Col1, Col2 instead), text values without single quotes, and a header row counted as data. The quiet one is a count that is too high. On an orders export with twelve rows, the new customers of the month come out as 9 orders, 7 emails as typed and 6 customers, because UNIQUE treats `kim.w@mail.com` and `Kim.W@mail.com` as two values. Wrap the query in `ARRAYFORMULA(LOWER(...))` before UNIQUE to count each customer once.

What is the syntax for using QUERY to display unique rows for a single column?

Select the one column in the query and wrap it in UNIQUE: `=UNIQUE(QUERY(A2:F13, "select D", 0))`. On an orders export with the channel in column D, this lists each channel once: Paid social, Paid search, Email, Organic search, and one empty cell for the order with no channel recorded. Add a where clause to filter first, for example `"select C where E >= date '2026-11-01'"` for the emails of customers whose first order is in November.

What function counts unique values in Google Sheets?

COUNTUNIQUE counts the distinct values in a range: `=COUNTUNIQUE(range)`. Around a QUERY, COUNTA and UNIQUE do the same job and let you see the list first: `=COUNTA(UNIQUE(QUERY(A2:F13, "select C where E >= date '2026-11-01'", 0)))` counts the distinct customer emails among November's new customers. On an export where one email was typed with capitals the second time, this returns 7; with `ARRAYFORMULA(LOWER(...))` around the query it returns the true 6.

How can I troubleshoot performance issues when using QUERY and UNIQUE on large datasets?

Filter with QUERY first and deduplicate second, so UNIQUE compares fewer rows: `=UNIQUE(QUERY(A2:F13, "select C, D where E >= date '2026-11-01'", 0))` hands UNIQUE only the orders of new customers. Close the ranges (A2:F13, not A2:F), because open-ended ranges make both functions scan every row of the sheet. If the sheet is slow because the export itself is large, pull fewer rows into it instead of more formulas over it.

Is there a UNIQUE function in Google Sheets?

Yes. `=UNIQUE(range)` returns every value or row of a range once, and `=UNIQUE(range,,TRUE)` returns only the values that appear exactly once. On an orders export, `=UNIQUE(C2:C13)` lists each customer email once, and `=UNIQUE(C2:C13,,TRUE)` lists the customers who ordered only once. UNIQUE compares text exactly, so the same email typed with capitals counts as a second customer; clean the column with LOWER and TRIM first.

What are some best practices for using QUERY and UNIQUE functions together?

Filter first and deduplicate second: UNIQUE(QUERY(range, "select ... where ...")). Use upper-case column letters for a range and Col1, Col2 for an array, set the headers argument to 0 or 1 rather than letting Sheets guess, and keep one data type per column. Use group by when you need a count or total next to each value, and remember count() counts rows: on one orders export, new customers by channel are 9 orders but 6 customers. Write the rule you used, such as crediting the first order's channel, next to the number.

How do I handle multiple columns when displaying unique rows?

Include every column you want in the select, and UNIQUE compares the combination. `=UNIQUE(QUERY(A2:F13, "select C, D", 0))` returns each customer and channel pair once: on twelve orders, ten pairs. A customer who ordered through two channels appears twice, once per channel, which is right for a list of pairs and wrong for a count of customers. To count customers, select the customer column alone, or count only each customer's first order with a group by query.

Can the QUERY function display only unique rows?

Not by itself: QUERY has no DISTINCT keyword. Wrap it in UNIQUE, `=UNIQUE(QUERY(data, query))`, or use a group by clause when you also want a count or a total. For example, `=QUERY(A2:F13, "select D, count(C) where A = E and E >= date '2026-11-01' group by D", 0)` returns each channel once with the number of new customers whose first order came through it.

Who wrote this

Vadym Kramarenko

Vadym Kramarenko Growth Marketing Manager

Vadym Kramarenko is a Growth Marketing Manager at OWOX, where he drives user acquisition and product-led growth strategies. He hosts the OWOX podcast, interviewing analytics professionals about data-driven marketing, attribution, and reporting best practices. Vadym specializes in turning complex analytics concepts into practical, actionable marketing frameworks.