Forum

Posted by Sylvia, April 17, 2022, 10:22 a.m.

Question 78# MAX(store_id)

I have three questions for Question #78: 

1. Why is INNER JOIN used here instead of LEFT JOIN? Customers that do not have any rental history should be counted as part of the ranking as well; the solution only takes into account the customers that had a rental in the past. 

2. If one customer can belong to more than one store, is the only part that should be changed "MAX(C.store_id) As store_id" ?

    Instead of using MAX(), we need to use customer_id, store_id, count(*) as num_rentals ... GROUP BY customer_id, store_id. 

3. There isn't any description in the question that asked us to create QUARTILE within each store, therefore the "PARTITION BY store_id" may not be used. OR the question can add the description to clarify that the QUARTILE is created for each store. 

Solution: 

WITH cust_rentals AS (
    SELECT C.customer_id, 
    MAX(C.store_id) AS store_id, -- one customer can only belong to one store
    COUNT(*) AS num_rentals FROM 
    rental R
    INNER JOIN customer C
    ON C.customer_id = R.customer_id
    GROUP BY C.customer_id
)
SELECT customer_id, store_id, quartile
FROM (
    SELECT 
        customer_id,
        store_id,
        NTILE(4) OVER(PARTITION BY store_id ORDER BY num_rentals) AS quartile
    FROM cust_rentals
) X
WHERE customer_id IN (1,2,3,4,5,6,7,8,9,10);

My answer: 

WITH customer_rental AS (
  SELECT a.customer_id, 
  MAX(a.store_id) as store_id, 
  count(b.rental_id) as num_rental
  FROM customer a
  LEFT JOIN rental b
  ON a.customer_id = b.customer_id
  GROUP BY a.customer_id
  )
SELECT * FROM (
SELECT customer_id, store_id, 
       NTILE(4) OVER (ORDER BY num_rental) as quartile
FROM customer_rental
  ) X
WHERE customer_id IN (1,2,3,4,5,6,7,8,9,10)

 

 

Answers

Hey Sylvia,

Thanks for your questions, those are great questions.

#1, we did have a comment in the CTE that one customer can only belong to one store, perhaps you didn't see it, we can add it to question instruction as well.

#2. A customer only becomes a customer once they rent, so there is no need to worry that a customer never rent since the topic of the first 80 questions is all about movie rentals, (there is no other business lines such as buying food), there is absolutely no need to worry a customer didn't rent in their lifetime.

#3. It's common sense to compare customers' spend only with other customers within the same store, otherwise, the analysis doesn't make any sense, i.e., why would you even pull customers' number of Uber trips percentile in Manhattan vs. some small town in the midwest. 

Keep in mind that all those questions are designed to grow your business and product sense. We are not constructing a question just for the sake of testing your technical capabilities, but want you to come home and have a better understanding of business.

-Leon

 

Hi Leon,

Thank you very much for your detailed answers! 

Your reminder "those questions are designed to grow your business and product sense" makes a lot of sense. I'll keep that in mind when doing technical interviews and try to take into account information that is not EXPLICITLY stated in the question. I appreciate your sharing your perspective as an interviewer! 

Sylvia

Sylvia, April 17, 2022, 4 p.m.

You bet, Sylvia, really liked your coding style, by the way, looks clean, consistent, and easy to understand! 😃

Keep up the great work!

Leon, April 18, 2022, 1:52 p.m.
SQLPad user avatar

Leon (949)

April 17, 2022, 10:39 a.m.