Results 1 to 5 of 5

Thread: Select using having clause

  1. #1
    Junior Member
    Join Date
    Feb 2008
    Answers
    1

    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.


  2. #2
    Junior Member
    Join Date
    Feb 2008
    Answers
    1

    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


  3. #3
    Expert Member
    Join Date
    Sep 2007
    Answers
    697

    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


  4. #4
    Moderator
    Join Date
    Jun 2007
    Answers
    2,074

    Re: Select using having clause

    You have wrongly specified the order of execution of the clauses.


  5. #5
    Expert Member
    Join Date
    Apr 2007
    Answers
    500

    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.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact