What is the Difference Between COUNT(*), COUNT(1), COUNT(column name), and COUNT(DISTINCT column name)? (2023)

Have you noticed there are different variations of the SQL COUNT() function? This article explains the various arguments and their uses.

As a SQL user, you’re probably quite familiar with the COUNT() function. Even though it’s relatively simple, it can be used in several different ways. Each way has a very different use. I imagine you’ve seen code that contains the function COUNT(*) or COUNT(1). You’ve probably also seen some other uses of the COUNT() function, such as COUNT(column name) and COUNT(DISTINCT column name), even if you haven’t used them.

You’re probably wondering what each variation of COUNT() does. Let’s find out!

The best way to practice SQL is our interactive SQL Practice Set course. It contains over 90 hands-on SQL exercises to review and refresh the most important SQL topics, including COUNT() and GROUP BY. With each exercise solved you build confidence in your SQL skills.

What Does the COUNT() Function do?

As you can imagine, the COUNT() function counts. But what does it count? The COUNT() function belongs to SQL’s aggregate functions. It counts the number of rows that satisfy the criteria defined in the parentheses. It does not return the rows themselves; it shows the number of rows that meet your criteria.

Speaking of aggregate functions, they are extremely useful in SQL reports. If you want to satisfy your curiosity, there’s plenty of aggregate functions and “grouping by” in our Creating Basic SQL Reports course.

Now, back to counting. There are (naturally) different things that can be counted. That’s why there are different variations of the COUNT() function. In this article, I’ll concentrate on four:

  • COUNT(*)
  • COUNT(1)
  • COUNT(column name)
  • COUNT(DISTINCT column name)

COUNT(*) vs COUNT(1)

You may have seen various discussions about the differences between COUNT(*) and COUNT(1). And maybe trying to find the answer confused you even more. So, is there any difference? The simple answer is no – there is no difference at all.

The COUNT(*) function counts the total rows in the table, including the NULL values. The semantics for COUNT(1) differ slightly; we’ll discuss them later. However, the results for COUNT(*) and COUNT(1) are identical.

