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 How to force a select query to abend neo4u DB2 5 Mon Apr 23, 2018 1:36 pm
No new posts Query to compare 2 values of 1 column... Poha Eater DB2 13 Fri Mar 09, 2018 10:45 am
No new posts How to replace the below query? pkmurali DB2 12 Tue Feb 27, 2018 9:51 pm
No new posts Need Guidance regarding UPDATE query Poha Eater DB2 3 Mon Feb 12, 2018 11:35 pm
No new posts Query to skip Max time-stamp and fetc... vickey_dw DB2 8 Sun Jan 28, 2018 1:09 am

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