SQL WHERE

SQL WHERE Function


If you want to select particular rows of your dataset and view this selection in your MY DATA page you have to use a WHERE query. The WHERE clause is used to filter records and extract only those records that match a specific condition (the WHERE condition). 

SELECT * FROM {name of dataset}
WHERE {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.

WHERE Query

Let’s say we only want to show the areas where Adam is the sales manager. The WHERE query for this case has the following syntax:

SELECT * FROM usa_salesmanagers_2_digit_areas
WHERE salesmanager='Adam';

WHERE 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 trying to filter based on a field of type text (string) you have to put the value you are searching between single quotes (‘ ‘). If you are trying to filter based on a field of type number, do not use quotes.

Operators in the WHERE clause


In the example above we used the ” = “-operator. This operator only allows you to select rows based on one single condition. With other operators it is also possible to select rows based on certain numbers or letters. In the table below you can find an overview of these operators.

Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN To specify multiple possible values for a column
AND To specify multiple possible conditions separated by AND
OR To specify multiple possible conditions separated by OR
NOT To specify a condition when not true