The Alternative to SQL IN Operator in Google Sheets Query (Also Not IN)

Published on

I could not find the SQL IN logical operator in Google Sheets Query. Is it available or not? If not, is there any alternative available?

In Google Sheets Query, IN as well as NO IN logical operators are not available. But there is an alternative.

If you know the purpose of SQL IN, then you can look for an alternative. So let me explain the purpose of the SQL IN logical operator.

Purpose:

To match multiple values in a list, we can use the IN operator in the SQL WHERE (I am not familiar with SQL, but I am accustomed to using Query in Sheets).

Actually, the SQL IN logical operator is equivalent to using multiple OR conditions. For multiple OR, we can use Matches operator in Query.

This post describes how to use MATCHES operator as the SQL IN and NOT IN alternatives in Google Sheets Query.

If you want to learn Matches, which is a regular expression match, in full detail, please check my guide Matches Regular Expression Match in Google Sheets Query.

SQL IN Operator Alternative in Google Sheets Query

I have a table of products (A1:C) with the first column contains product names, the second and third columns contain the unit price and the stock availability respectively.

I want to find the products where the product names match the names in a provided list in another column, E.

We can call this column E as the criteria column.

SQL IN Operator in Google Sheets Query - Alternative

If you don’t want to specify column E, then it’s quite easy to code the Query. I mean you can just list the names to find within Query.

Use the Matches in Query Where and enter the product names to find. The product names must be separated by |.

The formula for cell G1.

=query(A1:C,"Select * where A matches 'Apple|Sweet Melon|Avocado'")

But to find the products using product names in another column (here column E), use the formula as below.

=query(A1:C,"Select * where A matches '"&TEXTJOIN("|",true,E1:E)&"'")

Textjoin with Matches – Explanation

The TEXTJOIN will combine the criteria and place the pipe symbol in between. Any doubt?

Key this TEXTJOIN formula in a cell and see what it returns.

=TEXTJOIN("|",true,E1:E)

As per the above example, it would definitely return the string Apple|Sweet Melon|Avocado.

The above TEXTJOIN must be placed between '"& and this &"' as a common practice in the use of text criterion as cell reference in Query like '"&E1"'. See the example formulas below.

=query(A1:C,"Select * where A matches '"&E1&"'")
which is equal to
=query(A1:C,"Select * where A matches 'Apple'")

For more details of proper criteria use in Query, please read Examples to the Use of Literals in Query in Google Sheets.

This way you can use Matches as an alternative to SQL IN logical operator in Google Sheets Query.

SQL NOT IN Alternative

When you want to find products that are not on a list, you can depend on NOT IN alternative. I mean to find the products excluding the one listed in a separate column.

If you have followed the above examples, here things will be easy for you. Simply put a NOT (NOT logical operator) before the column to search.

SQL NOT IN  Alternative
=query(A1:C,"Select * where not A matches '"&TEXTJOIN("|",true,E1:E)&"'")

You must prefix the NOT operator to the column identifier, not a suffix.

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Expert Prashanth KV is a Diamond Product Expert in Google Sheets, officially recognized by Google for his contributions to the Docs Editors Help Community and featured in the Google Product Experts Directory. With years of experience working with Google Sheets and Excel for multinational firms in Mumbai and Dubai, he has been blogging since 2012, offering practical, real-world spreadsheet solutions that professionals rely on. Explore his blog to learn advanced formulas, automation tips, and problem-solving techniques to elevate your spreadsheet skills.

Mode of Comma-Separated Numbers in Excel (Dynamic Array)

There is no dedicated function in Excel to directly find the mode of comma-separated...

How to Find Mode of Comma-Separated Numbers in Google Sheets

Working with comma-separated numbers inside a single cell is a common scenario in Google...

How to Count Merged Cells in Google Sheets (and Get Their Size)

Sometimes, you may have blocks of merged cells in a column or row. But...

Count Consecutive Workday Absences in Google Sheets

This tutorial offers a powerful formula-based solution to count consecutive workday absences in Google...

More like this

How to Find Mode of Comma-Separated Numbers in Google Sheets

Working with comma-separated numbers inside a single cell is a common scenario in Google...

How to Count Merged Cells in Google Sheets (and Get Their Size)

Sometimes, you may have blocks of merged cells in a column or row. But...

Count Consecutive Workday Absences in Google Sheets

This tutorial offers a powerful formula-based solution to count consecutive workday absences in Google...

8 COMMENTS

  1. Hi Prashanth,

    It was helpful, but I may have run into an issue. Am I correct to assume the “matches” operator is case-insensitive, like the match function?

    • The MATCHES substring match is case-sensitive. To make it case-insensitive, use LOWER or UPPER functions.

      Example:

      =query(A1:C,"Select * where lower(A) matches
      'apple|sweet melon|avocado'")

  2. Hello,

    I got a result I want to see, but in TEXTJOIN("|",true,E1:E), what’s the meaning of “|”?

    It’s confusing how it works in the expression.

    • Hi, JunKim,

      In the MATCHES regular expression operator in Query, the “|” (pipe/vertical bar) symbol is to match any of two or more options similar to the OR Spreadsheet function.

      ‘Brazil|Japan’ – To match Brazil or Japan (choice of alternatives)

  3. Very useful post. I use to stumble at this issue many a time. I had to look for workarounds by going for a helper column. But this really sleek.

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.