Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2002
    Location
    Cape Town, South Africa
    Posts
    253

    Question Unanswered: count(*) vs count(column)

    Hi all,

    I have seen both of these count() statements used in an oracle example and I was wondering what the difference?

    Here is the exact query I was looking at:
    Code:
      SELECT s.prod_id, s.time_id,  
             COUNT(*) AS count_grp,  
      SUM(s.amount_sold) AS sum_dollar_sales,  
            COUNT(s.amount_sold) AS count_dollar_sales,  
      SUM(s.quantity_sold) AS sum_quantity_sales,  
            COUNT(s.quantity_sold) AS count_quantity_sales 
      FROM sales s
      GROUP BY s.prod_id, s.time_id;
    So what is the difference in COUNT(*) vs. COUNT(s.quantity_sold) as above?

    Thanks!

  2. #2
    Join Date
    Nov 2003
    Posts
    2,934
    Provided Answers: 12
    So what is the difference in COUNT(*) vs. COUNT(s.quantity_sold) as above?
    count(*) will count all rows whereas count(column_name) only counts rows "where column_name is not null". So
    Code:
    SELECT count(column_name)
    FROM theTable
    Is equivalent to
    Code:
    SELECT count(*) 
    FROM theTable
    WHERE column_name IS NOT NULL

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002

  4. #4
    Join Date
    Oct 2002
    Location
    Cape Town, South Africa
    Posts
    253
    Thanks, that makes more sense now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •