Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have 2 tables
Customer1
Id Dept Design
1 Hr AA
2 Finance BB
3 Finance CC
4 Elect DD

Customer2

Id Salary
1 10000
2 20000
3 30000
4 40000

What I have tried:

Please give me the solution.I have to use join or union?
Posted
Updated 24-May-19 1:13am

JOIN.
SQL
SELECT b.*, a.Salary
FROM (
    SELECT *, ROW_NUMBER() OVER (ORDER BY Salary DESC) AS RowNum
    FROM Customer2   
) AS a
JOIN Customer1 b ON a.ID = b.ID
WHERE a.RowNum = 2
But ... that's a very poor DB design.
 
Share this answer
 
SQL
;WITH CTE AS(
SELECT E.*
      ,D.Salary
      ,ROW_NUMBER()OVER(PARTITION BY E.Dept ORDER BY D.Salary DESC) AS RN
  FROM Customer1 AS E INNER JOIN Customer2 AS D
                                      ON (E.ID=D.ID) 
           )

SELECT ID,Dept,Design,Salary FROM CTE WHERE RN=2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900