SQL COUNT Function – How to Count Rows In a Table with Examples

SQL COUNT function belongs to Aggregate functions and it is one of the most easy-to-use and also most used functions of SQL. It enables user to count rows in certain column or rows in whole table. NULL records are missed out if we count number of records above certain column using sql COUNT.

SQL COUNT function Syntax and Examples

1 Number of rows in whole table:

SELECT COUNT(*) AS Row_Count
FROM dbo.Table

Returns count of all records in the table where at least 1 column value is non-empty

2 Number of records with non-empty value in selected column:

SELECT COUNT(Column) AS Number_of_non-empty_rows
FROM dbo.Table

Returns number of non-empty (NOT NULL) values above the column “Column”

3 Agregated number of all records in the table through certain fields:

SELECT Column, COUNT(*) AS Row_Count_by_Column
FROM dbo.Table
GROUP BY Column

Returns aggregated number of records through all unique values of “Column” field. Clause GROUP BY is mandatory in this case.

5/5 - (2 votes)
Category: SQL Functions

About Ing. Jan Zedníček - Data Engineer & Controlling

My name is Jan Zednicek, and I have been working as a freelance Data Engineer for roughly 10 years. During this time, I have been publishing case studies and technical guides on this website, targeting professionals, students, and enthusiasts interested in Data Engineering particularly on Microsoft technologies as well as corporate finance and reporting solutions. 🔥 If you found this article helpful, please share it or mention me on your website or Community forum

Leave a Reply

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