It’s been a while since I’ve used MySQL. I’m assuming that the table and column names are all-caps for a reason and that the MySQL you’re using is case-sensitive, and also assuming some names for relevant columns that you didn’t explicitly name. E.g., you didn’t say what the name of the column in a subscriptions table row that holds the id of the customer, so I named it USER_ID.
select s.USER_ID, s.PRODUCT_ID, s.BEGIN_DATE, s.END_DATE
from SUBSCRIPTIONS s
join
(
select u.ID as USER_ID, s.PRODUCT_ID, max(s.END_DATE) as END_DATE
from USERS u join SUBSCRIPTIONS s
on u.ID = s.USER_ID and s.STATUS = 'EXPIRED'
group by u.ID, s.PRODUCT_ID
) f
on s.USER_ID = f.USER_ID and s.PRODUCT_ID = f.PRODUCT_ID
and s.END_DATE = f.END_DATE and s.STATUS = 'EXPIRED'
Note that you asked for the last expired subscription, not the most recent subscription. E.g., if a given user has an expired subscription from 3 years ago and a current active subscription to the same product, you’ll see the 3-year-old expired subscription. On the chance that you would rather have a query that gives you the most recent subscription if that subscription is expired, you would do:
select s.USER_ID, s.PRODUCT_ID, s.BEGIN_DATE, s.END_DATE
from SUBSCRIPTIONS s
join
(
select u.ID as USER_ID, s.PRODUCT_ID, max(s.END_DATE) as END_DATE
from USERS u join SUBSCRIPTIONS s
on u.ID = s.USER_ID
group by u.ID, s.PRODUCT_ID
) f
on s.USER_ID = f.USER_ID and s.PRODUCT_ID = f.PRODUCT_ID
and s.END_DATE = f.END_DATE and s.STATUS = 'EXPIRED'
Note that this code assumes that a user won’t have both an active and an expired subscription with the same end date. If such an assumption shouldn’t be made, things get complicated.
Like I said, it’s been a while since I’ve touched MySQL so some of my syntax might be slightly off, no warranties expressed or implied, etc.