HomeGoogle DocsSpreadsheetHow to Return Unique Rows in Google Sheets QUERY

How to Return Unique Rows in Google Sheets QUERY

Published on

With the help of the UNIQUE and SORTN spreadsheet functions, we can return unique rows in Google Sheets QUERY.

We usually use the UNIQUE function to remove duplicate values. SORTN can also do this, but in a different way, especially with tie mode 2.

The purpose of the Google Sheets QUERY UNIQUE or SORTN combo is to apply the UNIQUE/SORTN function to filtered and summarized (queried) tables.

QUERY is the ultimate solution for creating summary reports and pivot tables. Its aggregation capabilities are exemplary.

In this tutorial, I’ll explain which function to use out of the two (UNIQUE or SORTN) with QUERY to return unique rows with a few examples below.

The syntaxes are as follows:

  • UNIQUE + QUERY Syntax: UNIQUE(QUERY())
  • SORTN + QUERY Syntax: SORTN(QUERY(),9^99,2,column_index_to_unique,1)

Note: I have omitted the QUERY statements in the above syntaxes because it may vary based on your data and filtering type. You should enter them within quotation marks, as you can see in the examples after a few paragraphs below.

Which syntax should I choose?

Use the UNIQUE formula if your QUERY result has one column. If your QUERY formula result has more than one column, you can use UNIQUE to unique it by all columns, but this may not be what you want.

Use the SORTN formula if your QUERY result has more than one column and you want to unique it by any particular column. SORTN allows you to specify the column you want to unique by, so you can use it to unique your QUERY result by any column.

Return Unique Rows in Google Sheets QUERY: Single Column

We have a two-column data set, with items in the first column and their transaction type in the second column. We can treat this as a category and subcategory.

The data range is B1:C, where cell range B1:C1 contains the column headers.

I want to filter B2:B if C2:C is “Purchase”, and the following formula does that.

=QUERY(B2:C,"SELECT B WHERE C = 'Purchase'")
Return Unique Rows in Google Sheets Query: One Column

Must Read: Learn Google Sheets Query Function: Step-by-Step Guide.

How do we remove duplicate values in the above QUERY result?

We can use the UNIQUE function with it to return unique rows in the above Google Sheets QUERY result.

=UNIQUE(QUERY(B2:C,"SELECT B WHERE C = 'Purchase'"))

This is basic filtering. You can use multiple criteria within QUERY and unique rows as above.

Return Unique Rows in Google Sheets QUERY: Multiple Columns

Here is a different dataset with three columns: Item, Transaction, and Date.

The data range is A1:C, where A1:C1 contains the column headers.

I want to filter A2:A and C2:C if B2:B is “Purchased”, and the following formula does that.

=QUERY(A2:C,"SELECT A,C WHERE B='Purchased'")
Return Unique Rows in Google Sheets Query: Multiple Columns

We cannot use the UNIQUE function to return unique rows in this Google Sheets QUERY result. See my expected results in the above screenshot.

The first result in E10:F12 shows the unique purchased items with the most recent purchase dates, and the second output in E14:F16 shows the oldest purchased items.

We can get the above-expected results in two ways. One is using SORTN, and the other is using QUERY itself. We will see both options below.

Google Sheets QUERY: Unique Rows Using SORTN

To get recently purchased items, you should sort the result in descending order before wrapping the QUERY with SORTN.

Use the ORDER BY clause with QUERY to sort the Date column in descending order. Then wrap SORTN. Here is the formula in cell E10:

=SORTN(QUERY(A2:C,"SELECT A,C WHERE B = 'Purchased' ORDER BY A ASC, C DESC"),9^99,2,1,1)

Note:- Instead of the ORDER BY, you can use the SORT function outside QUERY as a wrapper.

The highlighted index number in the formula represents the unique by column, which is Item.

To get the oldest purchased items, you should sort the result in ascending order before wrapping the QUERY with SORTN.

Use the ORDER BY clause with QUERY to sort the Date column in ascending order. Then wrap SORTN. Here is the formula in cell E14:

=SORTN(QUERY(A2:C,"SELECT A,C WHERE B = 'Purchased' ORDER BY A ASC, C"),9^99,2,1,1)

The above is the SORTN approach to return unique rows in Google Sheets QUERY.

QUERY without Any Wrappers

No doubt, QUERY is the most versatile function in Google Sheets. We can use it without SORTN to get unique rows.

For example, you can replace the formula in cell E10 with this one:

=QUERY(A2:C,"SELECT A,MAX(C) WHERE B = 'Purchased' GROUP BY A LABEL MAX(C)''")

And for cell E14, you can use this one:

=QUERY(A2:C,"SELECT A,MIN(C) WHERE B = 'Purchased' GROUP BY A LABEL MIN(C)''")

You May Like: How to Sum, Avg, Count, Max, and Min in Google Sheets Query.

That’s all about how to return unique rows in Google Sheets QUERY.

Related: How to Find Distinct Rows in Google Sheets Using Query.

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Running Total By Month in Excel

This tutorial demonstrates how to calculate the running total by month in a range...

SORT and SORTBY – Excel Vs Google Sheets

While Excel offers the SORT and SORTBY functions for sorting a table, Google Sheets...

Get Top N Values Using Excel’s FILTER Function

When using the FILTER function in Excel to extract the top N values, it's...

XLOOKUP in Excel: Working with Visible Rows in a Table

In Excel, the XLOOKUP function isn't inherently designed to exclusively work with visible rows;...

More like this

Interactive Random Task Assigner in Google Sheets

You have multiple tasks and multiple people. Here's a fun way to randomly assign...

Google Sheets Bar and Column Chart with Target Coloring

To color each data point in the column or bar chart according to the...

Analyzing Column A Between Non-Adjacent Values in Column B: Google Sheets

This tutorial addresses a common scenario encountered in Google Sheets: how to sum, average,...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.