create table t(user_id int, col2 numeric, col3 numeric);
insert into t(user_id, col2, col3)
-- values(1,1,4), (1,2,4), (1,3,4), (1,4,4)
values
(1,4,4), (1,4,4), (1,4,4), (1,4,4),
(2,1,4), (2,4,4), (2,4,4), (2,4,4),
(3,1,4), (3,2,3), (3,3,2), (3,4,1)
-- values(1,1,2), (1,2,2)
;
select * from t;
with numbered_rows as (
select
*,
row_number() over(partition by user_id order by col2) as rn
from t
) select * from numbered_rows where rn < 4
order by user_id, rn;