Sorry, for the confusing title, but did not found a better one. Here is the situation:

```
CREATE TABLE orders (
    order_id    int  NOT NULL,
    company_id  int  NOT NULL,
    last_update date     NULL
)

```

Table Data:

```
ORDER_ID   COMPANY_ID       LAST_UPDATE  
       1            1        2020/06/08  
       2            1        2020/06/08  
       3            1        2020/06/08  
       4            2        2020/06/08  
       5            2        2020/01/27  
       6            3        2020/06/08  
       7            3        2020/06/08  
       8            3        2020/06/08  
       9            3        NULL  
      10            4        2020/06/08  
      11            4        2020/06/08  
      12            4        2020/06/08  
      13            4        2020/06/08  
      14            4        2020/06/08  

```

I want to have all rows, with a company, where there is no row with the same company and a LAST\_UPDATE older than 3 months (or null).

### What does not work:

I cannot use a simple WHERE clause with the date, because this filters me out just the rows 5 and 9. I only want the rows 1-3 & 10-14.

### What works, but is to slow:

I can use a subquery (AND company\_ID NOT IN (SELECT DISTINCT company\_id [...])), but this completely kills my performance. In prod environment I have nearly 50M rows, the ResultSet of the subquery is too huge.

### My current workaround:

I just ordered my results by company\_id, last\_update and use a "continue" in my Java Code, if there is a too old last\_update. But that's also not optimal.

### Question:

Is there are performant SQL only way, to achive this. Maybe over a "group by ... having" - clause.

Thanks in advance!