Magicsheet logo

Market Analysis II

Hard
100%
Updated 6/1/2025

Asked by 1 Company

Topics

Market Analysis II

What is this problem about?

Market Analysis II is a much more complex SQL problem than Part I. You are given Users, Orders, and Items tables. Your goal is to determine, for each user, whether the item they bought in their second order (chronologically by date) is of the same brand as their favorite brand (listed in the Users table). The result should return every user and a "yes" or "no" status.

Why is this asked in interviews?

This is a Hard-level SQL question that tests advanced window functions. Interviewers ask it to evaluate if a candidate can isolate a specific sequence event (the "second" occurrence) for multiple independent entities (users) simultaneously. It requires combining ranking functions with left joins, showcasing a deep understanding of relational data alignment.

Algorithmic pattern used

This problem requires a CTE (Common Table Expression) with Window Functions.

  1. Create a CTE that ranks the orders for each user based on the order date. You use ROW_NUMBER() OVER(PARTITION BY buyer_id ORDER BY order_date ASC) AS rnk.
  2. Select only the rows from this CTE where rnk = 2 (isolating the second order).
  3. LEFT JOIN the main Users table to this filtered CTE, and then join the Items table to get the brand of that second order.
  4. Use a CASE statement in the SELECT clause: if the item's brand matches the user's favorite brand, output "yes", otherwise "no".

Example explanation

Users: User 1 (Fav: Samsung), User 2 (Fav: Apple). Orders:

  • User 1: Order A (Sony, Jan 1), Order B (Samsung, Jan 5), Order C (LG, Jan 10)
  • User 2: Order D (Apple, Jan 2)

CTE assigns ranks:

  • User 1: Order A (Rank 1), Order B (Rank 2), Order C (Rank 3)
  • User 2: Order D (Rank 1)

Filtered CTE for Rank 2:

  • Only Order B (Samsung) remains for User 1. User 2 has no Rank 2.

Final Join:

  • User 1 LEFT JOIN Order B. Brand matches Fav Brand? Yes.
  • User 2 LEFT JOIN nothing. Brand matches? No.

Common mistakes candidates make

A frequent mistake is using MIN() or MAX() instead of ROW_NUMBER(). While MIN() can find the first order, finding the second order using nested MIN() queries is incredibly messy and fragile. Another error is using an INNER JOIN from the Users table to the ranked orders, which drops users who only placed 1 order (or 0 orders) entirely from the final output, failing the requirement to return all users.

Interview preparation tip

When an interview question asks for the "N-th" item in a sequence (second order, third highest salary, latest login), immediately default to using ROW_NUMBER() OVER(PARTITION BY ... ORDER BY ...). It is the cleanest, most scalable way to index relational data chronologically before applying specific row-number filters.

Similar Questions