In Google Sheets, the QUERY function does not use the standard <> operator for “not equal” in its SQL-like syntax; instead, you use != or <> inside the query string, but the correct and most reliable form is !=. For example, =QUERY(A1:C10, "select * where B != 'Closed'") returns all rows where column B is not exactly “Closed”.
Understanding the QUERY Function
QUERY is one of Google Sheets’ most powerful functions. It lets you run a SQL-like query against a range of data. The basic syntax is:
=QUERY(data, query, [headers])
- data – the range to query (e.g., A1:D100).
- query – a string in double quotes containing the SQL-like command.
- headers – optional; the number of header rows at the top of the data.
The query string can include select, where, order by, group by, limit, and more. The where clause filters rows based on conditions, and that’s where you use “not equal”.
How to Write “Not Equal” in QUERY
In Google Sheets’ QUERY, you can use either != or <>. Both are valid, but != is more commonly used and less prone to confusion with HTML entities. Here are examples for different data types:
- Text:
where B != 'Closed'– matches rows where column B is not exactly “Closed” (case-insensitive by default). - Numbers:
where C != 100– matches rows where column C does not equal 100. - Dates:
where D != date '2025-01-01'– matches rows where column D is not January 1, 2025. - Booleans:
where E != FALSE– matches rows where column E is not FALSE (i.e., TRUE).
Example with a full formula: If your data is in A1:C10 with headers in row 1, and you want all rows where column B is not “Pending”, use:
=QUERY(A1:C10, "select * where B != 'Pending'", 1)
Comparing QUERY to Other Methods
While QUERY is powerful, there are simpler alternatives for filtering out a value:
- FILTER function:
=FILTER(A1:C10, B1:B10 <> "Pending")– easier for simple conditions, but QUERY handles complex logic and grouping more elegantly. - Filter views: Use Data > Create a filter, then uncheck the values you don’t want – no formula needed.
- Conditional formatting: Not for filtering data, but for highlighting rows that don’t match a value.
QUERY shines when you need to combine conditions, aggregate data, or sort results. For instance, you can do where B != 'Pending' and C > 50 in one line.
Common Pitfalls and Errors
Users often run into issues when using “not equal” in QUERY. Here are the most frequent:
- Using single quotes inside the query string: The entire query is in double quotes, so text values must use single quotes. For example,
"where B != 'Closed'"is correct;"where B != "Closed""breaks the formula. - Case sensitivity: QUERY’s text comparisons are case-insensitive by default. If you need case-sensitive matching, you must use
EXACT()inside a QUERY, which is complex. For most cases, case-insensitivity is fine. - Blank cells:
!=treats blank cells as not equal to a value, so they are included. If you want to exclude blanks, addand B is not null. - Using
<>in some locales: In some European locales, the comma in formulas is replaced by a semicolon, but the operators stay the same. Always use commas in the function arguments.
Example to exclude blanks: =QUERY(A1:C10, "select * where B != 'Pending' and B is not null", 1)
Advanced Usage: Combining with Other Conditions
You can combine “not equal” with other operators to create powerful filters:
- AND:
where B != 'Pending' and C > 100 - OR:
where B != 'Pending' or C > 100 - NOT with IN:
where B not in ('Pending', 'Review')– this is a cleaner way to exclude multiple values. - Using LIKE:
where B not like 'P%'– excludes rows where B starts with “P”.
Example with NOT IN:
=QUERY(A1:C10, "select * where B not in ('Pending', 'Review')", 1)
QUERY vs. Excel’s Equivalent
Excel does not have a direct QUERY function. The closest is the FILTER function (Excel 365) or the older Advanced Filter. In Excel, you would write:
=FILTER(A1:C10, B1:B10 <> "Pending")
Or use a pivot table with a filter. Google Sheets’ QUERY is more similar to SQL, which makes it more flexible for complex data manipulation. If you’re migrating from Excel, expect a learning curve.
Performance and Best Practices
QUERY can handle large datasets, but it’s not infinitely fast. For thousands of rows, it’s fine. For hundreds of thousands, consider using pivot tables or Google BigQuery. Best practices:
- Limit the range to the actual data, not entire columns (e.g., A1:C1000, not A:C).
- Use
limitif you only need a few rows. - Combine multiple conditions with parentheses to ensure correct logic.
- Use
labelto rename columns in the output.
Example with limit and label:
=QUERY(A1:C10, "select A, B where B != 'Pending' limit 5 label A 'Name'", 1)
Frequently Asked Questions
Can I use <> in Google Sheets QUERY? Yes, both <> and != work. However, != is more common and avoids potential issues with HTML encoding if you’re copy-pasting.
How do I exclude multiple values in QUERY? Use the not in operator: where B not in ('Value1', 'Value2'). This is cleaner than chaining multiple != with and.
Why does my QUERY return blank rows when using !=? Because blank cells are not equal to the specified value, so they pass the filter. Add and B is not null to exclude blanks.
Is QUERY case-sensitive? No, by default text comparisons are case-insensitive. If you need case-sensitive, you’d have to use a helper column with EXACT or use a script.
Can I use “not equal” with numbers and dates? Yes, just write the number directly or use date 'YYYY-MM-DD' for dates. Example: where C != 100 or where D != date '2025-01-01'.
What’s the difference between QUERY and FILTER? QUERY is more powerful for complex logic and aggregation, but FILTER is simpler and often faster for basic filtering. Use FILTER for simple conditions, QUERY when you need to group, sort, or use SQL-like syntax.
Related
- Sign Out Shortcut Windows: 5 Fast Ways
- Google Sheets QUERY Clause Guide
- Entry Level Data Entry Jobs: What They Are & How to Start
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Excel Green Highlight Row and Column: How to Turn It OnTo highlight the active row and column in green in Excel for Microsoft 365 on Windows, select View…
- Google Sheets IFS: Else Results, Syntax, and FixesGoogle Sheets’ IFS function has no separate else argument: if none of its conditions is true, it returns…
- Power Query for Data Lakes: What It Can and Can’t DoYes—Power Query in Excel for Microsoft 365 can connect to some data-lake storage, notably Azure Data Lake Storage…
- Excel Replace Shortcut: Use Ctrl+H and Find & ReplaceIn Excel for Microsoft 365 on Windows, press Ctrl+H to open Find and Replace on the Replace tab.…
- Generate Random 10 Digit Number JavaTo generate a random 10-digit number in Java, use long number = (long)(Math.random() * 9_000_000_000L) + 1_000_000_000L; or,…
- Computer Budget Spreadsheet: Plan Your PurchaseA computer budget spreadsheet helps you set a spending limit, list the full cost of a setup, and…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.