Skip to content

SQL - COUNT(*) vs COUNT(column_name) - Which Should You Use?

Posted on:November 6, 2023 at 04:59 AM

When working with SQL, counting rows is a common operation, and two primary methods are often employed: COUNT(*) and COUNT(column_name). While both can provide the desired results, understanding the differences between them is crucial for efficient and effective database querying. In this article, we’ll dive into the distinctions and use cases for COUNT(*) and COUNT(column_name).

COUNT(*) - Counting All Rows

The COUNT(*) function is a straightforward way to count all rows in a table. It does not require specifying a column name; it simply counts all rows, regardless of their content. Here are some key points to consider when using COUNT(*):

COUNT(column_name) - Counting Specific Column Values

On the other hand, COUNT(column_name) is used to count the non-null values in a specific column. Here’s what you need to know about this method:

Performance Considerations

It’s essential to understand the performance implications of your choice between COUNT(*) and COUNT(column_name):

Example 1: Using COUNT(*) for Total Row Count

Suppose you have a table called products that stores information about various products, and you want to find out the total number of products in the table.

SQL Query using COUNT(*):

SELECT COUNT(*) AS TotalProducts
FROM products;

Explanation:

In this query, COUNT(*) is used to count all rows in the products table, regardless of their content. The result will be a single value, indicating the total number of products in the table.

Example 2: Using COUNT(column_name) for Specific Value Count

Let’s say you want to know how many products in the products table belong to a specific category, ‘Electronics,’ and the category is stored in the category column.

SQL Query using COUNT(column_name):

SELECT  COUNT(category) AS ElectronicsProducts
FROM products WHERE category =  'Electronics';

Explanation:

In this query, COUNT(category) is used to count the number of non-null values in the category column where the category is ‘Electronics’. The WHERE clause filters the rows to include only those with ‘Electronics’ as the category. The result will be the count of products in the ‘Electronics’ category.

Example 3: Using COUNT(column_name) with GROUP BY

Suppose you want to count the number of products in each category from the products table.

SQL Query using COUNT(column_name) with GROUP BY:

SELECT category, COUNT(category) AS CategoryCount
FROM products GROUP  BY category;

Explanation:

In this query, COUNT(category) is used along with GROUP BY to count the number of products in each category. The result will be a list of categories and the count of products within each category.

Example 4: Using COUNT(*) for Counting All Orders

Let’s say you have a table named orders and you want to find out the total number of orders placed.

SQL Query using COUNT(*):

SELECT COUNT(*) AS TotalOrders
FROM orders;

Explanation:

In this query, COUNT(*) is used to count all rows in the orders table, representing the total number of orders placed.

Example 5: Using COUNT(column_name) for Counting Distinct Values

Suppose you want to count the number of unique cities where your customers are located. The customer’s city is stored in the city column.

SQL Query using COUNT(column_name):

SELECT COUNT(DISTINCT city) AS UniqueCities
FROM customers;

Explanation:

In this query, COUNT(DISTINCT city) is used to count the number of distinct (unique) values in the city column of the customers table. The result will be the count of unique cities where your customers are located.

Conclusion

In SQL, both COUNT(*) and COUNT(column_name) have their places, depending on your specific requirements:

The choice between the two depends on your particular use case and performance considerations. Understanding the nuances of each method empowers you to write more efficient and precise SQL queries, leading to better database management and data analysis.