Google Sheets Query: How to Use WHERE IN a List


You can use the following syntax in a Google Sheets query to return all rows where a column contains a value in a list:

=QUERY(A1:C11, "SELECT * WHERE A MATCHES '(value1|value2|value3)'")

This particular query will return all rows in the range A1:C11 where the value in column A is equal to value1, value2, or value3.

Note: The | operator stands for “OR” in Google Sheets.

The following example shows how to use this syntax in practice.

Example: Use WHERE IN List in Google Sheets Query

Suppose we have the following dataset that contains information about various basketball players:

We can use the following query to return all rows where the value in the “Team” column is equal to Mavs, Magic, Kings, or Lakers:

=QUERY(A1:C11, "SELECT * WHERE A MATCHES '(Mavs|Magic|Kings|Lakers)'") 

The following screenshot shows how to use this query in practice:

Google Sheets query where in list

Notice that the only rows returned are the ones where the value in the “Team” column is equal to Mavs, Magic, Kings, or Lakers.

You can also use similar syntax to query for rows where a column contains one of several numeric values.

For example, we can use the following query to return all rows where the value in the “Points” column is equal to 19, 20, or 22:

=QUERY(A1:C11, "SELECT * WHERE C MATCHES '(19|20|22)'") 

The following screenshot shows how to use this query in practice:

Notice that the query only returns the rows where the value in the “Points” column is equal to 19, 20, or 22.

Additional Resources

The following tutorials explain how to perform other common operations in Google Sheets:

Google Sheets Query: How to Return Only Unique Rows
Google Sheets Query: How to Remove Header from Results
Google Sheets Query: How to Ignore Blank Cells in Query

2 Replies to “Google Sheets Query: How to Use WHERE IN a List”

  1. Hi,
    It seems that the formula does not work if one of the values in the list has a parenthesis…

    1. Hi Loic…Thank you for your feedback! In Google Sheets, you can use the `QUERY` function to filter data based on a list of values, similar to the `WHERE IN` clause in SQL. Here’s a step-by-step guide to help you achieve this:

      ### Example Scenario
      Assume you have a dataset in range `A1:C10` with the following columns: `ID`, `Name`, and `Department`. You want to filter the rows where the `Department` is either “HR”, “Finance”, or “Engineering”.

      ### Step-by-Step Guide

      1. **Define Your Data Range**:
      Ensure your data is structured properly. Here is an example of what your data might look like:

      “`
      A B C
      1 ID Name Department
      2 1 John HR
      3 2 Alice Finance
      4 3 Bob Engineering
      5 4 Charlie Marketing
      6 5 Dave HR
      “`

      2. **Create a List of Values to Filter By**:
      You can create a list of departments you want to filter by, e.g., in cells `E1:E3`:

      “`
      E
      1 HR
      2 Finance
      3 Engineering
      “`

      3. **Construct the `QUERY` Function**:
      Use the `QUERY` function to filter the data based on the list of values. Here is how you can do it:

      “`excel
      =QUERY(A1:C10, “SELECT A, B, C WHERE C = ‘HR’ OR C = ‘Finance’ OR C = ‘Engineering'”, 1)
      “`

      This will manually filter the departments. To dynamically reference the list of values, you need to use a helper function.

      4. **Dynamic Filtering Using a Helper Function**:
      To make the filter dynamic based on the list in `E1:E3`, you can use `JOIN` and `FLATTEN` functions in combination with `QUERY`.

      First, create a comma-separated list of departments using `TEXTJOIN`:

      “`excel
      =TEXTJOIN(“‘,'”, TRUE, E1:E3)
      “`

      This will result in a string like `’HR’,’Finance’,’Engineering’`.

      5. **Combine in the `QUERY` Function**:
      Incorporate the `TEXTJOIN` result into the `QUERY` function using concatenation. Here’s the complete formula:

      “`excel
      =QUERY(A1:C10, “SELECT A, B, C WHERE C MATCHES ‘” & TEXTJOIN(“|”, TRUE, E1:E3) & “‘”, 1)
      “`

      The `MATCHES` keyword with a pipe (`|`) operator acts as an `OR` for regex matching.

      ### Explanation
      – `TEXTJOIN(“|”, TRUE, E1:E3)`: Creates a string of values separated by the pipe (`|`) character, which is used in regex to mean “OR”.
      – `QUERY(A1:C10, “SELECT A, B, C WHERE C MATCHES ‘” & … & “‘”, 1)`: Constructs the query string dynamically and filters the data accordingly.

      ### Conclusion
      Using the `QUERY` function with dynamic lists in Google Sheets involves creating a list of values and incorporating it into the `QUERY` function using `TEXTJOIN`. This method allows for flexible and dynamic data filtering based on lists in your spreadsheet.

Leave a Reply

Your email address will not be published. Required fields are marked *