Uplers SQL Interview Problem (Senior Data Analyst) | Includes 4 Test Cases

Поділитися
Вставка
  • Опубліковано 4 жов 2024
  • In this video we will discuss a SQL interview problem asked in Uplers interview for senior data analyst position. There are 4 test cases as well that we need to validate.
    script:
    Create table candidates(
    id int primary key,
    positions varchar(10) not null,
    salary int not null);
    test case 1:
    insert into candidates values(1,'junior',5000);
    insert into candidates values(2,'junior',7000);
    insert into candidates values(3,'junior',7000);
    insert into candidates values(4,'senior',10000);
    insert into candidates values(5,'senior',30000);
    insert into candidates values(6,'senior',20000);
    test case 2:
    insert into candidates values(20,'junior',10000);
    insert into candidates values(30,'senior',15000);
    insert into candidates values(40,'senior',30000);
    test case 3:
    insert into candidates values(1,'junior',15000);
    insert into candidates values(2,'junior',15000);
    insert into candidates values(3,'junior',20000);
    insert into candidates values(4,'senior',60000);
    test case 4:
    insert into candidates values(10,'junior',10000);
    insert into candidates values(40,'junior',10000);
    insert into candidates values(20,'senior',15000);
    insert into candidates values(30,'senior',30000);
    insert into candidates values(50,'senior',15000);
    Zero to hero(Advance) SQL Aggregation:
    • All About SQL Aggregat...
    Most Asked Join Based Interview Question:
    • Most Asked SQL JOIN ba...
    Solving 4 Trick SQL problems:
    • Solving 4 Tricky SQL P...
    Data Analyst Spotify Case Study:
    • Data Analyst Spotify C...
    Top 10 SQL interview Questions:
    • Top 10 SQL interview Q...
    Interview Question based on FULL OUTER JOIN:
    • SQL Interview Question...
    Playlist to master SQL :
    • Complex SQL Questions ...
    Rank, Dense_Rank and Row_Number:
    • RANK, DENSE_RANK, ROW_...
    #sql #dataanalyst #interview
  • Наука та технологія

