SQL BETWEEN

SQL BETWEEN Operators


The BETWEEN operator is used to create an SQL query that filters rows within a specific range. The BETWEEN operator works with numbers, text (strings) and dates. A WHERE clause with a BETWEEN operator looks like this:

SELECT * FROM {name of dataset}
WHERE {column} BETWEEN {value1} AND {value2};

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.

BETWEEN Query

Let’s say we only want to show the areas with area IDs between 25032 and 30100. In that case the WHERE query will look like this.

SELECT * FROM usa_salesmanagers_2_digit_areas
WHERE area_id_number BETWEEN 25032 AND 30100;

BETWEEN Query

Now let’s say we only want to show the areas where the sales manager name is between Adam and Devlin, i.e. where the name of the sales manager starts with a letter between A and D. In that case the WHERE query will look like this.

SELECT * FROM usa_salesmanagers_2_digit_areas
WHERE area_id_number BETWEEN 'Adam' AND 'Devlin';

BETWEEN Query

Order your BETWEEN query


If you only want to have a look at the data without focusing on the geographic components, it might come in handy to order the data in the output table of the BETWEEN query . To do this, press the small arrow next to the table header of the column that contains the values you used as filters in your BETWEEN query. A small drop down menu will appear with the options to arrange the data in ascending or descending order.

BETWEEN Query