Select until row matches in postgresql?
Is there a way to select rows until some condition is met? I.e. a type of limit , but not limited to N rows, but to all the rows until the first non-matching row? For example, say I have the table: CREATE TABLE t (id SERIAL PRIMARY KEY, rank INTEGER, value INTEGER); INSERT INTO t (rank, value) VALUES ( 1, 1), (2, 1), (2,2),(3,1); that is: test=# SELECT * FROM t; id | rank | value ----+------+------- 1 | 1 | 1 2 | 2 | 1 3 | 2 | 2 4 | 3 | 1 (4 rows) I want to order by rank, and select up until the first row that is over 1. I.e. SELECT * FROM t ORDER BY rank UNTIL value>1 and I want the first 2