Job Seekers   Employers
dbapool: Authors | Submissions | Contact Us
   Forgot password? | Sign up
  Home   Discussion Forum   Articles   Interview Questions   FAQs   Scripts   Rewards   Analyzer   White Papers   Blog   Certification   Downloads   Tools
 

Topic: SQL*Plus and PL/SQL >> SQL Query:-Help

Disclaimer: The purpose of all dbapool forums including OCP and Other Oracle Certification forums is to help each other with specific issues but not to share dump and copyrighted exam content, materials or intellectual property. You may review the entire Oracle Certification Program Candidate Agreement online Here.



  


 Title: SQL Query:-Help
 RAJNISH  Posted: Sep 01, 2008 07:08:27 AM

 Total Post: 92
 Joined: Jan, 2007






 Hi ALl,
select deptno,count(*) from dept
group by deptno

above showing me output like
deptno count
10 1
20 1
30 1
40 1

but i want show only first three recds i.e 10,20,30 WITHOUT USING WHERE CLAUSE IN THIS QUERY?

--rajnish

 vino
Posted: Sep 01, 2008 07:20:44 AM  

 Total Post: 357
 Joined: Aug, 2008






 
using LIMIT function will satisfy your need..

 RAJNISH
Posted: Sep 01, 2008 07:22:11 AM  

 Total Post: 92
 Joined: Jan, 2007






 
Hi Vino,
please give me your reply with some example i am very happy to see your instant reply.

 RAJNISH
Posted: Sep 01, 2008 07:30:20 AM  

 Total Post: 92
 Joined: Jan, 2007






 
HI,
LIMIT function is not working with this query.
what should i do?

 vino
Posted: Sep 01, 2008 07:33:47 AM  

 Total Post: 357
 Joined: Aug, 2008






 
Here is an example,

SELECT * FROM emp LIMIT 0,3;

The above query will display only first three records in that table..

Hope this helps.





 RAJNISH
Posted: Sep 01, 2008 07:39:19 AM  

 Total Post: 92
 Joined: Jan, 2007






 
hi vino but i want use limit in above query but after goroup clause its not working.

 vino
Posted: Sep 01, 2008 07:45:27 AM  

 Total Post: 357
 Joined: Aug, 2008






 
Try this,
>select ... from tablename group by deptno desc limit 0,3;



 RAJNISH
Posted: Sep 01, 2008 07:48:08 AM  

 Total Post: 92
 Joined: Jan, 2007






 
Hi Vino,
i am using with my query see below.
select deptno,count(*) from dept
group by deptno
desc limit 0,3

ITS NOT WORKING DEAR?

 vino
Posted: Sep 01, 2008 08:05:38 AM  

 Total Post: 357
 Joined: Aug, 2008






 
What the error you are getting, paste it..

 vino
Posted: Sep 01, 2008 09:34:37 AM  

 Total Post: 357
 Joined: Aug, 2008






 
Try this,

select deptno,count(*) from dept order by deptno desc limit 3;

bcoz, Order by is recommended that if we gonna use limit function.

 vino
Posted: Sep 01, 2008 09:35:00 AM  

 Total Post: 357
 Joined: Aug, 2008






 
Try this,

select deptno,count(*) from dept order by deptno desc limit 3;

bcoz, Order by is recommended that if we gonna use limit function.

 RAJNISH
Posted: Sep 01, 2008 10:31:47 AM  

 Total Post: 92
 Joined: Jan, 2007






 
HI VINO,
STILL ITS NOT WORKING DEAR YOU JUST TRY THIS QRY FROM YOUR SIDE.

select deptno,count(*) from dept
group by deptno
order by deptno desc limit 3

 shake
Posted: Sep 02, 2008 01:34:09 AM  

 Total Post: 85
 Joined: Jun, 2008






 
LIMIT clause works fine in MySQL and PostgreSQL. This is similar to TOP clause in Microsoft SQL Server. OFFSET keyword is used in PostgreSQL to provide the offset(no. of rows) value.

But in Oracle, we use ROWNUM along with WHERE clause to retrieve the records of a particular range.

SELECT * FROM emp WHERE ROWNUM <= 3; -> to get the first 3 records.

SELECT * FROM emp WHERE ROWNUM <= 8
MINUS
SELECT * FROM emp WHERE ROWNUM < 3; -> to get 3-8 records.


 RAJNISH
Posted: Sep 02, 2008 05:11:42 AM  

 Total Post: 92
 Joined: Jan, 2007






 
Hi Vino / Shake
finally i have made it...

SELECT * FROM (SELECT DEPTno, COUNT(*) cnt FROM DEPT
GROUP BY deptno ORDER BY cnt DESC)
WHERE ROWNUM < 4

it will show you first 3 recds.

 vino
Posted: Sep 03, 2008 08:14:16 AM  

 Total Post: 357
 Joined: Aug, 2008






 
Hi Rajinish,

Thanks for your reply, I too come across rumnum just now.. Would like to know if you have any queries..

 Time Zone: EDT

  




Forum Rules & Description


Who Can Read The Forum? Any registered user or guest
Who Can Post New Topics? Any registered user
Who Can Post Replies? Any registered user




 








Get FREE Magazines

Top 10 Forum User

  Murtuja Khokhar857
  Mohammed Taj746
  positive fanatic483
  Jayanta Sur479
  Vigyan Kaushik386
  Vinoth Kumar357
  Gopu Gopi340
  Gitesh Trivedi322
  neeraj sharma258
  Ramesh Jois246






oracle Mag



  About Us Advertise Terms of Use Privacy Newsletters Contact Us    

Home   Discussion Forum   FAQs  Articles  Jobs   Newsletters  Directory  Downloads 

Our Premium Sponsor
Confio Software