(Video) Difference between COUNT (*) , COUNT(1), COUNT(Col_Name) and COUNT(Distinct(Col_Name) in SQL

Let’s test this claim using an example query. Suppose I have a table named orders that contains these columns:

  • order_id: The ID of the order.
  • customer_id: The ID of the customer who placed the order.
  • order_value: The total value of the ordered items, in euros.
  • payment_date: When the order was paid by the customer.

If I wanted to count the number of rows in the whole table, I’d use the COUNT() function in the following way:

SELECT COUNT(*) AS number_of_rowsFROM orders;

As you see, I’ve used the COUNT(*) function. The result will appear in the new column number_of_rows:


Ok, but what if I use COUNT(1) instead? Here it is:

SELECT COUNT(1) AS number_of_rowsFROM orders;

The code is essentially the same. The only difference is that I’ve used COUNT(1) instead of COUNT(*). And what about the result? It returns the same number of rows:


There’s a popular misconception that “1” in COUNT(1) means “count the values in the first column and return the number of rows.” From that misconception follows a second: that COUNT(1) is faster because it will count only the first column, while COUNT(*) will use the whole table to get to the same result.

This is not true. The number in the parenthesis doesn’t mean the number of the column in the table. If you put any number in the parenthesis, I guarantee you the result will be the same. Want proof? Here it is:

SELECT COUNT(-13) AS number_of_rowsFROM orders;

If the first misconception was true, the code above would mean I want to count the number of rows in the -13th column. There are only four columns in the table orders, so there’s no 13th column. For sure, there is no column -13, whatever that should mean. Do you want to see the result of the code? Don't be surprised:


Once again, the result is the same. So what does the value in the parenthesis of COUNT() mean? It’s the value that the COUNT() function will assign to every row in the table. The function will then count how many times the asterisk (*) or (1) or (-13) has been assigned. Of course, it will be assigned a number of times that’s equal to the number of rows in the table. In other words, COUNT(1) assigns the value from the parentheses (number 1, in this case) to every row in the table, then the same function counts how many times the value in the parenthesis (1, in our case) has been assigned; naturally, this will always be equal to the number of rows in the table. The parentheses can contain any value; the only thing that won’t work will be leaving the parentheses empty.

(Video) SQL Interview Question - Difference between Count(*), Count(1), Count(colname) | Which is fastest

Let’s try something silly. Instead of a number, put the following value in the parenthesis: 'it will always be 8 rows'. Here’s the code:

SELECT COUNT('it will always be 8 rows') AS number_of_rowsFROM orders;

Run the code and – surprise, surprise – the result is really eight rows:


Since it doesn’t matter which value you put in the parentheses, it follows that COUNT(*) and COUNT(1) are precisely the same. They are precisely the same because the value in the COUNT() parentheses serves only to tell the query what it will count.

If these statements are precisely the same, then there’s no difference in the performance. Don’t let the asterisk (*) make you think it has the same use as in SELECT * statement. No, COUNT(*) will not go through the whole table before returning the number of rows, making itself slower than COUNT(1).

So, in the end, who wins in this dramatic COUNT(*) vs COUNT(1) battle? Nobody – it’s a draw; they’re exactly the same. However, I’d recommend using COUNT(*), as it’s much more commonly seen. It’s also less confusing, naturally leading other SQL users to understand that the function will count all the numbers in the table, including the NULL values.

COUNT(*) vs COUNT(column name)

How about this one, COUNT(*) vs COUNT(column name). Is there any difference? There sure is!

As you’ve already learned, COUNT(*) will count all the rows in the table, including NULL values. On the other hand, COUNT(column name) will count all the rows in the specified column while excluding NULL values.

As you already know, there are eight rows in the table orders. Let’s see how many rows there will be when I use the column order_id for counting (imagining I want to see how many orders have been placed). We’ll get eight rows again, right? Let’s see:

SELECT COUNT(order_id) AS number_of_ordersFROM orders;

Do we get the same result? Nope, there are seven orders, not eight.

(Video) Difference between COUNT(1) , COUNT(*), COUNT(COLUMN) and COUNT(ANYTHING)


Is that a mistake? No, it’s not; there are really only seven orders with an order_id; one row has a NULL instead of a proper order_id. Below is the row that makes the difference:


Always remember: COUNT(column name) will only count rows where the given column is NOT NULL.

Let’s do something interesting now and combine both COUNT() variations in one query. Suppose I want to see the customer’s ID with the total number of orders by that customer. I also want to show the total number of paid orders for that customer. (Paid orders don’t have a NULL value in the column payment_date.) Here’s how I’d do it:

SELECTcustomer_id,COUNT(*) AS number_of_orders,COUNT(payment_date) AS paid_number_of_ordersFROM ordersGROUP BY customer_id;

The query will first calculate the total number of orders using COUNT(*) – i.e. it will include NULL values. Then the part COUNT (payment_date) AS paid_number_of_orders will count the rows in the column payment_date that are NOT NULL. I want the results for each customer, so I’ve grouped the result by the column customer_id. Here’s what I get:


You can see that the difference occurs for the customer CU092.

The principles of combining GROUP BY and COUNT() are outlined in this article about GROUP BY and SQL aggregate functions. If you want some more practice, here are five examples of GROUP BY.

COUNT() allows us to use expressions as well as column names as the argument. Do you know how to find the number of the orders above €1 000 using only the COUNT() function? Here’s how:

SELECT COUNT(CASE WHEN order_price > 1000 THEN 1 END) AS significant_ordersFROM orders;

Instead of putting conditions at the end of the query and filtering after the COUNT() function does its job, we can use the CASE statement. That’s what I’ve done in the above query. It’s used like an IF-THEN-ELSE statement. CASE is followed by the condition, which is defined by the statements WHEN and THEN. There can also be an ELSE statement, but it’s unnecessary in this case – I’m only interested in counting the number of values, not in the values themselves. Every CASE statement ends with the END statement.

The COUNT() statement above reads as follows:

(Video) Aggregate Functions in SQL SERVER | COUNT(*) VS COUNT(DISTINCT)

  1. Find all the values in the column order_price above 1 000.
  2. Assign the value 1 (you can assign any value you want) to these values.
  3. Assign NULL to rows with prices below 1 000.
  4. Count the number of assigned 1s.
  5. Show the result in the column significant_orders.

Here’s the result:


COUNT(column name) vs COUNT (DISTINCT column_name)

You can probably imagine what the difference between those two COUNT() function versions is. COUNT(column_name) will include duplicate values when counting. In contrast, COUNT (DISTINCT column_name) will count only distinct (unique) rows in the defined column.

If you want to count the number of customers who’ve placed an order, maybe COUNT (column_name) will work. Let’s try this simple code:

SELECT COUNT (customer_id) AS number_of_customersFROM orders;

You’re familiar with this one; I’ve already used the COUNT(column name) function. This time it counts all rows in the column customer_id, with the result being shown in the column number_of_customers. Here’s the result:


Let’s check the result by looking at the entire orders table:


There are eight rows, but is this really the number of the customers? Notice that the customers CU108 and CU052 appear twice. If I want the real number of customers, then I need to count every customer only once. How can I do this? By using COUNT(DISTINCT customer_id):

SELECT COUNT(DISTINCT customer_id) AS number_of_customersFROM orders;

This query will also count rows in the column customer_id, but it will count every customer only once. This is due to the keyword DISTINCT. Have a look at the result:


This is the correct result; there are really only six unique customers.

Do You Think You Can Count on COUNT()?

Now that you understand several common variations of the COUNT() function, you can create more complex calculations and reports. COUNT() is one of the most used aggregate functions, so it’s vital that you clearly understand the different COUNT() variations and their purposes. If some of the COUNT() function variations we discussed in this article weren’t clear, let me know in the comment section. I’ll gladly help you. And, for more practice using COUNT(), try our Creating Basic SQL Reports course.

(Video) Session 7 Difference between Count(*) , Count (1) and Count(ColumnName)


What is the difference between count (*) and count 1 and count column name in Oracle? ›

count (col) only counts the not null entries in the table. Count(*) and count(1) are the same. Count(column_name) gives the number of rows where column_name is not null. That's incorrect, count(*) counts all rows, no matter of null values.

What is the difference between count distinct and distinct count? ›

The COUNT DISTINCT function returns the number of unique values in the column or expression, as the following example shows. SELECT COUNT (DISTINCT item_num) FROM items; If the COUNT DISTINCT function encounters NULL values, it ignores them unless every value in the specified column is NULL.

What is the difference between count (*) and count column_name? ›

The difference is: COUNT(*) will count the number of records. COUNT(column_name) will count the number of records where column_name is not null.

What is count (*) and count 1? ›

The COUNT(*)returns the total number of rows in a table, including the NULLs. My Emp table returns 5 that shows the total records in that table. The COUNT(1) function replaces all records from the query result set with value 1. If you have NULL values, it is also replaced by 1.

What is count (*) in SQL? ›

COUNT(*) with GROUP BY returns the number of rows in each group. This includes NULL values and duplicates. COUNT(ALL <expression>) evaluates expression for each row in a group, and returns the number of nonnull values.

What does count 1 mean in SQL? ›

COUNT(1) is basically just counting a constant value 1 column for each row. As other users here have said, it's the same as COUNT(0) or COUNT(42) .

What is difference between count (*) count expression count distinct expression? ›

COUNT ( * ) counts all the rows in the target table whether they include nulls or not. COUNT ( expression ) computes the number of rows with non-NULL values in a specific column or expression. COUNT ( DISTINCT expression ) computes the number of distinct non-NULL values in a column or expression.

Why do we use count (*) in SQL? ›

The COUNT() function is one of the most useful aggregate functions in SQL. Counting the total number of orders by a customer in the last few days, the number of unique visitors who bought a museum ticket, or the number of employees in a department, can all be done using the COUNT() function.

What is count Column_name? ›

The count(*) returns all rows whether column contains null value or not while count(columnName) returns the number of rows except null rows.

What does distinct mean in SQL? ›

The DISTINCT keyword in the SELECT clause is used to eliminate duplicate rows and display a unique list of values. In other words, the DISTINCT keyword retrieves unique values from a table. The basic syntax of the DISTINCT keyword in SQL is: SELECT DISTINCT.

What does count 1 and count 2 mean? ›

Search Legal Terms and Definitions

For example, the complaint in a civil (non-criminal) lawsuit might state: First Count (or cause of action) for negligence, and then state the detailed allegations; Second Count for breach of contract; Third Count for debt and so forth.

What is the use of count (*)? ›

Use the COUNT function to get the number of entries in a number field that is in a range or array of numbers. For example, you can enter the following formula to count the numbers in the range A1:A20: =COUNT(A1:A20). In this example, if five of the cells in the range contain numbers, the result is 5.

What is the use of count () and count (*)? ›

COUNT(*) with GROUP BY returns the number of rows in each group. This includes NULL values and duplicates. COUNT(ALL <expression>) evaluates expression for each row in a group, and returns the number of nonnull values.

What does count (*) do in Oracle? ›

COUNT(*) is an aggregate function that counts the number of rows accessed. No NULLs or duplicates are eliminated.

What does count (*) mean in SQL? ›

The COUNT() function returns the number of rows in the result set. For example, SELECT COUNT(*) FROM Customers; Run Code. Here, the SQL command count rows and returns the total number of rows of the Customers table.

What is distinct and COUNT in Oracle? ›

COUNT(DISTINCT expression) function returns the number of unique and non-null items in a group. COUNT(ALL expression) evaluates the expression and returns the number of non-null items in a group, including duplicate values.

What is the purpose of COUNT (*) function? ›

The COUNT function counts the number of cells that contain numbers, and counts numbers within the list of arguments. Use the COUNT function to get the number of entries in a number field that is in a range or array of numbers.

What is the meaning of COUNT += 1? ›

The expression count + 1 evaluates count , adds 1, and returns the result. The value of count is not changed. Interestingly, while it's possible to mimic ++count (pre-increment) with an alternative expression. var inc = (count += 1);


1. Difference Between COUNT(*), COUNT(1) and COUNT(column_name) In SQL | Important SQL Interview quest
(Crack Concepts)
2. SQL|Comparing COUNT(*)-COUNT(1)-COUNT('a')-COUNT(col_nm)-COUNT(DISTINCT col_nm)|Common SQL question?
3. Differences between COUNT(*), COUNT(1) and COUNT(Colname) | Oracle Aggregare Functions | SQL COUNT()
(RK SoftAcademy)
4. Difference between Count(*), Count(1), Count(colname) | SQL Interview Question | IQBees
5. 2 Ways to Calculate Distinct Count with Pivot Tables
(Excel Campus - Jon)
6. SQL Interview Scenario | Difference Between Count(*),Count(1),Count(Col_Nm)
Top Articles
Latest Posts
Article information

Author: Msgr. Refugio Daniel

Last Updated: 02/26/2023

Views: 6544

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Msgr. Refugio Daniel

Birthday: 1999-09-15

Address: 8416 Beatty Center, Derekfort, VA 72092-0500

Phone: +6838967160603

Job: Mining Executive

Hobby: Woodworking, Knitting, Fishing, Coffee roasting, Kayaking, Horseback riding, Kite flying

Introduction: My name is Msgr. Refugio Daniel, I am a fine, precious, encouraging, calm, glamorous, vivacious, friendly person who loves writing and wants to share my knowledge and understanding with you.