Introduction
ST_Equals
Relationships Between Geometries
8. ST_Contains
ST_Distance and ST_Dwithin
Spatial joins practice
Summary

Instruction

Great job! There is one function that is very similar to ST_Within called ST_Contains. ST_Contains behaves just like ST_Within, except the order of arguments is simply reversed.

Suppose we want to find the name of the district in which Nebula Hotel is located. We can use the following query:

SELECT
  sfn.name
FROM sf_planning_districts sfn
JOIN sf_hotels sfh
ON ST_Contains(
  sfn.boundaries,
  sfh.boundaries)
WHERE sfh.name = 'Nebula Hotel'

It's important to remember that ST_Contains(geometryA, geometryB is the same as ST_Within(geometryB, geometryA). Use whichever version makes the query more readable in your particular case.

Exercise

Now it's your turn. Show the distinct names of all districts that contain ATMs of the 'Crown Financial Inc.' company.

Stuck? Here's a hint!

Use the DISTINCT clause, together with the district name in the SELECT clause. Join sf_planning_districts with sf_atms. Add a condition for a specific company name in the WHERE clause.

Console

Code editor

Result

TableConsole