Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
sql query

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
jajularamesh
Currently Banned

New User


Joined: 21 Nov 2005
Posts: 8

PostPosted: Thu Dec 29, 2005 12:32 pm    Post subject: sql query
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    Post subject: Re: sql query
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: 680
Location: NJ

PostPosted: Thu Dec 29, 2005 2:11 pm    Post subject: Select n rows
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: 1452
Location: Chicago, IL

PostPosted: Thu Dec 29, 2005 2:56 pm    Post subject: Re: sql query
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    Post subject: Re: sql query
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    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts HEX value search in a DB2 query maxsubrat DB2 2 Wed Oct 04, 2017 3:04 pm
No new posts Create procedure issues -628 when add... chandraBE DB2 1 Mon Sep 18, 2017 12:16 pm
No new posts Can we limit length in concatenation ... balaji81_k DB2 7 Tue Aug 22, 2017 2:50 am
No new posts Need DB2 query to fetch previous row ! Chandan1993 DB2 10 Sat Jun 03, 2017 10:43 am
No new posts JCL to Set Return code based on DB2 S... vinu78 DB2 17 Mon Mar 13, 2017 9:47 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us