Introduction
ST_Equals
Relationships Between Geometries
ST_Distance and ST_Dwithin
15. ST_DWithin
Spatial joins practice
Summary

Instruction

Excellent! Another interesting function that is somewhat similar to ST_Distance, is ST_Dwithin(geometryA,geometryB,distanceX). This function checks whether geometryA and geometryB are within distanceX of each other. If so, it returns TRUE. Otherwise, it returns FALSE. Take a look at the following modified example of the last exercise:

SELECT 
  *
FROM sf_atms sa
JOIN sf_restaurants sep
ON ST_DWithin(
    ST_Transform(
      sep.coordinates, 26910),
    ST_Transform(
      sa.coordinates, 26910),5000) 
WHERE sep.id = 1

In the above query, we show all the information for ATMs that are within 5000 meters of the restaurant with id = 1.

Exercise

After a whole day of sightseeing, Mark ended up at the Palace of Fine Arts. San Francisco has mesmerised Mark so much that he forgot to eat! Show all restaurants (from the table sf_restaurants) that are within 3000 meters of the Palace of Fine Arts (table sf_sights). Show the name of the restaurant, rating, and type of food that is served in the restaurant.

Stuck? Here's a hint!

You will have to use ST_Dwithin(...) while joining the two tables.

Remember to use the ST_Transform function and an SRID of 26910.

Console

Code editor

Result

TableConsole