Write a query to show the highest and second highest salary of each department from the Employees table.. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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
Avinash Tiwari
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
Avinash Tiwari
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