I have a table of users who have subscribed to different plans of a streaming services and other data about the said users.
Something similar to this,
Name | Plan | Status | Subscription Date | Cancel Date |
---|---|---|---|---|
Ajax | 5-user | Active | Jan 2021 | – |
Zack | 1-User | Active | Mar 2021 | – |
Ray | 5-User | Inactive | Apr 2020 | Dec 2020 |
Ray | 1-User | Active | Dec 2020 | – |
Jack | 1-user | Inactive | Nov 2020 | Jun 2021 |
Kal | 5-user | Inactive | Jan 2022 | Feb 2022 |
Bruce | 5-User | Active | Jan 2020 | – |
Kal | 1-User | Active | Feb 2022 | – |
Diana | 1-User | Inactive | Jul 2015 | Jul 2020 |
I need to write a sql query to find out the list of users who have cancelled the 5 user plan and bought the 1-user plan. Names – WHERE Plan=5-user and status= Inactive + Plan = 1-user and Status=Active.
Also, the result should displav the names with the current active plan.(would help if all the other columns in the table are displayed along with it too)
I tried
SELECT NAME
FROM Table
WHERE Plan = 5-user AND Status = Inactive
OR Plan = 1-User AND Status = Active
EXPECTED RESULT :
|Ray|1-User|Dec 2020
|Kal|1-User|Feb 2022
P.S I’m new to this site and I have just started learning SQL, I apologise in advance for any error in my question framing.
3
Answers
We can use
INTERSECT
to perform an intersection between the two types of people, namely ones who have purchased[Import, Apple]
and[Native, Orange]
.In this case I suggest you to use
UNION
operatorYou may use the
EXISTS
operator as the following:See a demo.