How to Use WHERE IN a List using Query function
To use WHERE IN a list using the QUERY function in Google Sheets, you need to follow these steps:
- Organize your data in columns with proper headings.
- Create a list of values that you want to use with the WHERE IN clause.
- Use the QUERY function and include the WHERE IN clause with the list of values.
Here's an example to better understand the process:
Example
Suppose you have the following dataset in the range A1:C6:
Name | Age | City
John | 25 | New York
Jane | 30 | Chicago
Bob | 20 | Los Angeles
Lily | 28 | New York
Sam | 35 | Miami
You want to filter the data to display only the records for people living in New York and Los Angeles.
- First, create a list of cities you want to filter by. For instance, you can create a list in cell E1 with the values "New York" and "Los Angeles" separated by commas:
New York, Los Angeles
- Next, use the QUERY function to filter the data using the WHERE IN clause:
=QUERY(A1:C6, "SELECT * WHERE C IN ('New York', 'Los Angeles')", 1)
- Press Enter, and you will get the filtered result:
Name | Age | City
John | 25 | New York
Bob | 20 | Los Angeles
Lily | 28 | New York
In the QUERY function, the first argument (A1:C6) is the data range, the second argument is the query string, and the third argument (1) is the number of header rows. The query string uses the WHERE IN clause to filter the data based on the list of cities provided in the parentheses.
Did you find this useful?