SELECT department, count(department) FROM table GROUP BY department #### use strict; my %testhash = ( 8172263 => { name => 'Robert', department => 'music' }, 8177234 => { name => 'Elizabeth', department => 'chemistry' }, 7226331 => { name => 'Reginald', department => 'chemistry' }, 8117223 => { name => 'Ralph', position => 'King' }, 9118223 => { name => 'Richard', department => 'music' }, 9119233 => { name => 'Pamela', department => 'music' } ); # how do I discover the number of # chemistry students in my hash very quickly? my $musicstudents = grep { $testhash{$_}->{department} && $testhash{$_}->{department} =~ m/music/ } keys %testhash; print( "$musicstudents\n" ); #### my %outputhash = somefunction(\%testhash, 'department'); #### # my %outputhash = ( music => 3, # chemistry => 2 ); #### my %outputhash; $outputhash{$_->{department}}++ for grep { $_->{department} } values %testhash;