Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups 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 Join in SQL Query vickey_dw DB2 2 Tue Jan 17, 2017 12:15 am
This topic is locked: you cannot edit posts or make replies. MQ setup query mohitsaini Java & MQSeries 4 Sun Jan 08, 2017 1:53 am
No new posts DB2 query Using Recursion, Converting... smilewithashu2 DB2 1 Tue Jan 03, 2017 12:50 pm
No new posts SQL query to run through list of valu... Ni3-db2 DB2 14 Wed Dec 14, 2016 9:52 am
This topic is locked: you cannot edit posts or make replies. Db2 SQL query to convert rows into co... subratarec DB2 5 Thu Nov 17, 2016 4:51 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us