SQL COUNT Function

Quick summary: The SQL COUNT() function returns the number of rows that match a condition.

SQL COUNT Syntax

SELECT COUNT(column_name) FROM table_name WHERE condition
SQL

SQL COUNT Basic examples

SELECT COUNT(*) FROM users;
SQL
Output:
Total number of users

Counts all rows in a table.

SQL COUNT Real-world usage

SELECT COUNT(*) FROM orders WHERE status = 'paid';
SQL
Output:
Number of paid orders

Counts rows matching a condition.

SQL COUNT Edge cases

SELECT COUNT(email) FROM users;
SQL
Output:
Number of non-NULL emails

COUNT(column) ignores NULL values.

SQL COUNT Common mistakes

Using COUNT(column) instead of COUNT(*)

NULL values are excluded.

Incorrect
COUNT(email)
Correct
COUNT(*)

Use COUNT(*) when you need all rows.

SQL COUNT Frequently Asked Questions

What does COUNT() do in SQL?

Returns the number of rows.

COUNT(*) vs COUNT(column)?

COUNT(*) counts all rows, COUNT(column) ignores NULL.

Use case of COUNT()?

Counting records.

Returns type?

Integer.

Common mistake?

Confusing COUNT(*) with COUNT(column).

Works with GROUP BY?

Yes.

Performance?

Fast with indexes.

Handles NULL?

Depends on usage.

Used in analytics?

Yes.

Alternative?

None.

Supports DISTINCT?

Yes.

Best practice?

Use COUNT(*) when possible.

SQL COUNT Related SQL Keywords