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

Last modified date:

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.

My name is Jan Zedníček and I work as a BI Developer at Kentico Software in Brno. Mostly you can see me there working in the office, but I also work partly as a freelancer. I have been working as a freelancer for many companies for more than 5 years, but Kentico is the matter of my heart. I used to work as a financial controller at companies like Aero Vodochody or Sberbank and I also used to be a bond program manager in Unicapital Investment group. When I am not at work, I like playing volleyball, chess, doing a workout in the gym and I enjoy tasting of best quality rums. I am trying to summarize all my knowledge on this website not to forget them (because of the rum effect, you know =) and to put them forward to anyone. Don´t worry about asking for help or writing some comments.

Category: SQL Functions

About Ing. Jan Zedníček - BI Developer, Finance controller

My name is Jan Zedníček and I work as a BI Developer at Kentico Software in Brno. Mostly you can see me there working in the office, but I also work partly as a freelancer. I have been working as a freelancer for many companies for more than 5 years, but Kentico is the matter of my heart. I used to work as a financial controller at companies like Aero Vodochody or Sberbank and I also used to be a bond program manager in Unicapital Investment group. When I am not at work, I like playing volleyball, chess, doing a workout in the gym and I enjoy tasting of best quality rums. I am trying to summarize all my knowledge on this website not to forget them (because of the rum effect, you know =) and to put them forward to anyone. Don´t worry about asking for help or writing some comments.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.