filter table rows within range of related column I have a postgres database with the following table:
    CREATE TABLE data (
      id integer PRIMARY KEY,
      group_id integer NOT NULL,
      value double
    );
    

the data contained in it is like the following:
| id  | group_id | value |
| --- | -------- | ----- |
| 1   | 1        | 0     |
| 3   | 1        | 0.6   |
| 4   | 1        | 1     |
| 2   | 1        | 2     |
| 33  | 1        | 5     |
| 7   | 2        | 0.0   |
| 11  | 2        | 0.1   |
| 6   | 2        | 0.33  |
| 8   | 2        | 1.5   |
| 20  | 2        | 2     |

I need to get all the rows in a group_id between two id's values.
For example for the group_id 2 and ids 11 and 8 I should get:
| 11  | 2        | 0.1   |
| 6   | 2        | 0.33  |
| 8   | 2        | 1.5   |

I was looking for a cleaner solution than:
SELECT 
  *
FROM 
  data
WHERE
  group_id = 2 AND
value BETWEEN (SELECT value FROM data  WHERE id = 11)
      AND (SELECT value FROM data  WHERE id = 8)

and that possibly worked if the ids are reversed, like for the group_id 2 and ids 8 and 11 returned:
| 8   | 2        | 1.5   |
| 6   | 2        | 0.33  |
| 11  | 2        | 0.1   |
Fiddle
with user_defined_params as (
  select 2 as target_group_id,
  8 as first_id,
  11 as second_id
),
target_value_bounds AS (
  select min(value) as minval, max(value) as maxval
  from data where group_id=(select target_group_id from user_defined_params)
  AND ((id=(select first_id from user_defined_params))
       OR (id=(select second_id from user_defined_params)))
)
select * from data
where group_id=(select target_group_id from user_defined_params)
           AND value &gt;= (select minval from target_value_bounds)
           AND value &lt;= (select maxval from target_value_bounds);

If you're using this from a programming language, you'll definitely want to do away with the user_defined_params CTE and just use named query parameters.