my $sql = "select ..."; # query string as suggested by Corion my $sth = $dbh->prepare( $sql ); $sth->execute; my $row_data = $sth->fetchall_arrayref; # gets all data from query my %data_structure; for my $row ( @$row_data ) { my ( $college, $dept, $it, $ip ) = @$row; $data_structure{$college}{$dept}{$it} = $ip; }