0
0
SQLquery~3 mins

COUNT(*) vs COUNT(column) difference in SQL - When to Use Which

Choose your learning style9 modes available
The Big Idea

Discover how a tiny difference in counting can change your data insights completely!

The Scenario

Imagine you have a big list of customer orders in a spreadsheet. You want to know how many orders there are in total and how many orders actually have a delivery date filled in. Doing this by hand means scanning each row and counting carefully.

The Problem

Counting manually is slow and easy to mess up, especially if some orders don't have delivery dates. You might count empty cells by mistake or miss some rows. It's hard to keep track and get accurate numbers quickly.

The Solution

Using SQL's COUNT(*) and COUNT(column) lets the database do the counting perfectly and fast. COUNT(*) counts all rows, while COUNT(column) counts only rows where that column has a non-NULL value. This removes guesswork and speeds up your work.

Before vs After
Before
count = 0
for row in orders:
  if row.delivery_date is not None:
    count += 1
After
SELECT COUNT(*) FROM orders;
SELECT COUNT(delivery_date) FROM orders;
What It Enables

You can quickly get total counts and counts of specific data presence, helping you understand your data better and make decisions faster.

Real Life Example

A store manager wants to know how many orders were placed (total rows) and how many have been shipped (delivery date filled). Using COUNT(*) and COUNT(delivery_date) gives these numbers instantly.

Key Takeaways

COUNT(*) counts all rows, including those with empty columns.

COUNT(column) counts only rows where that column is not NULL.

Using these functions saves time and avoids manual counting errors.