IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

sql query


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
jajularamesh

New User


Joined: 21 Nov 2005
Posts: 8

PostPosted: Thu Dec 29, 2005 12:32 pm
Reply with quote

i want to select the top n employees (based on salary) in the organization
Back to top
View user's profile Send private message
nikyojin

New User


Joined: 05 Oct 2005
Posts: 94

PostPosted: Thu Dec 29, 2005 1:08 pm
Reply with quote

Query :-
Code:
Select * from tablename
             Order By Sal Asc/Desc
             Fetch first n rows only;

Choose the values Asc/Desc as per ur requirements and do include required details prior to posting ur queries.
Back to top
View user's profile Send private message
khamarutheen

Active Member


Joined: 23 Aug 2005
Posts: 677
Location: NJ

PostPosted: Thu Dec 29, 2005 2:11 pm
Reply with quote

Hi frnd,
Code:

  SELECT ENO,ENAME,SALARY
  FROM EMP
  FETCH FIRST/LAST N ROWS ONLY


As per nikyojin u can also use order by clause as per ur need.....
Back to top
View user's profile Send private message
priyesh.agrawal

Senior Member


Joined: 28 Mar 2005
Posts: 1448
Location: Chicago, IL

PostPosted: Thu Dec 29, 2005 2:56 pm
Reply with quote

Query given by nikyojin is correct, with out using ORDER BY its not always possible to receive values for TOP employees.

With out ORDER BY clause it'll simply return mentioned FIRST/LAST rows.

Regards,

Priyesh.
Back to top
View user's profile Send private message
nrsmca

New User


Joined: 25 May 2005
Posts: 14

PostPosted: Mon Jan 09, 2006 7:12 pm
Reply with quote

N-th max. list:-

1.Select * from tablename A where Num_var > (Select count(*) from ablename B where B.Salary > A.Salary);

We should 2 (>) for ur query...

or

2.Select * from tablename C where C.salary >=(
Select A.salary from tablename A where Num_var = (Select count(*) from ablename B where B.Salary >= A.Salary));

or

3.Select * from Tablename where row_num <= Num_var order by salary;

N-th min. list:-

1.Select * from tablename A where Num_var < (Select count(*) from ablename B where B.Salary < A.Salary);

Plz. Modify (2) & (3) ... as same as the (1).

Thanks,
Rajasekaran.N
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
No new posts Issue with EXEC CICS QUERY SECURITY c... CICS 6
Search our Forums:

Back to Top