8

I understand how count(*) in SQL when addressing one table but how does it work on inner joins?

e.g.

SELECT branch, staffNo, Count(*)
FROM Staff s, Properties p 
WHERE s.staffNo = p.staffNo
GROUP BY s.staffNo, p.staffNo

staff contains staffNo staffName

properties contains property management details (i.e. which staff manages which property)

This returns the number of properties managed by staff, but how does the count work? As in how does it know what to count?

1
  • Detail: it's not an outer join. It's an (implicit) inner join. Commented May 26, 2011 at 12:39

5 Answers 5

9

It's an aggregate function - as such it's managed by your group by clause - each row will correspond to a unique grouping (i.e. staffNo) and Count(*) will return the number of records in the join that match that grouping.

So for example:

 SELECT branch, grade, Count(*)
 FROM Staff s, Properties p 
 WHERE s.staffNo = p.staffNo
 GROUP BY branch, grade

would return the number of staff members of a given grade at each branch.

 SELECT branch, Count(*)
 FROM Staff s, Properties p 
 WHERE s.staffNo = p.staffNo
 GROUP BY branch

would return the total number of staff members at each branch

 SELECT grade, Count(*)
 FROM Staff s, Properties p 
 WHERE s.staffNo = p.staffNo
 GROUP BY grade

would return the total number of staff at each grade

Sign up to request clarification or add additional context in comments.

4 Comments

well blow me down - I hit submit without finishing my answer
ta :) I got there in the end!
so is it essentially counting however many p.staffs = s.staffs?
No - Run the following query: SELECT branch, s.staffNo FROM Staff s, Properties p WHERE s.staffNo = p.staffNo. This shows the unaggregated resultset. You may have multiple rows with the same staffNo. This will be because in one of the tables there are multiple rows with this staffNo. Adding the COunt() and the above Group clause will reduce the resultset to rows grouped by staffNo. Where previously multiple entries existed, now this will be reflected in the Count() result.
4

The aggregate function (whether it's count(), sum(), avg(), etc.) is computed on the rows in each group: that group is then collapsed/summarized/aggregated to a single row according to the select-list defined in the query.

The conceptual model for the execution of a select query is this:

  1. Compute the cartesian product of all tables references in the FROM clause (as if a full join were being performed.
  2. Apply the join criteria.
  3. Filter according to the criteria defined in the where clause.
  4. Partitition into groups, based on the criteria defined in the group by clause.
  5. Reduce each group to a single row, computing the values of each aggregate function on the rows in that group.
  6. Filter according to the criteria defined in the having clause
  7. Sort according to the criteria defined in the order by clause

This conceptual model omits dealing with any compute or compute...by clauses.

Not this this is not actually how anything but a very naive SQL engine would actually execute a query, but the results should be identical to what you'd [eventually] get if you did it this way.

Comments

3

Your query is invalid.

You have an ambiguous column name staffno.

You are selecting branch but not grouping by it - prepare for a Syntax error (everything but MySQL) or random branches to be selected for you (MySQL).

I think what you want to know, though, is that it will return a count for each "set" of your grouped-by fields, so for each combination of s.staffno, p.staffno how many rows belong in that set.

Comments

1

count (*) simply counts the number of rows in the query or the group by.

In your query, it will print the number of rows by staffNo. (It is redundant to have s.staffNo, p.staffNo; either will suffice).

Comments

0

It counts the number of rows for each distinct StaffNo in the cartesian product.

Also, you should group by Branch, StaffNo.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.