Question Details

No question body available.

Tags

sql postgresql

Answers (5)

January 5, 2026 Score: 3 Rep: 9,391 Quality: Low Completeness: 70%

Using NOT EXIST is another option

SELECT mt.id,
       mt.status
FROM myTable mt
WHERE NOT EXISTS (
    SELECT 1
    FROM myTable mts
    WHERE mts.id = mt.id
      AND mts.status = 'error'
)
ORDER BY mt.id;;

Using EXISTS is yet another option

SELECT mt.id,
       mt.status
FROM myTable mt
WHERE EXISTS (
    SELECT 1
    FROM myTable mts
    WHERE mts.id = mt.id
    GROUP BY mts.id
    HAVING bool_or(mts.status = 'error') = false
)
ORDER BY mt.id;

See example


NOT EXIST should perform way better than the EXIST option.

For illustration purposes, a simple, though not optimal demonstration is provided here

January 5, 2026 Score: 2 Rep: 526,131 Quality: Low Completeness: 40%

We can first aggregate your table by ID to find values not having any error record. Then join this result back to the table to find the full matching records.

WITH cte AS (
    SELECT ID
    FROM yourTable
    GROUP BY ID
    HAVING COUNT(CASE WHEN STATUS = 'error' THEN 1 END) = 0
)

SELECT t1.ID, t1.STATUS FROM yourTable t1 INNER JOIN cte t2 ON t2.ID = t1.ID;
January 5, 2026 Score: 2 Rep: 79,122 Quality: Low Completeness: 40%

A window function might be fastest

WITH cte AS ( SELECT , COUNT() FILTER (WHERE yt.STATUS = 'error') OVER (PARTITION BY yt.ID) AS counterror FROM yourTable yt ) SELECT cte.ID, cte.STATUS FROM cte WHERE cte.counterror = 0;
January 5, 2026 Score: 0 Rep: 21 Quality: Low Completeness: 30%

You can use simple nested SELECT

SELECT * FROM myTable WHERE ID NOT IN ( SELECT ID FROM myTable WHERE STATUS = 'error' );
January 5, 2026 Score: 0 Rep: 8,732 Quality: Low Completeness: 70%

One of the options is using Left Join (with Where Null clause) as a filter:

Select     x.id, x.status
From       myTable x
Left Join (SELECT id FROM myTable WHERE status = 'error') y ON x.id = y.id
Where      y.id Is Null
id status
2 compile
2 process
3 compile

fiddle