Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 753821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:56:28+00:00 2026-05-14T14:56:28+00:00

I often find these three variants: SELECT COUNT(*) FROM Foo; SELECT COUNT(1) FROM Foo;

  • 0

I often find these three variants:

SELECT COUNT(*) FROM Foo;
SELECT COUNT(1) FROM Foo;
SELECT COUNT(PrimaryKey) FROM Foo;

As far as I can see, they all do the same thing, and I find myself using the three in my codebase. However, I don’t like to do the same thing different ways. To which one should I stick? Is any one of them better than the two others?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:56:29+00:00Added an answer on May 14, 2026 at 2:56 pm

    Bottom Line

    Use either COUNT(field) or COUNT(*), and stick with it consistently, and if your database allows COUNT(tableHere) or COUNT(tableHere.*), use that.

    In short, don’t use COUNT(1) for anything. It’s a one-trick pony, which rarely does what you want, and in those rare cases is equivalent to count(*)

    Use count(*) for counting

    Use * for all your queries that need to count everything, even for joins, use *

    SELECT boss.boss_id, COUNT(subordinate.*)
    FROM boss
    LEFT JOIN subordinate on subordinate.boss_id = boss.boss_id
    GROUP BY boss.id
    

    But don’t use COUNT(*) for LEFT joins, as that will return 1 even if the subordinate table doesn’t match anything from parent table

    SELECT boss.boss_id, COUNT(*)
    FROM boss
    LEFT JOIN subordinate on subordinate.boss_id = boss.boss_id
    GROUP BY boss.id
    

    Don’t be fooled by those advising that when using * in COUNT, it fetches entire row from your table, saying that * is slow. The * on SELECT COUNT(*) and SELECT * has no bearing to each other, they are entirely different thing, they just share a common token, i.e. *.

    An alternate syntax

    In fact, if it is not permitted to name a field as same as its table name, RDBMS language designer could give COUNT(tableNameHere) the same semantics as COUNT(*). Example:

    For counting rows we could have this:

    SELECT COUNT(emp) FROM emp
    

    And they could make it simpler:

    SELECT COUNT() FROM emp
    

    And for LEFT JOINs, we could have this:

    SELECT boss.boss_id, COUNT(subordinate)
    FROM boss
    LEFT JOIN subordinate on subordinate.boss_id = boss.boss_id
    GROUP BY boss.id
    

    But they cannot do that (COUNT(tableNameHere)) since SQL standard permits naming a field with the same name as its table name:

    CREATE TABLE fruit -- ORM-friendly name
    (
    fruit_id int NOT NULL,
    fruit varchar(50), /* same name as table name, 
                    and let's say, someone forgot to put NOT NULL */
    shape varchar(50) NOT NULL,
    color varchar(50) NOT NULL
    )
    

    Counting with null

    And also, it is not a good practice to make a field nullable if its name matches the table name. Say you have values ‘Banana’, ‘Apple’, NULL, ‘Pears’ on fruit field. This will not count all rows, it will only yield 3, not 4

    SELECT count(fruit) FROM fruit
    

    Though some RDBMS do that sort of principle (for counting the table’s rows, it accepts table name as COUNT’s parameter), this will work in Postgresql (if there is no subordinate field in any of the two tables below, i.e. as long as there is no name conflict between field name and table name):

    SELECT boss.boss_id, COUNT(subordinate)
    FROM boss
    LEFT JOIN subordinate on subordinate.boss_id = boss.boss_id
    GROUP BY boss.id
    

    But that could cause confusion later if we will add a subordinate field in the table, as it will count the field(which could be nullable), not the table rows.

    So to be on the safe side, use:

    SELECT boss.boss_id, COUNT(subordinate.*)
    FROM boss
    LEFT JOIN subordinate on subordinate.boss_id = boss.boss_id
    GROUP BY boss.id
    

    count(1): The one-trick pony

    In particular to COUNT(1), it is a one-trick pony, it works well only on one table query:

    SELECT COUNT(1) FROM tbl
    

    But when you use joins, that trick won’t work on multi-table queries without its semantics being confused, and in particular you cannot write:

    -- count the subordinates that belongs to boss
    SELECT boss.boss_id, COUNT(subordinate.1)
    FROM boss
    LEFT JOIN subordinate on subordinate.boss_id = boss.boss_id
    GROUP BY boss.id
    

    So what’s the meaning of COUNT(1) here?

    SELECT boss.boss_id, COUNT(1)
    FROM boss
    LEFT JOIN subordinate on subordinate.boss_id = boss.boss_id
    GROUP BY boss.id
    

    Is it this…?

    -- counting all the subordinates only
    SELECT boss.boss_id, COUNT(subordinate.boss_id)
    FROM boss
    LEFT JOIN subordinate on subordinate.boss_id = boss.boss_id
    GROUP BY boss.id
    

    Or this…?

    -- or is that COUNT(1) will also count 1 for boss regardless if boss has a subordinate
    SELECT boss.boss_id, COUNT(*)
    FROM boss
    LEFT JOIN subordinate on subordinate.boss_id = boss.boss_id
    GROUP BY boss.id
    

    By careful thought, you can infer that COUNT(1) is the same as COUNT(*), regardless of type of join. But for LEFT JOINs result, we cannot mold COUNT(1) to work as: COUNT(subordinate.boss_id), COUNT(subordinate.*)

    So just use either of the following:

    -- count the subordinates that belongs to boss
    SELECT boss.boss_id, COUNT(subordinate.boss_id)
    FROM boss
    LEFT JOIN subordinate on subordinate.boss_id = boss.boss_id
    GROUP BY boss.id
    

    Works on Postgresql, it’s clear that you want to count the cardinality of the set

    -- count the subordinates that belongs to boss
    SELECT boss.boss_id, COUNT(subordinate.*)
    FROM boss
    LEFT JOIN subordinate on subordinate.boss_id = boss.boss_id
    GROUP BY boss.id
    

    Another way to count the cardinality of the set, very English-like (just don’t make a column with a name same as its table name) : http://www.sqlfiddle.com/#!1/98515/7

    select boss.boss_name, count(subordinate)
    from boss
    left join subordinate on subordinate.boss_code = boss.boss_code
    group by boss.boss_name
    

    You cannot do this: http://www.sqlfiddle.com/#!1/98515/8

    select boss.boss_name, count(subordinate.1)
    from boss
    left join subordinate on subordinate.boss_code = boss.boss_code
    group by boss.boss_name
    

    You can do this, but this produces wrong result: http://www.sqlfiddle.com/#!1/98515/9

    select boss.boss_name, count(1)
    from boss
    left join subordinate on subordinate.boss_code = boss.boss_code
    group by boss.boss_name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often find linq being problematic when working with custom collection object. They are
I often find myself doing these sort of ternary constructs which seem to lack
I often find myself wanting to do it. It can be very useful when
Quite often I find myself writing code like this: Dim oRenewalOrder = (From c
I often find myself wanting to debug CSS layout issues that involve DOM changes
I often find myself needing reference to an object that is several objects away,
I often find myself storing data in a mysql database, and then wanting to
I often find myself writing one off queries to either answer someone's question or
I often find it confusing as to when it is appropriate to use: rs.Close
I often find myself with a list of disconnected Linq2Sql objects or keys that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.