๐Ÿ”ข SQL Aggregate Functions
Estimated reading: 2 minutes 43 views

๐Ÿ”ข SQL COUNT() Function โ€“ Count Records in Your Tables

๐Ÿงฒ Introduction โ€“ What is SQL COUNT()?

The COUNT() function in SQL is used to count the number of rows returned by a query. It is one of the most commonly used aggregate functions and works with both numeric and non-numeric data.

๐ŸŽฏ In this guide, you’ll learn how to:

  • Use COUNT() to count all rows or non-NULL values
  • Count rows by group using GROUP BY
  • Filter results with HAVING
  • Understand the difference between COUNT(*), COUNT(column), and COUNT(DISTINCT column)

โœ… 1. Basic COUNT() Syntax

SELECT COUNT(*) FROM table_name;

โœ… Counts all rows, including those with NULLs.


๐Ÿ” 2. COUNT(column) โ€“ Excludes NULLs

SELECT COUNT(email) FROM users;

โœ… Counts only rows where email is not NULL.


๐Ÿ†š 3. COUNT(*) vs COUNT(column)

FunctionDescription
COUNT(*)Counts all rows, including NULLs
COUNT(column)Counts only non-NULL values in the column

๐ŸŽฏ 4. COUNT(DISTINCT column)

SELECT COUNT(DISTINCT department) FROM employees;

โœ… Counts unique departments only.


๐Ÿ“Š 5. COUNT() with GROUP BY

SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department;

โœ… Counts the number of employees per department.


๐Ÿ”Ž 6. Filtering Counts with HAVING

SELECT department, COUNT(*) AS total
FROM employees
GROUP BY department
HAVING COUNT(*) > 10;

โœ… Shows only departments with more than 10 employees.


๐Ÿ“˜ Best Practices

โœ… RecommendedโŒ Avoid This
Use aliases for readable outputLeaving COUNT() column unnamed
Use COUNT(*) when counting all rowsUsing COUNT(column) if NULLs exist
Use DISTINCT carefully with COUNTAssuming COUNT() handles uniqueness by default

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

The COUNT() function is fundamental for SQL querying. It helps determine how many records exist, either in total or under certain conditions.

๐Ÿ” Key Takeaways:

  • COUNT(*) counts all rows
  • COUNT(column) ignores NULLs
  • COUNT(DISTINCT col) counts unique values
  • Combine with GROUP BY for grouped insights

โš™๏ธ Real-World Relevance:
Used in reporting totals, dashboard KPIs, inventory counts, user activity tracking, and analytics.

โžก๏ธ Next: Explore CASE WHEN for conditional aggregation.


โ“ FAQ โ€“ SQL COUNT Function

โ“ What does COUNT(*) count?

โœ… It includes all rows, even if columns have NULLs.

โ“ When should I use COUNT(column)?

โœ… When you want to count only rows where that column has a value (non-NULL).

โ“ Can I count distinct values?

โœ… Yes, use COUNT(DISTINCT column_name).

โ“ What is the difference between COUNT(*) and COUNT(1)?

โœ… Functionally the same, but COUNT(*) is more readable and standard.

โ“ Can I use COUNT() with JOINs?

โœ… Absolutely. You can count joined rows or group counts per relationship.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ”ข SQL COUNT

Or Copy Link

CONTENTS
Scroll to Top