0

Write a query to show the highest and second highest salary of each department from the Employees table..

27th Jul 2022, 4:52 AM
Dumb Coder đŸ€Ą
Dumb Coder đŸ€Ą - avatar
3 Answers
+ 2
Attempts?
27th Jul 2022, 6:02 AM
AÍąJ
AÍąJ - avatar
0
AÍąJ SELECT Department , MAX(salary) AS 'Highest and Second Maximum Salary' FROM employees GROUP BY Department UNION ALL SELECT Department , MAX (salary) FROM Employees WHERE salary < (SELECT MAX(SALARY) FROM Employees ) Group By Department ORDER BY Department
27th Jul 2022, 9:19 AM
Dumb Coder đŸ€Ą
Dumb Coder đŸ€Ą - avatar
0
SELECT departmentid, NAME, salary FROM ( SELECT departmentid, NAME, salary, Dense_rank()OVER (partition BY departmentid ORDER BY salary DESC) AS Rank, Count(1)OVER(partition BY departmentid) AS cnt FROM employees )t WHERE t.rank = 2 OR ( t.rank = 1 AND cnt = 1 ) Sometimes SQL queries get very complicated by joins, Group By clauses, and other referential dependencies, So those Types of queries can be simplified to proxy data or virtual data which simplifies the queries. Refer to this article to get more information: https://www.scaler.com/topics/views-in-dbms/
19th Oct 2022, 9:34 AM
sarthak jain
sarthak jain - avatar