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 8020131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:36:04+00:00 2026-06-04T21:36:04+00:00

I haven’t understood the difference between count(*) and count having an attribute as argument.

  • 0

I haven’t understood the difference between count(*) and count having an attribute as argument.
Here I make an example: there are two relationships:

project (id, name, number);
employee (ssn, name, surname);
working_on (employee_ssn,   project_id);

With employee_ssn which references employee(ssn) and project_id referencing project(id).
Primary keys: project(id), employee(ssn), working_on (employee_ssn, project_id).
Now I have to find for each project, the id of the project, the name and the number of employees working on it.
A correct solution (found on the book) is this one:

select id, name, count (*)
from working_on join project on id=project_id
group by name, id

I have,’t understood why this solution is valid, shouldn’t count (*) count all tuples? Why this way it gets the exact number of employees working on it?
I have written this solution instead:

select id, name, count (employee_ssn)
from working_on join project on id=project_id
group by name, id

Are the solutions equivalent?
And in general, there is a difference using count (*) and count(attribute)? Could you provide an example where these two syntaxes produce different results?

  • 1 1 Answer
  • 0 Views
  • 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-06-04T21:36:05+00:00Added an answer on June 4, 2026 at 9:36 pm

    I figured out two differences till now –

    1. as eric mentioned above, count(col) doesn’t count null values whereas count (*) does.

    2. It behaves differently in a join query.

    Consider a situation where i need to join employee and department table, and i need to find count of employees in each department.

    2.a.

    SELECT     DPT.ID 
    ,          COUNT(*) EMP_COUNT
    FROM       DEPARTMENT DPT
    INNER JOIN EMPLOYEE E
    ON         DPT.ID = E.DEPARTMENT_ID
    GROUP BY   DPT.ID;
    

    In the above query, it will not return those records where employee table count=0.

    2.b.

    SELECT     DPT.ID 
    ,          COUNT(E.DEPARTMENT_ID) EMP_COUNT
    FROM       DEPARTMENT DPT
    INNER JOIN EMPLOYEE E
    ON         DPT.ID = E.DEPARTMENT_ID
    GROUP BY   DPT.ID;
    

    If you use count(E.DEPARTMENT_ID) instead of count(*), this time it will give records even when employee table count=0.

    2.c.

    SELECT     DPT.ID 
    ,          COUNT(DPT.ID) EMP_COUNT
    FROM       DEPARTMENT DPT
    INNER JOIN EMPLOYEE E
    ON         DPT.ID = E.DEPARTMENT_ID
    GROUP BY   DPT.ID;
    

    This is the interesting part, when you use COUNT(DPT.ID), since DPT.ID is column of department, here again records where employee table count=0 will NOT be considered.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I haven't found an appropriate example for this, so I ask here: My project
Haven't seen anything about it here but it seems to solve one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I haven't been able to solve this prolog exercise. I was hoping someone here
Haven't fired up reflector to look at the difference but would one expect to
I haven't completely understood, how to use sigprocmask() . Particularly, how the set and
Haven't found in docs. Does java ResultSet supports query arguments,like jdbcTemplate? For example, something
I haven't found a straight answer to how to make an animation in a
I haven't found a definite answer to this yet. Lots of apps let you
I haven't problem for change color of main title of navigation on a normal

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.