Skip to main content
Log inGet a demo
Back to SQL Dictionary
Aggregate functions

SQL COUNT

What is SQL COUNT?

The SQL COUNT aggregate function is used to count the number of rows that meet a specified condition or retrieve the total number of rows in a table. It provides valuable insights into the size or occurrence of data within a database.

When you would use it

You would use the COUNT function when you want to determine the number of records in a table that satisfy a specific condition or when you simply need to count all the rows in a table. It is often used for reporting, analytics, and data analysis.

Syntax

The basic syntax for using the COUNT aggregate function in an SQL query is as follows:

SELECT COUNT(column_name)
FROM table_name
WHERE condition;
  • SELECT: Specifies the result set.
  • COUNT(column_name): Counts the number of rows that have a non-null value in the specified column.
  • FROM: Specifies the table from which you want to retrieve data.
  • WHERE: Specifies the condition for filtering rows (optional).

Parameter values

  • column_name: The name of the column to count. You can also use * to count all rows regardless of column values.
  • table_name: The name of the table you want to count rows from.
  • condition: (Optional) A condition to filter rows before counting. If omitted, all rows in the table are counted.

Example query

Let's assume we have a table named "orders" with columns: "order_id," "customer_id," and "order_date." Here's an example query using the COUNT function:

SELECT COUNT(order_id)
FROM orders
WHERE customer_id = 101;

In this query, we're counting the number of orders made by the customer with customer_id equal to 101.

Example table response

Suppose the "orders" table contains the following data:

order_idcustomer_idorder_date
11012023-01-15
21022023-01-16
31012023-01-18
41032023-01-20

The query would return:

COUNT(order_id)
2

There are two orders associated with customer_id 101.

Use cases

The COUNT aggregate function is valuable in various scenarios, including:

  1. Calculating the number of specific records that meet a particular condition, such as counting orders for a specific customer.
  2. Finding the total number of rows in a table, which is useful for basic table statistics.
  3. Generating summary reports and aggregating data for analysis.

SQL languages this is available for

The COUNT aggregate function is a standard feature supported by all major SQL database management systems (DBMS) and SQL-based languages, including but not limited to:

  • MySQL
  • PostgreSQL
  • Oracle Database
  • Microsoft SQL Server
  • SQLite
  • IBM Db2

In summary, the COUNT aggregate function is a fundamental tool for counting rows and aggregating data in SQL databases. It is available in all major SQL database systems, allowing you to gain insights into the size and distribution of data within your tables.

Related

SQL Less Than or Equal To

SQL AVG

SQL SUM

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.