КОМЕНТАРІ • 77

  • @sankalpasarkar3540
    @sankalpasarkar3540 Рік тому +4

    Great video. The same question was asked to me in an interview few months back. Back then, I was not able to answer it properly, but thanks for the solution

  • @souptikdas9461
    @souptikdas9461 Рік тому +2

    I try to solve the problems from each and every video you upload. And I am able to solve this question in one go thanks to the concept similar to this you solved in the previous video you uploaded perhaps a few months back. Thanks for the concept.

  • @VAR21723
    @VAR21723 Рік тому +3

    Very useful. Please upload like this every day

  • @Datapassenger_prashant
    @Datapassenger_prashant 2 місяці тому

    This was definately an amazing video. Althought, I had an experience of solving similar approach thru your videos, but the test cases added bit more challenges and extra steps added to it, but it was easy to follow later steps as we alreaady built the main query and it was all about adjusting as per test cases.
    my final query:
    With Seniors_Selection as (
    Select *, sum(salary) over(order by salary asc rows between unbounded preceding and current row) as Running_Salary_Sum from candidates
    where positions = 'Senior'
    )
    , Seniors_Hiring_Budget as (
    Select count(*) as Seniors, sum(salary) as Amount_used, case when Sum(salary) is null then 50000 else 50000-sum(salary) end as Amount_Remaining from Seniors_Selection
    where Running_Salary_Sum

  • @akash_0411
    @akash_0411 Рік тому

    Thanks for the explanation
    I have approached the problem like this : (Worked for all the test cases)
    with cte1 as
    (select *,sum(salary) over (partition by positions order by salary,id ) as rs from candidates)
    ,cte2 as
    (select * from cte1 where rs

  • @lucifer_878
    @lucifer_878 11 місяців тому

    Thankyou for the great video.
    with cte as(select b.*, sum(salary) over (partition by positions order by rn) total_amount from(
    select c.*, row_number() over (partition by positions order by salary) rn from candidates c) b),
    cte1 as (select * from cte
    where (positions='senior' and total_amount

  • @MixedUploader
    @MixedUploader 9 місяців тому

    Ankit pausing the video I used 'isnull(sum(salary),0)' to handle null value for test case 3, it worked.

  • @dasubabuch1596
    @dasubabuch1596 3 місяці тому

    Hi Ankit sir,
    with t as
    (
    select id, positions,salary,sum(salary)over(partition by positions order by salary) as sm from candidate1
    order by positions desc
    ),
    t1 as
    (
    select t.*, case when sm

  • @sivasrimakurthi206
    @sivasrimakurthi206 Рік тому +2

    always fan of your enjoyable content and well designed approach to any problem.

  • @dfkgjdflkg
    @dfkgjdflkg 8 місяців тому

    As usual, great content. I just get how you find solutions. I guess it is talent, natural.

  • @Tech_with_Srini
    @Tech_with_Srini 20 днів тому

    with cte as(
    select *, sum(salary) over( partition by positions order by salary,id) running_sal from candidates),
    seniorCTE as(
    select *
    from cte
    where running_sal

  • @2412_Sujoy_Das
    @2412_Sujoy_Das 10 місяців тому

    Sir, my query is as follows:
    with cte_1 as
    (Select id, positions, salary, SUM(salary) OVER(order by positions DESC, salary ASC rows between unbounded preceding and current row) as net_SUM
    from
    (Select *, SUM(salary) OVER(partition by positions order by salary, id rows between unbounded preceding and current row) as rnk
    from candidates) xyz
    where rnk

  • @MuskanGoyal-db7cs
    @MuskanGoyal-db7cs 3 місяці тому

    with cte as(
    select *, row_number() over(partition by positions order by salary) as rn
    from candidate3),
    cte2 as(
    select * ,
    sum(salary) over(partition by positions order by rn) as sum_i
    from cte
    order by positions desc
    ),
    cte3 as(
    select * from cte2
    where sum_i

  • @Alexpudow
    @Alexpudow 8 місяців тому

    select count(case positions when 'junior' then 1 end) 'Juniors', count(case positions when 'senior' then 1 end) 'Seniors'
    from (select positions, sum(salary) over(order by case positions when 'senior' then 0 else 1 end, salary rows between unbounded preceding and current row) sm
    from (select id, positions, salary
    ,sum(salary) over(partition by positions order by positions, salary rows between unbounded preceding and current row) ss
    from candidates) t
    where ss

  • @vaibhavverma1340
    @vaibhavverma1340 Рік тому

    My sol, Passing all the testcases :)
    with cte as
    (Select *, sum(salary) over (partition by positions order by salary,id)sum_sal from candidates)
    select 'senior' as positions, count(id)as total_cnt from cte
    where positions = 'senior' and sum_sal

  • @gourav1343
    @gourav1343 11 місяців тому +1

    Hi Ankit Brother
    First of all many thanks for these 56 valuable videos so far, they are really helping us in our SQL journey.
    In Addition I just wants to know your perspective upon will one will be able to crack the SQL technical interview if one have had prepared all these videos properly.
    your take on that will be very insightful.

  • @Narendra_live
    @Narendra_live 9 місяців тому

    with cte as(
    select *,
    sum(salary) over(partition by positions order by salary) as total_sal
    from candidates where positions='senior'
    ),
    cte2 as(
    select * from cte
    where total_sal

  • @hiralalpatra500
    @hiralalpatra500 Рік тому

    with cte as(
    with cte as(
    select *,sum(salary) over(partition by positions order by salary,id) as sal from candidates)
    ,cte2 as(select
    case when sal

  • @lokeshmadiga6215
    @lokeshmadiga6215 10 місяців тому

    Great Video,
    This is my Query :
    WITH running_sum as
    (SELECT
    *,SUM(salary) OVER(partition by positions order by salary) as running_sum
    FROM practice.candidates
    ORDER BY positions desc)
    SELECT
    SUM(CASE WHEN positions = 'senior' THEN 1 ELSE 0 END) AS 'Seniors',
    SUM(CASE WHEN positions != 'senior' THEN 1 ELSE 0 END) AS 'Juniors'
    FROM running_sum
    WHERE running_sum

  • @rohanchoudhary672
    @rohanchoudhary672 4 місяці тому

    delete from candidates;
    insert into candidates values(1,'junior',5000);
    insert into candidates values(2,'junior',7000);
    insert into candidates values(3,'junior',7000);
    insert into candidates values(4,'senior',10000);
    insert into candidates values(5,'senior',30000);
    insert into candidates values(6,'senior',20000);
    with running_cte as(
    select
    id,
    positions,
    sum(salary) over(partition by positions order by salary asc, id) as running_sal
    from candidates
    ), senior_cte as (
    select
    count(id) as sen_count,
    max(running_sal) as amount_senior
    from running_cte
    where positions = 'senior' and running_sal

  • @akashkonda8385
    @akashkonda8385 11 місяців тому

    with cte1 AS (
    Select *, sum(Salary) over(partition by positions order by salary rows between unbounded preceding and current row) as rolling_sum
    from candidates),
    cte2 AS (
    Select count(*) as Seniors, max(rolling_sum) as seniors_total_salary
    from cte1
    where positions = 'senior' and rolling_sum

  • @leojoyjr2755
    @leojoyjr2755 Рік тому

    Keep inspiring 🌟😍💓 great solution

  • @AksTriv
    @AksTriv Рік тому

    with cte as
    (
    select a.*,
    sum(salary) over (partition by positions order by salary rows unbounded preceding ) running_balance
    from candidates_4 a
    ),
    cte2 as
    (select q1.*, sum(salary) over (order by positions desc, salary rows unbounded preceding) expense from cte q1 where running_balance

  • @stat_life
    @stat_life Рік тому

    Your approach was the best sir,
    Direct and Simplified✅✅
    WITH running_cte AS (
    SELECT *,
    SUM(salary) OVER (PARTITION BY positions ORDER BY salary ASC, id) AS running_sal
    FROM candidates
    )
    , senior AS (
    SELECT COUNT(*) AS seniors, COALESCE(SUM(salary),0) AS s_sal
    FROM running_cte
    WHERE positions = 'senior'
    AND running_sal

  • @rajasharma9886
    @rajasharma9886 5 місяців тому

    with cte as (
    select *, sum(salary) over (partition by positions order by salary,id asc ) as cum_sal
    from candidatess
    ) , cte2 as (
    select id,positions, salary,cum_sal
    from cte
    where positions='senior' and cum_sal

  • @karangupta_DE
    @karangupta_DE Рік тому

    with cte as (
    select *,
    sum(salary)over(partition by positions order by salary, id) as running_total,
    50000 - sum(salary)over(partition by positions order by salary, id) as remaining_balance
    from candidates
    ),
    cte1 as (
    select count(*) as total_seniors
    from cte
    where positions = 'senior'
    and remaining_balance > 0
    ),
    cte2 as (
    select * , (select coalesce(min(remaining_balance), 50000) from cte where positions = 'senior' and remaining_balance > 0) as balance
    from cte
    where true
    and positions = 'junior'
    ),
    cte3 as (
    select count(*) as total_juniors
    from cte2
    where true
    and balance - running_total >= 0
    )
    select (select total_seniors from cte1) as total_seniors, (select total_juniors from cte3) as total_juniors;

  • @saurabhpandey5296
    @saurabhpandey5296 Рік тому

    with cte as
    (select positions, salary, 50000 - sum(salary) over(order by positions desc, salary asc) as diff_sal
    from candidates
    order by positions desc, salary asc
    ),
    cte2 as
    (
    select positions, salary, 50000 - sum(salary) over(order by positions desc, salary asc) as diff_sal
    from cte
    where (positions = 'senior' and diff_sal >= 0) or positions = 'junior'
    order by positions desc, salary asc
    )
    select coalesce(sum(case when positions = 'junior' then cnt end),0) as 'Juniors',
    coalesce(sum(case when positions = 'senior' then cnt end),0) as 'Seniors'
    from
    (select positions, count(*) as cnt
    from cte2
    where (positions = 'senior' and diff_sal >= 0) or (positions = 'junior' and diff_sal >= 0)
    group by 1) a1

  • @mohammadshahbaz3287
    @mohammadshahbaz3287 Рік тому

    @ankit here is my solution
    with seniors as(
    select *, sum(salary) over(order by salary ) as cum_sal
    , 50000 - sum(salary) over(order by salary ) as delta
    , rank() over(order by salary) as rnk
    from candidates
    where POSITIONS='senior'
    )
    , remain_budget as(
    select
    case when DELTA>=0 then DELTA else 50000 end
    as DELTA from seniors
    where (DELTA>=0 and rnk=(select max(rnk) from seniors))
    or (DELTA=0
    union
    select * from juniors
    where DELTA>=0
    ;

  • @Vlogs..573
    @Vlogs..573 Рік тому

    Thanks for the video , it was great..

  • @mallikarjunpatil7553
    @mallikarjunpatil7553 Рік тому

    Once Again Great Video Bro!!

  • @akedia759
    @akedia759 7 місяців тому

    hey @Ankit Bansal i have seen you vedio and solved in sql work bench i got correct result for test case 1 but not getting correct o/p for test case 2 and 3.
    please show my code onces
    with running_cte as
    (select *,sum(salary) over(partition by positions order by salary asc,id) as running_sal
    from candidates),
    senior_cte as
    (select count(*) as seniors,coalesce(sum(salary),0) from running_cte
    where running_sal

  • @nikhilreddy4582
    @nikhilreddy4582 Рік тому

    My solution for test case 1
    "select max(case when positions = 'junior' then cnt end) as Juniors,
    max(case when positions = 'Senior' then cnt end) as Seniors from
    (select positions,count(positions) cnt from(select positions,sum(salary) over(partition by positions order by salary,id)as total
    from candidates_one)A
    where total

  • @rajeshkanna21104
    @rajeshkanna21104 Рік тому +1

    Hi bro , can u explain about context switch......

  • @sukanyaiyer2671
    @sukanyaiyer2671 Рік тому

    Thank you...for solving my qstn

  • @sandipansarkar9211
    @sandipansarkar9211 11 місяців тому

    finished watching

  • @anish_bhateja
    @anish_bhateja Рік тому

    Here's my sol:
    with cte as (select *, sum(salary) over(partition by positions order by positions desc, salary,id) as runnin_sal from candidates) ,cte2 as (select count(*) as Seniors,max(runnin_sal) as runnin_sal from cte where runnin_sal

  • @kirangadhe4962
    @kirangadhe4962 Рік тому

    same question which you already covered with some test cases.

  • @corporatechic2742
    @corporatechic2742 10 місяців тому

    HI, Can you please tell Java developer interview process

  • @HarshKukreja052
    @HarshKukreja052 Рік тому

    If I am not wrong, you have solved similar question for one of the FAANG company before.
    But nevertheless, good revision of all the concepts 🙏

    • @ankitbansal6
      @ankitbansal6  Рік тому

      Yes it's similar. Just different test cases here .

  • @ChanchalGoel-o2q
    @ChanchalGoel-o2q Рік тому

    Can you give online classes for sql and informatica cloud?

  • @anuragsrivastava52
    @anuragsrivastava52 Рік тому +2

    positions='senior' not working on Mysql.

  • @hustlerguy6091
    @hustlerguy6091 Місяць тому

    My Approach:
    with senior_hires as (
    select positions,
    SUM(CASE WHEN cumuSalary

  • @rohanapte
    @rohanapte Рік тому +1

    My Solution -:
    WITH cte AS (
    SELECT *, SUM(salary) OVER(PARTITION BY positions ORDER BY salary,id) running_sum
    FROM Candidates);

    SELECT
    DISTINCT (
    SELECT COUNT(*) FROM cte
    WHERE positions='senior' AND running_sum

  • @pushkardafre319
    @pushkardafre319 Рік тому

    Hi Ankit
    Aggregate function also called as Group function? what is the use of variance and standard daviation
    Just for information

  • @mattmatt245
    @mattmatt245 Рік тому

    It would be much easier to use excel solver for this type of problem.

  • @UnrealAdi
    @UnrealAdi Рік тому

    Test Case 3:
    with cte as (
    select *,
    sum(salary) over(partition by positions order by positions desc, salary desc) cumulative,
    (50000 - sum(salary) over(partition by positions order by positions desc, salary desc)) remain
    from candidates3
    order by positions desc, salary desc
    ),
    cte1 as (
    select count(*) seniors, coalesce(min(remain), 0) remain
    from cte
    where cumulative = (select remain from cte1)
    )
    select seniors, juniors
    from cte1 join cte2
    ;

  • @himanshuparihar9888
    @himanshuparihar9888 Рік тому

    with cte as (
    select count(*) as s , ifnull(max(s),0) as m from
    (select positions , sum(salary) over(order by salary ) as s
    from candidate where positions = 'senior')
    as k where s < 50000 ) ,
    cte2 as (
    select count(*) as j from
    (select positions , sum(salary) over(order by salary ) as sj
    from candidate where positions = 'junior') as k
    where sj

  • @lakshaykhanna2462
    @lakshaykhanna2462 8 місяців тому

    MySQL solution:
    WITH cte_1 AS
    (
    SELECT *, SUM(salary) OVER(PARTITION BY positions ORDER BY salary ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS r_salary
    FROM candidates
    ), cte_2 AS
    (
    SELECT id, positions, salary
    FROM cte_1
    WHERE positions = 'senior' AND r_salary

  • @manasijoshi5009
    @manasijoshi5009 Рік тому

    Hello Sir, How much DSA is required for Data Engineer Role, please reply, I am eagarly waiting for your reply

  • @SourovRoy-ds8ug
    @SourovRoy-ds8ug Рік тому

    oracle:
    with senior_count as(
    select count(id) s_id, nvl(sum(SALARY),0) senior_sal from(
    select id,SALARY, sum(SALARY) over( order by rn) running_sum
    from(
    select id,POSITIONS,SALARY,row_number() over( order by SALARY)rn from candidates_test where POSITIONS ='senior'
    ))
    where running_sum

  • @MrGaurav331
    @MrGaurav331 10 місяців тому

    WITH test AS (
    SELECT
    id,
    positions,
    salary,
    SUM(salary) OVER (ORDER BY salary DESC, id) AS new_budget
    FROM (
    SELECT
    *,
    SUM(salary) OVER (PARTITION BY positions ORDER BY salary DESC, id) AS budget
    FROM candidates
    ) cte
    WHERE budget

  • @jhonsen9842
    @jhonsen9842 7 місяців тому

    Could you please make a vidio on this question asked in Mercedez Benz
    Team-1 Team-2 Winner
    A B A
    B A B
    C D D
    A E A
    N D N
    B N B
    WinTeam MatchesWon
    TeamA 2
    TeamB 2
    TeamC 0
    TeamN 1
    TeamD 1

  • @aniketghodinde3041
    @aniketghodinde3041 11 місяців тому

    select COALESCE(sum(junior_flag),0) as junior,COALESCE(sum(senior_flag),0) as senior from (
    select *,sum(salary) over (order by sal_rank1) as sal_sum1 from (
    select *, row_number() over (order by positions desc,salary asc) as sal_rank1 from
    (select *,dense_rank() over (Partition by positions order by salary) as sal_rank,sum(salary) over (Partition by positions order by salary) as sal_sum,
    CASE WHEN positions = 'senior' THEN 1 END AS senior_flag,CASE WHEN positions = 'junior' THEN 1 END AS junior_flag
    from candidate ) l
    where sal_sum

  • @UnrealAdi
    @UnrealAdi Рік тому

    Please check this out & let me know if it's flawed!
    Thanks.
    with cte as (
    select *,
    sum(salary) over(partition by positions order by positions desc, salary asc, id) as cumulative,
    (50000 - sum(salary) over(partition by positions order by positions desc, salary asc, id)) remain
    from candidates1
    order by positions desc, salary
    ),
    cte1 as (
    select count(*) seniors, min(remain) remain from cte where positions = 'senior' and cumulative

  • @edumail1016
    @edumail1016 2 місяці тому

    WITH cte AS (
    SELECT * FROM (
    SELECT positions, salary, SUM(salary) OVER (PARTITION BY positions ORDER BY salary) AS rn_sum
    FROM candidates
    WHERE positions='senior'
    ) T
    WHERE rn_sum

  • @rihanalli3754
    @rihanalli3754 Рік тому

    here i solve this quetions with 2 times cte function and case clause
    with cte as(
    select *,
    sum(salary) over(partition by positions order by salary asc) as cumulative
    from Emple),cte1 as
    (select *from cte where positions in('senior','junior') and cumulative

    • @UnrealAdi
      @UnrealAdi Рік тому

      Hi Rihan, thanks for the query. However, the logic seems to be a bit flawed. You are looking for candidates whose cumulative salary is

    • @shafi123
      @shafi123 Рік тому +1

      @@UnrealAdiAgree with you. The query does not even work for single test case

    • @UnrealAdi
      @UnrealAdi Рік тому

      @@shafi123 please checkout my solution for the 1st test case! It is kinda similar to ankit's solution.
      Let me know if you think if there is any flaw with it👍