Can you use and/or together in a Query function on google sheets

Viewed 35

I am trying to use the below function which works when "not" is removed.

QUERY(Sheet!A:L,"SELECT A,G,H,I,J,K WHERE F='City' AND H='model' AND I>=Number AND (not C contains 'AA1' or not C Contains 'AB1')",1)
2 Answers

we can test with matches and regex. if it still not working for you then the dataset is insufficient.

=QUERY(Sheet!A:L,
 "SELECT A,G,H,I,J,K 
  WHERE F='City' 
    AND H='model' 
    AND I>=Number 
    AND not C matches 'AA1|AB1'", 1)

Just a postscript to this, if you did want to use Contains it should be

=QUERY(Sheet1!A:L,"SELECT A,G,H,I,J,K WHERE F='City' AND H='Sedan' AND I>=2016 AND not (C contains 'BMW 3 series' or C Contains 'Audi A4')",1)

Your original formula would only exclude a row that contained both BMW 3 series and Audi A4 because

(not C contains 'BMW 3 series' or not C Contains 'Audi A4'",1)

is equivalent to

not (C contains 'BMW 3 series' and C Contains 'Audi A4'",1)

by de Morgan's theorem

So given the following data

enter image description here

Your formula would retrieve

enter image description here

while the correct result is

enter image description here

Related