LeetCode Medium 1596 Amazon Interview SQL Question with Detailed Explanation

Поделиться
HTML-код
  • Опубликовано: 13 дек 2024

Комментарии • 16

  • @florincopaci6821
    @florincopaci6821 2 года назад +9

    it's impossible not to understand how you explain, I haven't seen anyone explain how you explained for example the problem with self join with managers up to level 3. You should be a trainer if you are not already. All the best!

    • @EverydayDataScience
      @EverydayDataScience  2 года назад +1

      Thank you Florin for such kind words. I’m glad these videos are helping some people.

    • @florincopaci6821
      @florincopaci6821 2 года назад +1

      @@EverydayDataScience A lot.Thank you

  • @yashnikhare2210
    @yashnikhare2210 2 года назад +3

    We can also use Rank Function cte2 as (select *,rank() over(partition by customer_id order by num_ordered desc ) as rnk from cte Then use where clause (where rnk=1)

    • @mickyman753
      @mickyman753 22 часа назад

      yeah just instead of count=first value, rank=1 wala filter aaega last select query mai

  • @rinkali7654
    @rinkali7654 2 года назад

    Very very helpful! Could you please tell when SQL medium questions will have more uploads?

  • @kavyatripathi2151
    @kavyatripathi2151 Год назад

    Please solve Hoppers Company questions on leetcode sql

  • @harshilgandhi8415
    @harshilgandhi8415 2 года назад

    Could we have used max window function in the cte2?

    • @EverydayDataScience
      @EverydayDataScience  2 года назад +2

      Great catch, Harshil. Yes, we can use MAX as well. The cte2 code will look something like this:
      cte2 AS
      (SELECT *, MAX(num_ordered) OVER(PARTITION BY customer_id) AS most_frequent
      FROM cte

    • @adityams1659
      @adityams1659 2 года назад

      yess,
      But if you think about it, the shown method is more efficient.
      Since you have already used *Order by num_ordered desc* the first value by default is the value we want. if we use Max on it again we are unnecessarily increasing computational complexity

  • @lakshmivenkatavarun8406
    @lakshmivenkatavarun8406 Год назад

    Can anyone explain why we used LEFT JOIN to join cte2 and products?

    • @nitinmadan4009
      @nitinmadan4009 7 месяцев назад

      because we want to keep all the customer_id from the cte2.

  • @mlvprasadofficial
    @mlvprasadofficial 2 года назад

    55

  • @james-r685
    @james-r685 Год назад

    another solution:
    with cte1 as (
    select customer_id,product_id,count(1) as ct from orders
    group by customer_id,product_id
    ),
    cte2 as (
    select *,dense_rank() over(partition by customer_id order by ct desc) as dr from cte1
    )
    select a.customer_id,a.product_id,b.product_name from cte2 a
    left join products b on a.product_id=b.product_id where dr=1