I have the following table (simplified, removed unneeded columns):
packageTransactions
| memberID | subscriptionType |
| ==========|===================|
| 12345 | 101 |
| 12345 | 203 |
| 12346 | 101 |
| 12347 | 101 |
| 12348 | 101 |
| 12348 | 203 |
| 12349 | 203 |
| 12345 | 205 |
I want to query all records which have not subscriptionType = 101, but only those where a record for subscriptionType = 101 for the same memberID exists.
Therefore, I use:
SELECT memberID, subscriptionType
FROM packageTransactions
WHERE memberID IN
( SELECT memberID
FROM packageTransactions
WHERE subscriptionType = '101'
)
AND subscriptionType <> '101'
;
which gives me the resultset that I'm looking for:
| memberID | subscriptionType |
| ==========|===================|
| 12345 | 203 |
| 12348 | 203 |
| 12345 | 205 |
However, when using this query on a table with a few thousend records (+30k in my case), it takes minutes to return the result.
So I wonder, if there's a "better" / more efficient way to query the data?
memberIDs, where also a record forsubscriptionType= '101' generally exists. memberID 12349 for example should not be returned, because it has no record forsubscriptionType= '101'.