Department Wise Highest Salary | Ep-2 | Top 20 SQL Interview Questions | GeeksforGeeks

Поділитися
Вставка
  • Опубліковано 26 лис 2024

КОМЕНТАРІ • 57

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

    -- USING WINDOWS FUNCTION
    select * from (select *, ROW_NUMBER() Over(partition by dept order by salary desc) as highest_payed
    from Employee)
    where highest_payed = 1
    --(Just change the (Nth value) number 1 to 2 or 5 in order to find the 2nd or 5th highest payed employee)
    -- Simple method
    select dept, max(salary) from Employee
    group by dept

  • @mayanksingh7814
    @mayanksingh7814 5 місяців тому +1

    with cte as (
    select *, dense_rank(sal) over(partition by dept_no order by sal desc) as 'rnk'
    from emp
    )
    select ename, sal
    from cte
    where rnk = 1

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

    select salary , department_name from
    (select salary,department_name, row_number() over(partition by department_name order by salary desc) as rn
    from employee
    ) where rn =1

  • @chandershekhar7439
    @chandershekhar7439 2 роки тому

    q)what if we need name of highest salary dept wise?
    ans). select name , sal dept from (select max(sal) as max_sal_dept wise, dept from emp groupby(dept)) inner join emp on sal and dept;

  • @prasadphatak1503
    @prasadphatak1503 6 місяців тому +6

    But the question was to find the employee with highest salary

    • @sameersawal28
      @sameersawal28 5 днів тому

      Can be solved using window function

  • @sachinupreti7159
    @sachinupreti7159 2 роки тому +3

    Excellent content in 4 min😇🔥

  • @algorithmsguide5076
    @algorithmsguide5076 5 років тому +5

    Here is the query to find second highest salary of each department.
    WITH CTE As (
    SELECT DEPT_ID,
    Salary,
    RANK() OVER(PARTITION BY DEPT_ID ORDER BY Salary DESC) As Rank
    FROM
    Tbl_Dept
    )
    SELECT DEPT_ID, Salary
    FROM CTE
    WHERE Rank = 2

    • @sravankumar967
      @sravankumar967 4 роки тому +7

      Rank() will give wrong output if there are duplicates. Use Dense_rank() instead

  • @SunilGupta-vc8jw
    @SunilGupta-vc8jw 4 роки тому +6

    In this only 19 video, where other 31 video, please share with us

  • @entertainmenthub5066
    @entertainmenthub5066 29 днів тому

    Please keep the DDL and Insert Command also

  • @hasanova6752
    @hasanova6752 6 місяців тому

    thanks for this excellent content
    please add captions

  • @codenchill732
    @codenchill732 3 роки тому

    great explanation

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

    Thank uuu

  • @mohanmohan-mv4ic
    @mohanmohan-mv4ic Рік тому

    Excellent

  • @ujjwalwadera6858
    @ujjwalwadera6858 2 роки тому +1

    U didn't display the name of the employee.. please do that too

  • @trixonx
    @trixonx 5 років тому

    This video is not download friendly. You cannot watch it on 2x speed and it also doesn't play in the background.

  • @TheCodeWhisperer0o0
    @TheCodeWhisperer0o0 5 місяців тому +1

    bro, just put the link of the database in the comment description!!

  • @vinod-s4c
    @vinod-s4c 2 місяці тому

    Pls share remaining videos bro

  • @info5483
    @info5483 2 роки тому +1

    Select department, max(salary) from emp group by department;

  • @SYD_Technologies
    @SYD_Technologies 5 років тому

    Tq sir...Plese upload more videos on SQL quries

    • @3sh66
      @3sh66 5 років тому

      Sure,will do it.plzz do share and subscribe.thank u

  • @livetradding...2894
    @livetradding...2894 Рік тому

    Select department max(salary) from table group by department

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

    its wrong. u need to use row_number over partition by, order by desc. and cte

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

      why you want partition in simple output, he is trying to solve in simple way.

  • @Scienceandfunfanclub
    @Scienceandfunfanclub 5 років тому +1

    Can you tell ???
    two ways to find second highest salary?

    • @priyaranjan1733
      @priyaranjan1733 5 років тому +1

      ua-cam.com/video/-6v7ctxC7yk/v-deo.html

  • @monildand4366
    @monildand4366 5 років тому +1

    How to find name of all the employees with maximum salary department wise?

    • @neeraj2323
      @neeraj2323 3 роки тому +1

      @@KJAKo that's wrong it will not work if other department have same salary as that of first highest salary

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

    How to display emp names also for same data.. 🤔

  • @vishalgaurav8222
    @vishalgaurav8222 5 років тому +1

    if we have 2 seperate tables for employee's department and employee's salary.also the employees' salary as well as their department changes with time(but their time are not related),then how to find the employee's salary according to their department.

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

      select distinct a.emp_depart, b.max(emp_salary) from department as a inner join employee as b on a.emp_id=b.emp_id ;
      As tables are changes with time so it will give the output present in the table at the execution time of the query.

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

      @@Md_Danish433 b.max(emp_salary why max?

  • @AnujKumar-pv7ci
    @AnujKumar-pv7ci 3 роки тому

    What if we have to find the employee's name with the highest salary in each department and there we have multiple employees.

    • @ANUJKUMAR-fu4bk
      @ANUJKUMAR-fu4bk 3 роки тому

      select ename from emp where sal in( select max(sal) from emp group by deptno );

    • @ujjwalwadera6858
      @ujjwalwadera6858 2 роки тому

      @@ANUJKUMAR-fu4bk this won't work...
      Basically He's asking if we have highest salary as let's say 6500 and if 2 people have 6500 salary and then we have to display the names along with dept

    • @BOSS-AI-20
      @BOSS-AI-20 11 місяців тому

      @@ANUJKUMAR-fu4bk This is correct ig

  • @sudhansusingh1122
    @sudhansusingh1122 3 роки тому

    How count(*) ,deptno from employee group by deptno ensures that only employees will be counted.. I mean may. E some other field present can too be counted... Like salary here..

  • @pratyushsingh4721
    @pratyushsingh4721 3 роки тому +1

    Fetching Employee name of each department with max salary.🔥🔥🔥🔥🎇🎇🎆🎆
    SELECT DEPARTMENT_ID, FIRST_NAME, Salary FROM emp WHERE (DEPARTMENT_ID,Salary) IN (SELECT DEPARTMENT_ID, MAX(Salary) FROM emp GROUP BY DEPARTMENT_ID)

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

    done

  • @nuvvulasandeep3093
    @nuvvulasandeep3093 4 роки тому

    if dept id is a primary key in employee table how can we have duplicate dept id's in employee table..

    • @KJAKo
      @KJAKo 4 роки тому

      Here in this example its not primary key. But if it was then group by on deptno example wouldn't hold importance.

  • @sonusharma1039
    @sonusharma1039 3 роки тому

    Is it correct to find ename departmant wise having max salary
    select ename from emp where in (deptno,sal)=(select max(sal),deptno from emp group by deptno);

    • @sheldonvibes
      @sheldonvibes 2 роки тому +1

      SELECT NAME, SALARY, DEPT_ID
      from department
      WHERE (SALARY, DEPT_ID) IN (select MAX(SALARY), DEPT_ID
      from department
      Group by DEPT_ID);

  • @subbaraotanguturu112
    @subbaraotanguturu112 2 роки тому

    Bro I wanted to know that how can we able to write a query to find out the 1st two maximum salaries from each dept....can you please let me know how to solve that.

    • @lilyfullery4779
      @lilyfullery4779 2 роки тому

      if u are using sql server -
      select * from (
      select * , ROW_NUMBER() over(partition by deptid order by salary desc) as row from bemployee) L
      where row = 1 or row =2

  • @vishalsonawane.8905
    @vishalsonawane.8905 7 місяців тому

    DOne

  • @dhanushdhanu5384
    @dhanushdhanu5384 5 років тому

    Can you find the department wise nth max salary

    • @3sh66
      @3sh66 5 років тому +1

      Yes , it will be taught in comming session .stay tuned.thank u

    • @nibibinu
      @nibibinu 4 роки тому +2

      SELECT * FROM ( SELECT e.*, ROW_NUMBER() OVER (ORDER BY salary DESC) rn FROM Employee e ) WHERE rn = N; /*N is the nth highest salary*/

  • @anshuldakshprajapati170
    @anshuldakshprajapati170 5 років тому

    how to find the nth maximum score

    • @3sh66
      @3sh66 5 років тому +1

      Yes , It will be taught in comming sessions.stay tuned for more.thank u

    • @anshuldakshprajapati170
      @anshuldakshprajapati170 5 років тому

      @@3sh66 okk