Geeks Talk

Prepare for your Next Interview




Select using having clause

This is a discussion on Select using having clause within the SQL forums, part of the Databases category; Hi, I'm trying to execute a simple query that returns the minimum value from a column and the count of it: SELECT c_id, count(c_id) FROM customer having c_id = ...


Go Back   Geeks Talk > Databases > SQL

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 03-03-2008
Junior Member
 
Join Date: Feb 2008
Location: New Delhi
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
mandy4ever is on a distinguished road
Select using having clause

Hi,

I'm trying to execute a simple query that returns the minimum value from a column and the count of it:

SELECT c_id, count(c_id) FROM customer
having c_id = min(c_id)
group by c_id

But instead of returning a single row (the minimum value) this returns ALL the c_id's in this table along with their count. can someone please tell me where I'm going wrong.

Thanks in advance.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-03-2008
Junior Member
 
Join Date: Feb 2008
Location: Hyderabad
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
nandini29 is on a distinguished road
Re: Select using having clause

hi

you are using wrong order in query.
first write group by
second having clause

after that run the query you will get right answer.

nandini
Reply With Quote
  #3 (permalink)  
Old 03-03-2008
Expert Member
 
Join Date: Sep 2007
Posts: 754
Thanks: 22
Thanked 64 Times in 63 Posts
krishnaindia2007 is on a distinguished road
Re: Select using having clause

Quote:
Originally Posted by mandy4ever View Post
Hi,

I'm trying to execute a simple query that returns the minimum value from a column and the count of it:

SELECT c_id, count(c_id) FROM customer
having c_id = min(c_id)
group by c_id

But instead of returning a single row (the minimum value) this returns ALL the c_id's in this table along with their count. can someone please tell me where I'm going wrong.

Thanks in advance.

Use the following query

select c_id,count(c_id) from customer
where c_id = (select min(c_id) from customer)
group by c_id

The mistake in your query is It executes group by first then having clause.
Group by C_id identifies distinct C_id's from your table.
Then having clause finds minimum C_id in that group which is c_id itself.
If c_id is a primary key then Your query will display all c_id's in your table and its count as 1.

Regards
Krishna
Reply With Quote
  #4 (permalink)  
Old 03-05-2008
Moderator
 
Join Date: Jun 2007
Location: Bangalore,India
Posts: 1,432
Thanks: 8
Thanked 126 Times in 113 Posts
debasisdas will become famous soon enoughdebasisdas will become famous soon enough
Re: Select using having clause

You have wrongly specified the order of execution of the clauses.
Reply With Quote
  #5 (permalink)  
Old 03-05-2008
Expert Member
 
Join Date: Apr 2007
Location: Bangalore
Posts: 447
Thanks: 20
Thanked 54 Times in 54 Posts
susarlasireesha is on a distinguished road
Re: Select using having clause

Try this query also
Code:
select c_id,cnt from 
(SELECT c_id, count(c_id) cnt FROM customer 
group by c_id 
having c_id = min(c_id)) 
where rownum<=1
Quote:
Originally Posted by mandy4ever View Post
Hi,

I'm trying to execute a simple query that returns the minimum value from a column and the count of it:

SELECT c_id, count(c_id) FROM customer
having c_id = min(c_id)
group by c_id

But instead of returning a single row (the minimum value) this returns ALL the c_id's in this table along with their count. can someone please tell me where I'm going wrong.

Thanks in advance.
Reply With Quote
Reply

  Geeks Talk > Databases > SQL


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
GROUP_BY and HAVING clause in SQL nancyphilips SQL 5 09-09-2008 12:39 AM
For update clause babi_geek Oracle 3 03-19-2008 08:26 AM
return clause in cursor Barbie Oracle 4 01-31-2007 05:46 AM
Presence of Clause nehalshah MainFrame 1 10-09-2006 04:01 PM
Occurs clause in COBOL timmy MainFrame 2 07-29-2006 04:23 PM


All times are GMT -4. The time now is 09:35 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved