I'm trying to find islands (type not water, completely surrounded by water) in a polygon table. The polygons are not overlapping.

Data:

```
CREATE TABLE islands (
   ID serial PRIMARY KEY,
   type VARCHAR (10),
   geom geometry);
INSERT INTO islands(type, geom)
   VALUES
   ('water',st_geomfromewkt('SRID=3006;POLYGON((510796 6429408,513939 6420063,526040 6421096,527763 6430183,517341 6436643,510796 6429408),(516954 6429623,516652 6425747,518203 6426436,519667 6428073,516954 6429623))')),
   ('land',st_geomfromewkt('SRID=3006;POLYGON((516954 6429623,516652 6425747,518203 6426436,519667 6428073,516954 6429623))')),
   ('land',st_geomfromewkt('SRID=3006;POLYGON((510796 6429408,517341 6436643,512561 6436427,508384 6434619,510796 6429408),(511011 6434231,512475 6435652,513207 6433025,511011 6434231))')),
   ('land',st_geomfromewkt('SRID=3006;POLYGON((511011 6434231,512475 6435652,513207 6433025,511011 6434231))'));

```

Only id 2 should be selected:
[![enter image description here](https://i.stack.imgur.com/uzfsJ.png)](https://i.stack.imgur.com/uzfsJ.png)

I have tried query below based on [Determine if a polygon is not enclosed by other polygons](https://gis.stackexchange.com/questions/291824/determine-if-a-polygon-is-not-enclosed-by-other-polygons), but it is not selecting anything.

```
SELECT a.id
FROM islands a 
INNER JOIN islands b ON (ST_Intersects(a.geom, b.geom) AND a.id != b.id)
where a.type = 'water' and b.type <> 'water'
GROUP BY a.id
HAVING 1e-6 > 
  abs(ST_Length(ST_ExteriorRing(a.geom)) - 
  sum(ST_Length(ST_Intersection(ST_Exteriorring(a.geom), st_interiorringn(b.geom,1)))))

```