logo

Back to questions

Advertiser Status [Facebook SQL Interview Question]

Hard

You're provided with two tables: the table contains information about advertisers and their respective payment status, and the table contains the current payment information for advertisers, and it only includes advertisers who have made payments.

Write a query to update the payment status of Facebook advertisers based on the information in the table. The output should include the user ID and their current payment status, sorted by the user id.

The payment status of advertisers can be classified into the following categories:

  • New: Advertisers who are newly registered and have made their first payment.
  • Existing: Advertisers who have made payments in the past and have recently made a current payment.
  • Churn: Advertisers who have made payments in the past but have not made any recent payment.
  • Resurrect: Advertisers who have not made a recent payment but may have made a previous payment and have made a payment again recently.

Before proceeding with the question, it is important to understand the possible transitions in the advertiser's status based on the payment status. The following table provides a summary of these transitions:

#Current StatusUpdated StatusPayment on Day T
1NEWEXISTINGPaid
2NEWCHURNNot paid
3EXISTINGEXISTINGPaid
4EXISTINGCHURNNot paid
5CHURNRESURRECTPaid
6CHURNCHURNNot paid
7RESURRECTEXISTINGPaid
8RESURRECTCHURNNot paid
  • "Current Status" column: Represents the advertiser's current status.
  • "Payment Status" column: Represents the updated payment status based on the conditions
  • "Payment on Day T" column: Indicates whether the advertiser made a payment on a specific day (T) or not.

The transitions between payment statuses in the provided table can be summarized as follows:

  • Rows 2, 4, 6, and 8: If an advertiser does not make a payment on day T, regardless of their previous status, their payment status transitions to "CHURN" as the updated status.
  • Rows 1, 3, 5, and 7: If an advertiser makes a payment on day T, the status is updated to either "EXISTING" or "RESURRECT" based on their previous status. If the previous status was "CHURN," the updated status is "RESURRECT." For any other previous status, the updated status is "EXISTING."

Table:

Column NameType
user_idstring
statusstring

Example Input:

user_idstatus
bingNEW
yahooNEW
alibabaEXISTING

Table:

Column NameType
user_idstring
paiddecimal

Example Input:

user_idpaid
yahoo45.00
alibaba100.00
target13.00

Example Output:

user_idnew_status
bingCHURN
yahooEXISTING
alibabaEXISTING

Explanation:

  • The advertiser "bing" is classified as "CHURN" because no payment record is found in the table.
  • The advertiser "yahoo" is labeled as "EXISTING" since they have made a recent payment, as indicated by the presence of their payment record.
  • Similarly, the advertiser "alibaba" is also classified as "EXISTING" due to their recent payment.

The dataset you are querying against may have different input & output - this is just an example!

PostgreSQL 14