Select Query that Includes count(*)

There are times when you might need to include a count of records in a simple select query.

select *,
(select count(*) from alison.ActivityLog) as rowCount
from alison.ActivityLog

Some databases will require that you list the fields rather than using * to designate all fields:

Select CurrentPage, StatusCode, ErrorCode,
(select count(*) from alison.ActivityLog) as rowCount
from alison.ActivityLog

About the Author Scott Salisbury

Scott is the creator of Motivated Code Pro and the Managing Partner of Pinch Hitter Solutions, Inc. Motivated Code Pro is devoted to helping developers build better software careers. Pinch Hitter Solutions (phs4j.com) is a consultancy focused on mobile app development and enterprise web work. Scott works primarily in Java and JavaScript and focuses on Spring and Ext JS.

Leave a Comment: