# count

> For the complete documentation index, see [llms.txt](https://docs.redpanda.com/llms.txt). Component-specific: [cloud-data-platform-full.txt](https://docs.redpanda.com/cloud-data-platform-full.txt)

---
title: count
latest-operator-version: v26.1.4
latest-console-tag: v3.7.3
latest-connect-version: 4.93.0
latest-redpanda-tag: v26.1.9
docname: sql/sql-functions/aggregate-functions/count
page-component-name: cloud-data-platform
page-version: master
page-component-version: master
page-component-title: Cloud
page-relative-src-path: sql/sql-functions/aggregate-functions/count.adoc
page-edit-url: https://github.com/redpanda-data/cloud-docs/edit/main/modules/reference/pages/sql/sql-functions/aggregate-functions/count.adoc
description: The `count()` function retrieves the number of records that match a specific condition.
page-topic-type: reference
page-git-created-date: "2026-05-26"
page-git-modified-date: "2026-05-26"
---

<!-- Source: https://docs.redpanda.com/cloud-data-platform/reference/sql/sql-functions/aggregate-functions/count.md -->

The `count()` function retrieves the number of records that match a specific condition. It works with any data type supported by Redpanda SQL and returns a `bigint`.

> 📝 **NOTE**
>
> The output will indicate the total number of rows in a table, regardless of the input types.

## [](#examples)Examples

This example uses an orders table that stores details of the purchase transactions:

```sql
CREATE TABLE orders (
    orderid int,
    custname text,
    orderproduct text,
    ordertotal real
);
INSERT INTO orders (orderid, custname, orderproduct, ordertotal)
VALUES
(9557411, 'Maya', 'Jeans', 10.5),
(9557421, 'Aaron', 'T-Shirt', 9.2),
(9557451, 'Alex', 'Hat', 10.8),
(9557311, 'Will', 'Hat', 8.5),
(9557321, 'Will', 'T-Shirt', 12.15),
(9557351, 'Maya', 'T-Shirt', 9.5),
(9557221, 'Maya', 'Jeans', 11.02),
(9557251, 'Alex', 'Jeans', 11.09),
(9557231, 'Aaron', 'Hat', 14.56),
(9557281, 'Aaron', 'Hat', 12.15),
(9557291, 'Will', 'T-Shirt', 13.1);
```

```sql
SELECT * FROM orders;
```

This query shows the following table:

```sql
+----------+-----------+---------------+-------------+
| orderid  | custname  | orderproduct  | ordertotal  |
+----------+-----------+---------------+-------------+
| 9557411  | Maya      | Jeans         | 10.5        |
| 9557421  | Aaron     | T-Shirt       | 9.2         |
| 9557451  | Alex      | Hat           | 10.8        |
| 9557311  | Will      | Hat           | 8.5         |
| 9557321  | Will      | T-Shirt       | 12.15       |
| 9557351  | Maya      | T-Shirt       | 9.5         |
| 9557221  | Maya      | Jeans         | 11.02       |
| 9557251  | Alex      | Jeans         | 11.09       |
| 9557231  | Aaron     | Hat           | 14.56       |
| 9557281  | Aaron     | Hat           | 12.15       |
| 9557291  | Will      | T-Shirt       | 13.1        |
+----------+-----------+---------------+-------------+
```

### [](#count-with-a-single-expression)`count()` with a single expression

This example returns the number of all orders in the orders table:

```sql
SELECT COUNT(*) FROM orders;
```

The query returns:

```sql
+-------+
| count |
+-------+
| 11    |
+-------+
```

### [](#count-with-a-group-by-clause)`count()` with a `GROUP BY` clause

This example will combine the `count()` function and the `GROUP BY` clause.

-   The `GROUP BY` clause groups the orders based on the customer’s name.

-   The `count()` function counts the orders for each customer.


```sql
SELECT custname, COUNT (orderid)
FROM orders
GROUP BY custname;
```

The query returns:

```sql
+-----------+--------+
| custname  | count  |
+-----------+--------+
| Aaron     | 3      |
| Alex      | 2      |
| Will      | 3      |
| Maya      | 3      |
+-----------+--------+
```

### [](#count-with-a-having-clause)`count()` with a `HAVING` clause

This example combines the `count()` function and the `HAVING` clause to apply a specific condition to find customers who have made more than two orders:

```sql
SELECT custname, COUNT (orderid)
FROM orders
GROUP BY custname
HAVING COUNT (orderid) > 2;
```

-   The `GROUP BY` clause groups the orders based on the customer’s name.

-   The `HAVING` clause will filter only customers with more than two order IDs.

-   The `count()` function counts the orders for each customer.


```sql
+-----------+-------+
| custname  | count |
+-----------+-------+
| Aaron     | 3     |
| Will      | 3     |
| Maya      | 3     |
+-----------+-------+
```