Mphasis SQL Interview question - UPDATE scores column values with department wise maximum score

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

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

  • @nagabhushannaga1801
    @nagabhushannaga1801 8 месяцев назад +2

    Good explanation and good to know possible ways to write logic

    • @CloudChallengers
      @CloudChallengers  8 месяцев назад

      @nagabhushannaga1801, Thanks for the encouragement

  • @snehsparsh7954
    @snehsparsh7954 10 часов назад

    -- Approach 1: Using a subquery in the FROM clause
    UPDATE
    practiceDB1.emp_dept_tbl2 AS target
    SET
    scores = sub.max_score
    FROM
    (
    SELECT
    dept_name,
    MAX(scores) AS max_score
    FROM
    practiceDB1.emp_dept_tbl2
    GROUP BY
    dept_name
    ) AS sub
    WHERE target.dept_name = sub.dept_name;
    -- Approach 2: Using a MERGE statement
    MERGE practiceDB1.emp_dept_tbl2 AS target
    USING (
    SELECT
    dept_name,
    MAX(scores) AS max_score
    FROM
    practiceDB1.emp_dept_tbl2
    GROUP BY
    dept_name
    ) AS source
    ON target.dept_name = source.dept_name
    WHEN MATCHED THEN
    UPDATE SET scores = source.max_score;

  • @masterhipster8358
    @masterhipster8358 2 месяца назад

    with cte as (
    select
    *
    , max(scores) over(partition by dept) as max_score
    from empdept_tbl
    order by eid
    )
    update empdept_tbl
    set scores = cte.max_score
    from cte
    where empdept_tbl.eid = cte.eid;
    select * from empdept_tbl

  • @landchennai8549
    @landchennai8549 3 месяца назад

    ; with cte
    as
    (
    select *
    , FIRST_VALUE(scores) over (partition by dept order by scores desc) as NewScore
    from #empdept_tbl
    )
    update cte set scores = NewScore

  • @mahenderchilagani5916
    @mahenderchilagani5916 5 месяцев назад +2

    select eid, dept, max(scores)over(partition by dept) as scores from empdept_tbl

    • @riteshhraj8033
      @riteshhraj8033 12 дней назад

      your query is correct but its not updating teh original table :).the thing is if yu do again select * from tablename ,you will see old table again :).

  • @rudrakshisrivastava3417
    @rudrakshisrivastava3417 4 месяца назад +1

    with cte as(SELECT *,
    MAX(Scores) over(partition by dept) as max_dept_score FROM employee_scores
    order by eid)
    UPDATE employee_scores set scores=(select max_dept_score from cte where cte.eid=employee_scores.eid)
    select * from employee_scores

  • @chandanpatra1053
    @chandanpatra1053 8 месяцев назад +2

    I have solved using rows between preceding & following concept
    select eid,dept ,
    max(scores) over(partition by dept order by scores rows between unbounded preceding and
    unbounded following) as 'scores'
    from empdept_tbl

    • @CloudChallengers
      @CloudChallengers  8 месяцев назад

      Thanks for sharing different approach Chandan

  • @pravinshinde1891
    @pravinshinde1891 5 месяцев назад +2

    select eid,dept, max(scores)over(partition by dept order by scores desc) as scores from empdept_tbl
    order by eid

  • @vijay.s-ll1yq
    @vijay.s-ll1yq 8 месяцев назад +1

    Nice Logic

  • @hairavyadav6579
    @hairavyadav6579 4 месяца назад +1

    my sql approach
    update empdept_tbl join (select *,max(scores) over(partition by dept) max_score from empdept_tbl) as
    new_emp on empdept_tbl.dept =new_emp.dept set empdept_tbl.scores= new_emp.max_score;

  • @saravanansevanan
    @saravanansevanan 5 месяцев назад +1

    --Solution_1
    select *, max(scores) over (partition by dept order by eid
    rows between unbounded preceding and unbounded following) as max_score from empdept_tbl
    --Soution_2
    select eid, dept,
    case when dept = 'd1' then (select max(scores) from empdept_tbl_cc where dept = 'd1')
    when dept = 'd2' then (select max(scores) from empdept_tbl_cc where dept = 'd2')
    when dept = 'd3' then (select max(scores) from empdept_tbl_cc where dept = 'd3')
    when dept = 'd4' then (select max(scores) from empdept_tbl_cc where dept = 'd4')
    end as score
    from empdept_tbl

  • @dennurajan7897
    @dennurajan7897 8 месяцев назад +1

    Can we use a case function here🤔

    • @CloudChallengers
      @CloudChallengers  8 месяцев назад

      @dennurajan78971, yes you can use. But you need to put efforts in writing logic. Instead we can go with other simple logic.

  • @divyaborse5866
    @divyaborse5866 8 месяцев назад +1

    with cte as (select dept,max(scores) as max_score from empdept_tbl group by dept )
    select a.eid,a.dept,b.max_score as scores from empdept_tbl a left join cte b on a.dept = b.dept;

    • @divyaborse5866
      @divyaborse5866 8 месяцев назад +1

      sry I have problem statement wrong update part is missing
      updated query
      with cte as (select dept,max(scores) as max_score from empdept_tbl group by dept ),
      cte2 as (select a.eid,a.dept,b.max_score as scores from empdept_tbl a left join cte b on a.dept = b.dept)
      update empdept_tbl set scores = b.scores from empdept_tbl a inner join cte2 as b on a.dept = b.dept;

  • @vijay.s-ll1yq
    @vijay.s-ll1yq 7 месяцев назад +1

    update a set scores = b.cnt from
    empdept_tbl a inner join
    (select max(scores) cnt,dept from empdept_tbl
    group by dept) b
    on a.dept =b.dept

  • @chinmay.dunakhe
    @chinmay.dunakhe 8 месяцев назад +1

    My Solution: with cte as(
    select *, row_number() over(partition by dept order by scores desc) rn
    from empdept_tbl)
    select eid, dept, scores, case when rn = 1 then scores else (select max(scores) from empdept_tbl t2 where cte.dept = t2.dept) end as new
    from cte
    order by eid;

    • @CloudChallengers
      @CloudChallengers  8 месяцев назад

      Thanks for sharing different approach Chinmay.

  • @ishanshubham8355
    @ishanshubham8355 8 месяцев назад +2

    update empdept_tbl e
    inner join (
    select eid,max(scores) over(partition by dept) as scores1 from empdept_tbl) m
    using(eid)
    set scores = scores1;

  • @CEMANIVANNAN
    @CEMANIVANNAN 8 месяцев назад +1

    with t1 as(select *,dense_rank() over(partition by dept order by scores desc) dn from empdept_tbl qualify dn=1)
    ,t2 as(select eid,dept from empdept_tbl)
    select t2.eid,t2.dept,t1.scores from t1 right join t2 on t1.dept=t2.dept order by 1;

  • @Nikhil-xw2wc
    @Nikhil-xw2wc Месяц назад

    WITH DeptMaxScores AS (
    SELECT
    dept,
    MAX(scores) AS max_score
    FROM
    empdept_tbl
    GROUP BY
    dept
    )
    UPDATE empdept_tbl
    SET scores = d.max_score
    FROM empdept_tbl e
    JOIN DeptMaxScores d
    ON e.dept = d.dept;

  • @HARSHRAJ-gp6ve
    @HARSHRAJ-gp6ve 4 месяца назад +1

    with cte as(
    select dept,MAX(scores) as x1 FROM empdept_tbl GROUP BY dept
    )
    select eid,cte.dept,x1 FROM cte JOIN empdept_tbl ON cte.dept=empdept_tbl.dept;

  • @SaifKhan-sd2gd
    @SaifKhan-sd2gd 8 месяцев назад +1

    with cte as(
    select *, row_number() over(partition by dept order by scores desc) as rnk
    from empdept_tbl
    )
    select eid, dept, FIRST_VALUE(scores) over(partition by dept ORDER by rnk) as new_scores from cte
    order by eid

    • @CloudChallengers
      @CloudChallengers  8 месяцев назад

      @SaifKhan-sd2gd, can u try having update in your query?

  • @kushmanthreddy4762
    @kushmanthreddy4762 8 месяцев назад +1

    select eid,dept,first_value(scores) over(partition by dept order by dept,scores desc range between unbounded preceding and unbounded following) as fi
    from empdept_tbl order by eid ;

  • @monasanthosh9208
    @monasanthosh9208 8 месяцев назад +1

    First Comment 😀
    MYSQL Solution
    Select eid,Dept,first_value(Scores) Over (Partition by Dept Order by Scores desc) as Scores from Empdept_Tbl
    order by eid;
    Updation not working in mysql

    • @CloudChallengers
      @CloudChallengers  8 месяцев назад

      @monasanthosh9208, Thanks for quick turn around 🙂

  • @UnrealAdi
    @UnrealAdi 7 месяцев назад +1

    Are these SQL questions for freshers or 3+ years of experience candidate?

    • @CloudChallengers
      @CloudChallengers  7 месяцев назад +1

      @UnrealAdi, This question is asked for experienced candidate with 3+ years of experience in Data Analytics.

  • @sportsblast8215
    @sportsblast8215 8 месяцев назад +1

    update empdept_tbl
    set scores=5.28
    where dept='d1';

    • @CloudChallengers
      @CloudChallengers  8 месяцев назад

      @sportsblast8215, this query will update values for dept d1 only. We have to update others department values as well.