LeetCode 1587 Interview SQL Question with Detailed Explanation | Practice SQL

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

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

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

    with cte as(
    select u.name,(select sum(t.amount) from transactions t group by t.account having t.account=u.account)
    as balance from users u
    )
    select * from cte where balance > 10000;
    I have learnt to use cte from your videos and now i able to apply my logic.

  • @parikshitgupta343
    @parikshitgupta343 10 месяцев назад

    I have solved the same problem like this
    WITH cte AS
    (SELECT account, SUM(amount) AS balance
    FROM Transactions
    GROUP BY account),
    cte2 AS
    (SELECT *
    FROM cte
    WHERE balance > 10000)
    SELECT name, balance
    FROM cte2 LEFT JOIN Users
    USING (account)
    The code is big, but isn't it better to break down big table into small and then join them. We can have faster query execution like that

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

    I feel that group by should be done using account and not name as account is a primary key and name can be duplicated.

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

      Great catch, Madhur. Yes, since in the question there was no repetition in the name, GROUP BY name also works, but ideally it should be GROUP BY account number.

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

      But we are not returning account column or any agregate function of it so how can we group by account

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

    33