---
schema_version: "1"
id: d0b64c061f464cbc636e6757eea5459775cbe5c1fc0242897d1ddc895a3cd559
title: COUNT() and COUNT(fieldName)
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/salesforce-soql-sosl/guides/soql-sosl/sforce-api-calls-soql-select-count.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-soql-sosl/guide/sforce-api-calls-soql-select-count.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: ba6368ad7510098e88cd204d5a012bfa4dabbe3da2b606f26baea9cb1589b715
products:
  - SOQL and SOSL Reference
  - Platform Developer Content
---

# `COUNT()` and `COUNT(fieldName)`

To discover the number of rows that a query returns, use the aggregate function `COUNT()` in a `SELECT` statement of a SOQL query.

## `COUNT()`

`COUNT()` returns the number of rows that match the filtering conditions.

For example:

<!-- otherprops: soql -->

```codesegment
SELECT COUNT()
FROM Account
WHERE Name LIKE 'a%'
```

<!-- otherprops: soql -->

```codesegment
SELECT COUNT()
FROM Contact, Contact.Account
WHERE Account.Name = 'MyriadPubs'
```

For `COUNT()`, the `size` field of the QueryResult object returns the number of rows retrieved in the query. The `records` field returns `null`.

Note the following when using `COUNT()`:

- `COUNT()` must be the only element in the `SELECT` list.
- The number of rows returned by `COUNT()` includes null values that match the filtering conditions of the query.
- You can use `COUNT()` with a `LIMIT` clause.
- You can't use `COUNT()` with an `ORDER BY` clause.

## `COUNT(fieldName)`

`COUNT(fieldName)` returns the number of rows that match the filtering conditions and have a non-`null` value for fieldName.

For example:

<!-- otherprops: soql -->

```codesegment
SELECT COUNT(Id)
FROM Account
WHERE Name LIKE 'a%'
```

**note:** `COUNT()` and `COUNT(Id)` in SOQL are similar to `COUNT(*)` in SQL.

For `COUNT(fieldName)`, the object in the `records` field returns the number of rows. The `size` field does not reflect the count. For example:

<!-- otherprops: soql -->

```codesegment
SELECT COUNT(Id)
FROM Account
WHERE Name LIKE 'a%'
```

The count is returned in `expr0`, which is the implied alias for the aggregated field.

You can include multiple `COUNT(fieldName)` items in a `SELECT` clause. For example, the following query returns the number of opportunities, and the number of opportunities associated with a campaign.

<!-- otherprops: soql -->

```codesegment
SELECT COUNT(Id), COUNT(CampaignId)
FROM Opportunity
```

You can use `GROUP BY` clause with `COUNT(fieldName)` to analyze your records and return summary reporting information. For example, the following query returns the number of leads for each `LeadSource` value:

<!-- otherprops: set_clipboard -->

```soql
SELECT LeadSource, COUNT(Name)
FROM Lead
GROUP BY LeadSource
```

**See Also**

- [_Apex Reference Guide_: countQuery(query)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_database.htm#apex_System_Database_countQuery)
