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.
38
+
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.
39
39
40
40
[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.
67
+
Applies the aggregate function to all values. ALL serves as the default.
68
68
69
69
DISTINCT
70
-
Specifies that COUNT returns the number of unique nonnull values.
70
+
Specifies that `COUNT` returns the number of unique nonnull values.
71
71
72
72
*expression*
73
-
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.
73
+
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.
74
74
75
75
\*
76
-
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.
76
+
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.
77
77
78
78
OVER **(**[*partition_by_clause*][*order_by_clause*][*ROW_or_RANGE_clause*]**)**
79
-
*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).
80
-
79
+
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.
80
+
81
81
## Return types
82
82
**int**
83
83
84
84
## Remarks
85
-
COUNT(*) returns the number of items in a group. This includes NULL values and duplicates.
85
+
COUNT(\*) returns the number of items in a group. This includes NULL values and duplicates.
86
86
87
-
COUNT(ALL *expression*) evaluates *expression* for each row in a group and returns the number of nonnull values.
87
+
COUNT(ALL *expression*) evaluates *expression* for each row in a group, and returns the number of nonnull values.
88
88
89
-
COUNT(DISTINCT *expression*) evaluates *expression* for each row in a group and returns the number of unique, nonnull values.
89
+
COUNT(DISTINCT *expression*) evaluates *expression* for each row in a group, and returns the number of unique, nonnull values.
90
90
91
-
For return values greater than 2^31-1, COUNT produces an error. Use COUNT_BIG instead.
91
+
For return values exceeding 2^31-1, `COUNT` returns an error. For these cases, use `COUNT_BIG` instead.
92
92
93
-
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).
93
+
`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.
94
94
95
95
## Examples
96
96
97
97
### A. Using COUNT and DISTINCT
98
-
The following example lists the number of different titles that an employee who works at [!INCLUDE[ssSampleDBCoFull](../../includes/sssampledbcofull-md.md)] can hold.
98
+
This example returns the number of different titles that an [!INCLUDE[ssSampleDBCoFull](../../includes/sssampledbcofull-md.md)] employee can hold.
99
99
100
100
```sql
101
101
SELECTCOUNT(DISTINCT Title)
@@ -112,8 +112,8 @@ GO
112
112
(1 row(s) affected)
113
113
```
114
114
115
-
### B. Using COUNT(*)
116
-
The following example finds the total number of employees who work at [!INCLUDE[ssSampleDBCoFull](../../includes/sssampledbcofull-md.md)].
115
+
### B. Using COUNT(\*)
116
+
This example returns the total number of [!INCLUDE[ssSampleDBCoFull](../../includes/sssampledbcofull-md.md)] employees.
117
117
118
118
```sql
119
119
SELECTCOUNT(*)
@@ -130,8 +130,8 @@ GO
130
130
(1 row(s) affected)
131
131
```
132
132
133
-
### C. Using COUNT(*) with other aggregates
134
-
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.
133
+
### C. Using COUNT(\*) with other aggregates
134
+
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.
135
135
136
136
```sql
137
137
SELECTCOUNT(*), AVG(Bonus)
@@ -150,7 +150,7 @@ GO
150
150
```
151
151
152
152
### D. Using the OVER clause
153
-
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.
153
+
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)]
196
196
197
197
### E. Using COUNT and DISTINCT
198
-
The following example lists the number of different titles that an employee who works at a specific company can hold.
198
+
This example returns the number of different titles that an employee of a specific company can hold.
199
199
200
200
```sql
201
201
USE ssawPDW;
@@ -211,8 +211,8 @@ FROM dbo.DimEmployee;
211
211
67
212
212
```
213
213
214
-
### F. Using COUNT(*)
215
-
The following example returns the total number of rows in the `dbo.DimEmployee` table.
214
+
### F. Using COUNT(\*)
215
+
This example returns the total number of rows in the `dbo.DimEmployee` table.
216
216
217
217
```sql
218
218
USE ssawPDW;
@@ -228,8 +228,8 @@ FROM dbo.DimEmployee;
228
228
296
229
229
```
230
230
231
-
### G. Using COUNT(*) with other aggregates
232
-
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.
231
+
### G. Using COUNT(\*) with other aggregates
232
+
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.
233
233
234
234
```sql
235
235
USE ssawPDW;
@@ -249,7 +249,7 @@ TotalCount Average Sales Quota
249
249
```
250
250
251
251
### H. Using COUNT with HAVING
252
-
The following example uses COUNT with the HAVING clause to return the departments in a company that have more than 15 employees.
252
+
This example uses `COUNT` with the `HAVING` clause to return the departments of a company, each of which has more than 15 employees.
253
253
254
254
```sql
255
255
USE ssawPDW;
@@ -271,7 +271,7 @@ Production 179
271
271
```
272
272
273
273
### I. Using COUNT with OVER
274
-
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.
274
+
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