๐Ÿ“ What Youโ€™ll Learn

  • What COUNT() does
  • Basic and advanced syntax
  • Real-world examples
  • How to use COUNT() with GROUP BY and WHERE

๐Ÿ“Œ What Is COUNT?

The COUNT() function returns the number of rows that match a specific condition.

It is commonly used to:

  • Count all rows
  • Count non-null values in a column
  • Get totals for grouped data

๐Ÿงฑ Basic Syntax

Count all rows (including NULLs)

SELECT COUNT(*) FROM table_name;

Count non-null values in a column

SELECT COUNT(column_name) FROM table_name;

๐Ÿงช Example: Total Users

SELECT COUNT(*) AS total_users
FROM users;

๐Ÿงช Example: Count Users with Email

SELECT COUNT(email) AS users_with_email
FROM users;

โœ… This excludes rows where email is NULL.

๐Ÿ“Š Example with GROUP BY

Count users per country

SELECT country, COUNT(*) AS user_count
FROM users
GROUP BY country;

๐Ÿ“Œ Example with WHERE

Count users from USA

SELECT COUNT(*) AS usa_users
FROM users
WHERE country = 'USA';

๐Ÿ“˜ Recap

  • COUNT(*) counts all rows, including those with NULLs
  • COUNT(column) counts non-null values only
  • Combine with GROUP BY for grouped totals
  • Add WHERE to count conditionally