Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have trouble naming the COUNT() column from SQL queries and will swap between various variants

  • _Count
  • [Count] (sql, or "count" or backticks for MySQL etc)
  • C
  • Cnt
  • CountSomething (where "something" is the field being counted, or "CountAll")
  • NoOfRows
  • RowCount
  • etc

Has anyone come up with any name that you are happy with and always use without hesitation?
This is bothering me because after joining SO just recently, my answers have shown this tendency of flip-flopping with no consistency.
I need to get this sorted.

Please help.

(While we're at it, what do you use for SUM etc?)

Note: Before you close this question, consider that this one was not:

What's the best name for a non-mutating “add” method on an immutable collection?

share|improve this question

migrated from stackoverflow.com Jan 31 '11 at 23:35

This question came from our site for professional and enthusiast programmers.

3  
@cyberkiwi: if we tell you the correct name for the COUNT(*) column, do you promise you will be using it? –  Quassnoi Jan 31 '11 at 22:12
1  
@omg "End users can not mark questions wiki anymore" (The CW checkbox only exists on answers these days) –  Martin Jan 31 '11 at 22:24
2  
@cyberkiwi: there is a street in London, GROUP cnt lane or something like this. Was inhabited by SQL developers in XIV century, hence the name. –  Quassnoi Jan 31 '11 at 22:27
1  
@Martin: Thx, I never read meta –  OMG Ponies Jan 31 '11 at 22:37
1  
This may be the best comment thread I've read all morning after all. –  BoltClock Jan 31 '11 at 23:18
show 11 more comments

4 Answers

up vote 10 down vote accepted

Name it based on what it means - what are you counting, and why?

SELECT COUNT(CustomerId) as CustomerCount FROM Customer

SELECT SUM(Price) as TotalPrice FROM InvoiceLine WHERE InvoiceId = @InvoiceId

Using the same name consistently just because it's a SUM/COUNT operation makes about as much sense as having all your queries look like:

SELECT Id as Column0, Forenames as Column1, Surname as Column2 FROM Customer

the meaning and reasoning behind the use of the column is more important than consistency from one query to the next, IMHO.

share|improve this answer
    
+1 for what it means. Unfortunately I can't downvote my own answer =P –  Josh Smeaton Jan 31 '11 at 21:48
1  
Because "customer count" and "total price" make sense as English expressions where "price sum" doesn't... :) –  Dylan Beattie Jan 31 '11 at 22:06
    
@Josh: You can delete it. –  BoltClock Jan 31 '11 at 22:13
    
@dylan sorry I said Josh I meant "Dylan" of course! –  cyberkiwi Jan 31 '11 at 22:13
    
@Dylan: price sum hardly makes sense in Russian expressions too. Why would you want to sum the prices? –  Quassnoi Jan 31 '11 at 22:15
add comment

I just use rows. The fact that an integer is present as the data tells us its a number already. Nothing I hate more than iRowCount or some such!

share|improve this answer
    
I never thought of just rows instead of rowcount. +1 –  cyberkiwi Jan 31 '11 at 21:53
    
@cyberkiwi: ROWS is a reserved word at least in Oracle. –  Quassnoi Jan 31 '11 at 22:23
    
@Quassnoi good point. Maybe the question shouldn't be "agnostic". I should ask on english.se as to what the antonym is. –  cyberkiwi Jan 31 '11 at 22:39
add comment

I just use "Counter" since it's not a command (or function, etc.) and it's easy.

share|improve this answer
    
Doesn't counter imply something like a sequential row number that will change per record? I would have thought it fits row_number() rather than count(), but could consider it. –  cyberkiwi Jan 31 '11 at 23:03
    
It doesn't to imply that to me if it is a summary. But I use it for a sequential count too - it doesn't confuse me. –  Mark SQLDev Jan 31 '11 at 23:19
add comment

"If there are more than two instances of COUNT/SUM/etc in the query, how do you distinguish between them?"

When you contemplate that question, it becomes obvious that:

  • using a naming convention consistently matters
  • the naming convention should be informational enough to distinguish from other uses of the same function

With the advent of intellisense/etc in SQL IDEs like SSMS and PLSQL Developer, I tend to prefix my aggregate derived columns. IE: numUsers, or cntUsers This makes it easier to key to them without sifting through others.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.