SQL AND OR NOT

SQL AND, OR, NOT Operators


If you want to apply multiple conditions in an SQL query you can use the AND, OR and NOT operators in your query. The AND and OR operators are used to filter rows based on more than one condition. The NOT operator displays rows where one or more conditions are NOT TRUE.

AND Operator


The AND operator is used to filter rows where all conditions you state are true. A WHERE query with AND operators looks like this:

SELECT * FROM {name of dataset}
WHERE {condition} AND {condition2} AND {condition3};

In the example below we have a dataset with 2-digit postal codes from the US. Each postal code area has one sales manager, but a sales manager can be responsible for several postal code areas.

AND, OR, NOT Query

Let’s say we only want to show the areas where Rick is sales manager and the 2-digit postal code is 11. In that case the WHERE query will look like this.

SELECT * FROM {name of dataset}
WHERE salesmanager = 'Rick' AND pc2_number = 11;

AND, OR, NOT Query

Quick tip! Difference between numbers and text
Remember that when you create a query you have to take the data type into account. When you are filtering rows based on a field of type text (string) you have to put your  filter values between single quotes (‘ ‘). If you are filtering rows based on a field of type number you do not have to use quotes.

OR Operator


The OR operator is used to filter rows if one of the conditions you are using to filter is true. A WHERE query with OR operators looks like this:

SELECT * FROM {name of dataset}
WHERE {condition} OR {condition2} OR {condition3};

In the example below we have a dataset with 2-digit postal codes from the US. Each postal code area has one sales manager, but a sales manager can be responsible for several postal code areas.

AND, OR, NOT Query

Let’s say we only want to show the areas where the 2-digit postal code is 11 OR 27. In that case the WHERE query will look like this.

SELECT * FROM {name of dataset}
WHERE pc2_number=11 OR pc2_number=27;

AND, OR, NOT Query

NOT Operator


The NOT operator is used to filter rows where conditions you use in your filter are not true. A WHERE query with NOT operators looks like this:

SELECT * FROM {name of dataset}
WHERE NOT {condition};

In the example below we have a dataset with 2-digit postal codes from the US. Each postal code area has one sales manager, but a sales manager can be responsible for several postal code areas.

AND, OR, NOT Query

Let’s say we only want to show the areas where the sales manager is not Rick. In that case the WHERE query will look like this.

SELECT * FROM usa_salesmanagers_2_digit_areas
WHERE NOT salesmanager='Rick';

AND, OR, NOT Query

Quick tip! Editing data like a pro with operators
In this example we only used the WHERE clause with the AND, OR and NOT operators. If you use these operators along with the UPDATE clause you have a powerful way to do targeted bulk edits of your data. For more information about the UPDATE clause, please read our article about UPDATE queries.