PostgreSQL – How to Select Multiple Ranges in a WHERE Clause

postgresqlwhere

I am trying to find a way to query a tables in my database. The table is called districts which contains a boroid and area column. Basically the community table looks like:

boroid  |  area   
--------+---------------
101     | 41641860.1953
104     | 49300667.7004
106     | 38700249.8776
112     | 77981905.0100
210     | 174703337.756
301     | 132531300.780
306     | 131678770.100
303     | 246427540.049
302     | 167440618.596
402     | 171605041.803 

I am trying to find all community districts within the ranges 101-108, 301-303 and only 402. Can someone help me achieve this? Below is my query:

SELECT boroid from districts where ('[101-108]','[301-303]','[402]'); 

Best Answer

BETWEEN is a nice method to help define ranges.

SELECT boroid
FROM districts
WHERE boroid BETWEEN 101 AND 108
  OR boroid BETWEEN 301 AND 303
  OR boroid = 402;