2

What is the difference between following SQL Server 2008 statements?

SELECT COUNT(*) FROM dbo.Regular_Report

SELECT COUNT(0) FROM dbo.Regular_Report

SELECT COUNT(1) FROM dbo.Regular_Report

SELECT COUNT(100) FROM dbo.Regular_Report

SELECT COUNT(ID) FROM dbo.Regular_Report
halfer
  • 19,824
  • 17
  • 99
  • 186
Mahesh
  • 1,465
  • 4
  • 19
  • 23

2 Answers2

9

The first four are all identical - they count the total number of rows.

The last one counts the number of rows where ID is not null.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
8

count(id) will count the non-null values in the id field.

All the other will count the number of records.

(The ones with literal numbers instead of * will count all the records where that value is not null. Probably the query planner will recognise that the literal value will never be null for any record, and do the same as count(*) instead of actually counting the non-null values.)

Guffa
  • 687,336
  • 108
  • 737
  • 1,005