In QGIS 2.18 project there are several layers, lines and polygons respectively
My task is to select all lines when their start and end points are laying within polygons using Virtual Layer, see image below.
I can achieve the result that I want with the following query
WITH start_p AS (
SELECT l.id AS start_id
FROM line AS l, "Layer_2" AS p
WHERE ST_within(start_point(l.geometry),p.geometry)
),
end_p AS (
SELECT l.id AS end_id
FROM line AS l, "Layer_2" AS p
WHERE ST_within(end_point(l.geometry),p.geometry)
)
SELECT *
FROM line AS l, start_p, end_p
WHERE l.id = start_p.start_id
AND l.id = end_p.end_id
My Question: Why I can not achieve the same result using the following query
SELECT l.*
FROM line AS l, "Layer_2" as p
WHERE ST_within(end_point(l.geometry),p.geometry)
AND ST_within(start_point(l.geometry),p.geometry)
However, a query like (see below) will work
SELECT l.*
FROM line AS l, "Layer_2" as p
WHERE ST_within(start_point(l.geometry),p.geometry)