You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Returns the number of items in a group. COUNT works like the [COUNT_BIG](../../t-sql/functions/count-big-transact-sql.md) function. The only difference between the two functions is their return values. COUNT always returns an **int** data type value. COUNT_BIG always returns a **bigint** data type value.
37
+
This function returns the number of items found in a group. `COUNT` operates like the [COUNT_BIG](../../t-sql/functions/count-big-transact-sql.md) function. These functions differ only in the data types of their return values. `COUNT` always returns an **int** data type value. `COUNT_BIG` always returns a **bigint** data type value.
38
38
39
39
[Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
Applies the aggregate function to all values. ALL is the default.
66
+
Applies the aggregate function to all values. ALL serves as the default.
67
67
68
68
DISTINCT
69
-
Specifies that COUNT returns the number of unique nonnull values.
69
+
Specifies that `COUNT` returns the number of unique nonnull values.
70
70
71
71
*expression*
72
-
Is an [expression](../../t-sql/language-elements/expressions-transact-sql.md) of any type except **text**, **image**, or **ntext**. Aggregate functions and subqueries are not permitted.
72
+
An [expression](../../t-sql/language-elements/expressions-transact-sql.md) of any type, except **image**, **ntext**, or **text**. Note that `COUNT` does not support aggregate functions or subqueries in an expression.
73
73
74
74
\*
75
-
Specifies that all rows should be counted to return the total number of rows in a table. COUNT(\*) takes no parameters and cannot be used with DISTINCT. COUNT(\*) does not require an *expression* parameter because, by definition, it does not use information about any particular column. COUNT(*) returns the number of rows in a specified table without getting rid of duplicates. It counts each row separately. This includes rows that contain null values.
75
+
Specifies that `COUNT`should count all rows to determine the total table row count to return. `COUNT(*)` takes no parameters and does not support the use of DISTINCT. `COUNT(*)` does not require an *expression* parameter because by definition, it does not use information about any particular column. `COUNT(*)` returns the number of rows in a specified table, and it preserves duplicate rows. It counts each row separately. This includes rows that contain null values.
76
76
77
77
OVER **(**[*partition_by_clause*][*order_by_clause*][*ROW_or_RANGE_clause*]**)**
78
-
*partition_by_clause* divides the result set produced by the FROM clause into partitions to which the function is applied. If not specified, the function treats all rows of the query result set as a single group. *order_by_clause* determines the logical order in which the operation is performed. For more information, see [OVER Clause (Transact-SQL)](../../t-sql/queries/select-over-clause-transact-sql.md).
79
-
78
+
The *partition_by_clause* divides the result set produced by the `FROM` clause into partitions to which the `COUNT`function is applied. If not specified, the function treats all rows of the query result set as a single group. The *order_by_clause* determines the logical order of the operation. See [OVER Clause (Transact-SQL)](../../t-sql/queries/select-over-clause-transact-sql.md) for more information.
79
+
80
80
## Return types
81
81
**int**
82
82
83
83
## Remarks
84
-
COUNT(*) returns the number of items in a group. This includes NULL values and duplicates.
84
+
COUNT(\*) returns the number of items in a group. This includes NULL values and duplicates.
85
85
86
-
COUNT(ALL *expression*) evaluates *expression* for each row in a group and returns the number of nonnull values.
86
+
COUNT(ALL *expression*) evaluates *expression* for each row in a group, and returns the number of nonnull values.
87
87
88
-
COUNT(DISTINCT *expression*) evaluates *expression* for each row in a group and returns the number of unique, nonnull values.
88
+
COUNT(DISTINCT *expression*) evaluates *expression* for each row in a group, and returns the number of unique, nonnull values.
89
89
90
-
For return values greater than 2^31-1, COUNT produces an error. Use COUNT_BIG instead.
90
+
For return values exceeding 2^31-1, `COUNT` returns an error. For these cases, use `COUNT_BIG` instead.
91
91
92
-
COUNT is a deterministic function when used without the OVER and ORDER BY clauses. It is nondeterministic when specified with the OVER and ORDER BY clauses. For more information, see [Deterministic and Nondeterministic Functions](../../relational-databases/user-defined-functions/deterministic-and-nondeterministic-functions.md).
92
+
`COUNT` is a deterministic function when used ***without*** the OVER and ORDER BY clauses. It is nondeterministic when used ***with*** the OVER and ORDER BY clauses. See [Deterministic and Nondeterministic Functions](../../relational-databases/user-defined-functions/deterministic-and-nondeterministic-functions.md) for more information.
93
93
94
94
## Examples
95
95
96
96
### A. Using COUNT and DISTINCT
97
-
The following example lists the number of different titles that an employee who works at [!INCLUDE[ssSampleDBCoFull](../../includes/sssampledbcofull-md.md)] can hold.
97
+
This example returns the number of different titles that an [!INCLUDE[ssSampleDBCoFull](../../includes/sssampledbcofull-md.md)] employee can hold.
98
98
99
99
```sql
100
100
SELECTCOUNT(DISTINCT Title)
@@ -111,8 +111,8 @@ GO
111
111
(1 row(s) affected)
112
112
```
113
113
114
-
### B. Using COUNT(*)
115
-
The following example finds the total number of employees who work at [!INCLUDE[ssSampleDBCoFull](../../includes/sssampledbcofull-md.md)].
114
+
### B. Using COUNT(\*)
115
+
This example returns the total number of [!INCLUDE[ssSampleDBCoFull](../../includes/sssampledbcofull-md.md)] employees.
116
116
117
117
```sql
118
118
SELECTCOUNT(*)
@@ -129,8 +129,8 @@ GO
129
129
(1 row(s) affected)
130
130
```
131
131
132
-
### C. Using COUNT(*) with other aggregates
133
-
The following example shows that `COUNT(*)`can be combined with other aggregate functions in the select list. The example uses the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database.
132
+
### C. Using COUNT(\*) with other aggregates
133
+
This example shows that `COUNT(*)`works with other aggregate functions in the `SELECT` list. The example uses the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database.
134
134
135
135
```sql
136
136
SELECTCOUNT(*), AVG(Bonus)
@@ -149,7 +149,7 @@ GO
149
149
```
150
150
151
151
### D. Using the OVER clause
152
-
The following example uses the MIN, MAX, AVG and COUNT functions with the OVER clause to provide aggregated values for each department in the `HumanResources.Department` table in the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database.
152
+
This example uses the `MIN`, `MAX`, `AVG` and `COUNT` functions with the `OVER` clause, to return aggregated values for each department in the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database`HumanResources.Department` table.
## Examples: [!INCLUDE[ssSDWfull](../../includes/sssdwfull-md.md)] and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
195
195
196
196
### E. Using COUNT and DISTINCT
197
-
The following example lists the number of different titles that an employee who works at a specific company can hold.
197
+
This example returns the number of different titles that an employee of a specific company can hold.
198
198
199
199
```sql
200
200
USE ssawPDW;
@@ -210,8 +210,8 @@ FROM dbo.DimEmployee;
210
210
67
211
211
```
212
212
213
-
### F. Using COUNT(*)
214
-
The following example returns the total number of rows in the `dbo.DimEmployee` table.
213
+
### F. Using COUNT(\*)
214
+
This example returns the total number of rows in the `dbo.DimEmployee` table.
215
215
216
216
```sql
217
217
USE ssawPDW;
@@ -227,8 +227,8 @@ FROM dbo.DimEmployee;
227
227
296
228
228
```
229
229
230
-
### G. Using COUNT(*) with other aggregates
231
-
The following example combines `COUNT(*)` with other aggregate functions in the SELECT list. The query returns the number of sales representatives with a annual sales quota greater than $500,000 and the average sales quota.
230
+
### G. Using COUNT(\*) with other aggregates
231
+
This example combines `COUNT(*)` with other aggregate functions in the `SELECT` list. It returns the number of sales representatives with an annual sales quota greater than $500,000, and the average sales quota of those sales representatives.
232
232
233
233
```sql
234
234
USE ssawPDW;
@@ -248,7 +248,7 @@ TotalCount Average Sales Quota
248
248
```
249
249
250
250
### H. Using COUNT with HAVING
251
-
The following example uses COUNT with the HAVING clause to return the departments in a company that have more than 15 employees.
251
+
This example uses `COUNT` with the `HAVING` clause to return the departments of a company, each of which has more than 15 employees.
252
252
253
253
```sql
254
254
USE ssawPDW;
@@ -270,7 +270,7 @@ Production 179
270
270
```
271
271
272
272
### I. Using COUNT with OVER
273
-
The following example uses COUNT with the OVER clause to return the number of products that are contained in each of the specified sales orders.
273
+
This example uses `COUNT` with the `OVER` clause, to return the number of products contained in each of the specified sales orders.
0 commit comments