Real Time SQL Interview Queries|| 2nd Highest Salary in Each Dept

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

КОМЕНТАРІ • 2

  • @KomalTyagi-p4o
    @KomalTyagi-p4o 2 місяці тому +1

    Nice,👌

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

    -- using the common table expressions
    ;with cte_employee as
    (
    select
    e.empid, e.salary,e.deptid, d.deptname,
    ROW_NUMBER() OVER (PARTITION BY e.deptid ORDER BY e.salary DESC) as RNo
    from employee as e inner join department as d
    on e.deptid = d.deptid
    )
    select
    empid, salary, deptid, deptname
    from cte_employee
    where cte_employee.RNo = 2;
    -- using the derived table concept
    select
    empid, deptid, deptname, salary
    from
    (
    select
    emp.empid, emp.salary, emp.deptid, dept.deptname, ROW_NUMBER() OVER (PARTITION BY dept.deptname ORDER BY emp.salary DESC) as RNo
    from employee as emp inner join department as dept
    on emp.deptid = dept.deptid
    ) as temp
    where temp.RNo = 2;
    Thank you