Skip to content
Advertisement

SQL – Select rows partially depending on another table

I am very new to the database and queries. I am not sure if this is very basic. Please help me to find a solution.

I have two tables – subscription and customer with primary keys as subscription_id and customer_id respectively. the customer_id is foreign key pointing to customer table customer_id column. Below is a sample of table and its data:

subscription

Customer

I want to query all the subscriptions which satisfies following two criteria

  1. subscription_satus is pending and membership_type is GOLD or PLATINUM and membership_status is ACTIVE.(1001,1002)
  2. subscription_status is archived only when customer membership_type is PLATINUM and the membership_start_date is between subscription_start_date and subscription_end_date and membership_status is PROCESING(1003)

So we should get 1001, 1002 records under criteria 1. and 1003 under criteria 2

Advertisement

Answer

Query should look like this; lines #1 – 21 represent sample data, you don’t type that. Query you do need begins at line #22 and contains two parts of the WHERE clause: the first satisfies the first condition, while the second satisfies the second condition.

Note that sample MEMBERSHIP_START_DATE for curstomer 3 is 02-JUL-2020 which is not between subscription start (4-JUL-2020) and end (3-JUL-2021) dates, so – 1003 isn’t part of the result set. Modify membership start date to e.g. 22-JUL-2020 and it will be.


User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement