|
|
| Author |
Message |
chiru_sh
New User
Joined: 21 Feb 2008 Posts: 14 Location: Mumbai
|
|
|
|
Hi,
I am new to DB2, so I was having a very basic Query. Can anybody help me on this.
Suppose, there are two tables.
TABLE: EMP
Emp No. Location
111 Mumbai
222 New York
333 Tokyo
TABLE: SALARY
Emp No. Salary
111 60000
222 80000
333 70000
I want to write a query where I can get all the details of employee with MAXIMUM SALARY.
i.e I want record
Emp No. Salary Location
222 80000 New York
Is there any approach where we can join the EMP & SALARY table and use MAX function.
Thanks in advance. |
|
| Back to top |
|
 |
References
|
|
 |
abiyn
New User
Joined: 25 Mar 2008 Posts: 12 Location: Chennai
|
|
|
|
chiru_sh,
U can use Sub-query for this.
Select E.empid,E.location,S.salary from EMP e, Salary s where E.empid=S.empid and
S.salary=(select max(salary) from salary)
you can also Groupby function.
Regards
Abi |
|
| Back to top |
|
 |
chiru_sh
New User
Joined: 21 Feb 2008 Posts: 14 Location: Mumbai
|
|
|
|
Thanks for the reply.
One of the approach I could think of was:
SELECT E.EMPID,E.LOCATION,S.SALARY
FROM EMP E,SALARY S
WHERE E.EMPID=S.EMPID
ORDER BY S.SALARY DESC
FETCH 1 ROWS ONLY
Can we not get the row details where we are getting MAX VALUE like
For eg.
Approach 2 : SELECT E.EMPID,E.LOCATION,MAX(S.SALARY)
FROM EMP E,SALARY S
WHERE E.EMPID=S.EMPID
Will the above Approach 2 work. I am not sure. Please help me what will happen in above case. |
|
| Back to top |
|
 |
Richa Jain
New User
Joined: 18 Mar 2008 Posts: 34 Location: Chennai
|
|
|
|
Try this...
| Code: |
SELECT EMP.EMP_NO, EMP.LOCATION, SALARY.SALARY FROM EMP INNER JOIN SALARY ON EMP.EMP_NO=SALARY.EMP_NO WHERE SALARY.SALARY=(SELECT MAX(SALARY) FROM SALARY); |
|
|
| Back to top |
|
 |
Gnanas SNG
Senior Member
Joined: 06 Sep 2007 Posts: 446 Location: India
|
|
|
|
Probably you well get all the rows...
How will MAX fn be in effect? |
|
| Back to top |
|
 |
Gnanas SNG
Senior Member
Joined: 06 Sep 2007 Posts: 446 Location: India
|
|
|
|
| What Richa Jain suggested is as same as Abi's query... |
|
| Back to top |
|
 |
Richa Jain
New User
Joined: 18 Mar 2008 Posts: 34 Location: Chennai
|
|
|
|
Gnanas
| Quote: |
What Richa Jain suggested is as same as Abi's query...
Since chiru_sh asked for JOIN and MAX function so i suggested the same
|
|
|
| Back to top |
|
 |
abiyn
New User
Joined: 25 Mar 2008 Posts: 12 Location: Chennai
|
|
|
|
chiru_sh,
Approach 1 will work.
Approach 2 will not work. Because Group by function should be used when we select fields along with the aggregate function.
Correct me if i am wrong.
Thank You |
|
| Back to top |
|
 |
Richa Jain
New User
Joined: 18 Mar 2008 Posts: 34 Location: Chennai
|
|
|
|
abiyn
You are correct. Aggregate functions cannot work without Group by clause. |
|
| Back to top |
|
 |
|
|