COUNT

Overview

The COUNT() function is an aggregate function that returns the number of rows that match a specified condition. It's one of the most commonly used aggregate functions in SQL.

COUNT() is useful for counting records, determining table sizes, and creating summary statistics.

Basic Syntax

COUNT Syntax
SELECT COUNT(column_name)
FROM table_name
WHERE condition;

COUNT(*) - Count All Rows

The most common use is COUNT(*) which counts all rows in a table:

Count all rows
SELECT COUNT(*) AS total_customers
FROM Customers;

COUNT(column_name) - Count Non-NULL Values

COUNT(column_name) counts only non-NULL values in the specified column:

Count non-NULL values
-- Count customers with email addresses
SELECT COUNT(email) AS customers_with_email
FROM Customers;

Difference Between COUNT(*) and COUNT(column)

Understanding the difference:

Expression Counts
COUNT(*) All rows, including rows with NULL values
COUNT(column_name) Only non-NULL values in the column
COUNT(DISTINCT column_name) Number of unique non-NULL values

Examples

Example 1: Count All Records

Total records
SELECT COUNT(*) AS total_products
FROM Products;

Example 2: Count with WHERE

Conditional count
SELECT COUNT(*) AS active_customers
FROM Customers
WHERE status = 'Active';

Example 3: COUNT DISTINCT

Count unique values
-- Count unique cities
SELECT COUNT(DISTINCT city) AS unique_cities
FROM Customers;

Example 4: Count with Multiple Conditions

Complex conditions
SELECT COUNT(*) AS high_value_orders
FROM Orders
WHERE total_amount > 1000
  AND order_date >= '2024-01-01';

COUNT with GROUP BY

Use COUNT() with GROUP BY to count records per group:

Count per category
SELECT 
    category,
    COUNT(*) AS product_count
FROM Products
GROUP BY category;

Example Result:

Sample Result
┌──────────────┬───────────────┐
│ category     │ product_count │
├──────────────┼───────────────┤
│ Electronics  │ 25            │
│ Clothing     │ 30            │
│ Books        │ 15            │
└──────────────┴───────────────┘

COUNT with HAVING

Filter groups using HAVING after GROUP BY:

Filter groups by count
SELECT 
    category,
    COUNT(*) AS product_count
FROM Products
GROUP BY category
HAVING COUNT(*) > 20;

Multiple COUNT Operations

You can use multiple COUNT operations in a single query:

Multiple counts
SELECT 
    COUNT(*) AS total_customers,
    COUNT(email) AS customers_with_email,
    COUNT(DISTINCT city) AS unique_cities
FROM Customers;

COUNT with JOIN

Count records from joined tables:

Count from joined tables
SELECT 
    c.name AS customer_name,
    COUNT(o.order_id) AS order_count
FROM Customers c
LEFT JOIN Orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.name;

COUNT NULL Values

To count NULL values, use a conditional expression:

Count NULL values
-- Count customers without email
SELECT COUNT(*) - COUNT(email) AS customers_without_email
FROM Customers;

-- Or use CASE
SELECT 
    COUNT(CASE WHEN email IS NULL THEN 1 END) AS customers_without_email
FROM Customers;

Common Use Cases

Performance Tips

Best Practices

Next Steps

Learn about calculating sums and averages with SUM and AVG, or explore grouping data with GROUP BY.